5

I tried running FxCop on a few assemblies of our product, and I get lots and lots of matches of the "Specify IFormatProvider" rule.

As it happens, some of those are legitimate, but it also matches code like this:

Logger.DebugFormat("Appending file {0}", fileName);

Which can be written as

Logger.DebugFormat(CultureInfo.InvariantCulture, "Appending file {0}", fileName);

The second variant is much harder to read.

So, is it actually recommended to always specifiy the IFormatProvider or is it "just" a limitation of the heuristic used in the rule?

Simon Lindgren
  • 500
  • 1
  • 6
  • 14

3 Answers3

6

It only applies to methods with an IFormatProvider overload.

To deal with this problem, I have two static classes, InvariantText and CulturedText, that work with strings in the invariant culture and current culture, respectively. For example, I have a Format method in each class. This way, I can do culture-neutral and culture-aware formatting without having to specify an IFormatProvider each time.

Example:

InvariantText.Format("0x{0:X8}",value);

CulturedText.Format("Appending file {0}",file);

InvariantText.Format and CulturedText.Format are simply wrappers to the String.Format method, and accordingly likewise return strings.


You can even use this pattern to wrap other functions that require culture-neutral and culture-specific strings. For example, create two methods, InvariantLog and CulturedLog that wrap calls to Logger.DebugFormat in your question and take the appropriate IFormatProvider in each case.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
  • 1
    Thanks, that's a definitive improvement, however it doesn't work for my logger as the interface is out of my control... And those calls were the ones that annoyed me the most :) Marking this as the correct answer, as it really is the most useful one so far. – Simon Lindgren Aug 09 '11 at 07:09
  • The InvariantText.Format and CulturedText.Format methods return strings. In your example, you can rewrite the logger methods as `Logger.DebugFormat(CulturedText.Format("Appending file {0}", fileName));`. – Peter O. Aug 09 '11 at 07:37
  • That's actually a very nice idea. +1. – Joey Aug 09 '11 at 07:43
  • @Peter, yes, that would be a way. It ends up being roughly the same amount of characters, but it is more readable. – Simon Lindgren Aug 09 '11 at 10:51
2

It depends. You are aware how and where application will be used, so please consider following MSDN recommendations:

  1. If the value will be displayed to the user, use the current culture. See System.Globalization.CultureInfo.CurrentCulture.
  2. If the value will be stored and accessed by software (persisted to a file or database), use the invariant culture. See System.Globalization.CultureInfo.InvariantCulture.
  3. If you do not know the destination of the value, have the data consumer or provider specify the culture.

PS: I believe FxCop follows the third rule and let you specify the right culture yourself.

sll
  • 61,540
  • 22
  • 104
  • 156
1

The rule is not the only reader of your code. If you do not explicitly specify a formatting culture, a maintenance developer will not be able to distinguish between a deliberate fallback to the default formatting culture (CurrentCulture in most cases) or an omission that may lead to incorrect formatting. If you don't like the verbosity, consider using wrapper methods like those proposed by Peter O.

Nicole Calinoiu
  • 20,843
  • 2
  • 44
  • 49