2013-04-15 12 views
5

Un metodo puro è definito come "does not make any visible state changes".Un metodo sta scrivendo su un logger puro?

Il mio metodo sta scrivendo un messaggio di registro se un argomento è nullo o viene generata un'eccezione. È ancora puro? Scrivere su un registratore è un cambiamento visibile?

Ecco il codice:

/// <summary> 
    /// Formats with invariant culture - won't throw an exception. 
    /// </summary> 
    /// <param name="format">A composite format string.</param> 
    /// <param name="args">An object array that contains zero or more objects to format.</param> 
    /// <returns>A copy of format in which the format items have been replaced by the string representation of the corresponding objects in args.</returns> 
    [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "CyanCor.Core.Logging.Log.ExceptionPrevent(System.String,System.String,System.Int32,System.String)", Justification = "It is for the logger (riedl)")] 
    [Pure - yes or no?] 
    public static string FormatSafe(this string format, params object[] args) 
    { 
     if (format == null) 
     { 
      Log.ExceptionPrevent("Argument format is null"); 
      return NullFormat; 
     } 

     try 
     { 
      return string.Format(CultureInfo.InvariantCulture, format, args); 
     } 
     catch (ArgumentException exc) 
     { 
      Log.Exception(exc); 
      return format; 
     } 
     catch (FormatException exc) 
     { 
      Log.Exception(exc); 
      return format; 
     } 
    } 
+0

Perché dichiararlo puro? Vuoi chiamarlo da un contratto? –

+0

Beh, si potrebbe sostenere che è una funzione pura che si traduce in una funzione impura. – sloth

risposta

5

Generalmente, "cambiamento di stato" si intende modificare lo stato di un oggetto, ad esempio. modifica di una variabile o modifica della struttura di un oggetto complesso. Seguendo questa definizione, sembrerebbe che il tuo metodo sia ancora puro.

+1

Sono d'accordo. È puro –

+0

Grazie per la spiegazione. – ldrdl

+0

Con quella definizione la modifica di un valore in un database può ancora essere considerata "pura", il che è ovviamente sbagliato. – tster

Problemi correlati