Phương pháp thuần túy được định nghĩa là "does not make any visible state changes".Có phải phương pháp viết cho trình ghi nhật ký không?
Phương pháp của tôi là viết một thông điệp tường trình nếu một đối số là null hoặc một ngoại lệ được ném. Nó vẫn tinh khiết? Là viết cho một logger một sự thay đổi có thể nhìn thấy?
Dưới đây là các mã:
/// <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;
}
}
Tại sao tuyên bố tinh khiết? Bạn có muốn gọi nó từ một hợp đồng? –
Vâng, bạn có thể cho rằng đó là một hàm thuần túy mang lại hàm số không tinh khiết. – sloth