Should I always specify which IFormatProvider to use?
Asked Answered
H

3

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?

Hallowell answered 8/8, 2011 at 9:46 Comment(0)
A
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.

Aq answered 8/8, 2011 at 10:2 Comment(4)
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.Hallowell
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));.Aq
That's actually a very nice idea. +1.Ked
@Peter, yes, that would be a way. It ends up being roughly the same amount of characters, but it is more readable.Hallowell
O
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.

Offcolor answered 8/8, 2011 at 10:0 Comment(0)
D
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.

Depravity answered 8/8, 2011 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.