2013-04-15 19 views
5

Saf bir yöntem "does not make any visible state changes" olarak tanımlanmıştır.Kaydediciye yazı yazan bir yöntem midir?

Yöntemim, bir argüman boşsa veya bir istisna atılırsa bir günlük mesajı yazıyor. Hala saf mı? Bir kaydediciye gözle görülür bir değişiklik mi yazılıyor?

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

Neden bunu saf olarak nitelendiriyorsunuz? Bir sözleşmeden mi aramak istiyorsunuz? –

+0

Eh, bunun bir kusurlu işleve yol açan saf bir işlevi olduğunu iddia edebilirsiniz. – sloth

cevap

5

Genel olarak, "durum değişikliği" yani bir nesnenin, durumunu değiştirerek şu anlama gelir:

İşte kod. Bir değişkeni değiştirmek veya karmaşık bir nesnenin yapısını değiştirmek. Bu tanımı takiben, yönteminizin hala saf olduğu anlaşılıyor.

+1

Katılıyorum. Bu saf. –

+0

Açıklamanız için teşekkür ederiz. – ldrdl

+0

Bu tanım ile bir veritabanındaki bir değerin değiştirilmesi, açık olarak yanlış olan "saf" olarak kabul edilebilir. – tster

İlgili konular