2013-04-15 7 views
5

純粋な方法は、 "does not make any visible state changes"と定義されています。ロガーに純粋に書き込んでいるメソッドはありますか?

私の方法では、1つの引数がnullまたは例外がスローされた場合にログメッセージを書き込んでいます。まだ純粋ですか?ロガーに目に見える変化を書いていますか?

ここでは、コードです:

/// <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

なぜそれを純粋に宣言しますか?契約書から電話したいですか? –

+0

さて、あなたはそれが不純な関数になる純粋な関数だと主張することができます。 – sloth

答えて

5

一般的に、「状態の変化」、すなわち、オブジェクトの状態を変更することを意味します。変数を変更するか、複雑なオブジェクトの構造を変更する。この定義に従えば、あなたの方法は依然として純粋なようです。

+1

私は同意します。それは純粋です。 –

+0

ありがとうございます。 – ldrdl

+0

この定義では、データベース内の値を変更することは、明らかに間違っている "純粋な"ものとみなすことができます。 – tster

関連する問題