Patrick Smacchia [MVP C#]

Sponsors

The Lounge

Wicked Cool Jobs

News

  • NDepend v3 is fully integrated in Visual Studio, and is now available for download! Software dependencies visualization, 82 .NET software metrics, continuous rule validations, assembly version diff, declarative code queries and more ! http://ndepend.com

Advertisement

On being Culture Aware

Normal 0 21 false false false FR X-NONE X-NONE

A typical development pitfall comes from forgetting about the culture setup of the machine on which the code is executed in production (typical it works on my machine mistake). Imagine that a program running on a French machine stores a float as the string "3,14". The float value cannot be restored from the same program running on a US machine. It would expect the string to be "3.14" with a dot, not a coma.

To prevent such mistake in the NDepend code I created the following CQL rule. Basically, it forces the code base to use only Parse() and ToString() methods that accept a parameter of type IFormatProvider.  As far as I know, the problem occurs on DateTime, float (Single), double and decimal.

 

// <Name>Float and Date Parsing must be culture aware</Name>

WARN IF Count > 0 IN SELECT METHODS

FROM TYPES "System.DateTime",

           "System.Single",

           "System.Double",

           "System.Decimal"

// The NameLike CQL clause operates on the signature

// "methodName(type1,type2...typeN)"

WHERE (NameLike "Parse\(" OR NameLike "ToString\(") AND

      !NameLike "IFormatProvider"

 

With this rule set up, in the future we won’t forget about caring for culture when converting from/to string. Previously, I already wrote a post concerning this Nurture Knowledge Database agile state in mind.

FxCop has a similar rule named Specify IFormatProvider. We are inclined to append this CQL rule to the set of default CQL rules, what is your opinion?

 


Posted Mon, Nov 9 2009 3:26 PM by Patrick Smacchia

[Advertisement]

Comments

Mike O'Brien wrote re: On being Culture Aware
on Mon, Nov 9 2009 1:36 PM

Is NDepend a commercial product or is it free/OSS?

Patrick Smacchia wrote re: On being Culture Aware
on Mon, Nov 9 2009 3:52 PM

Commercial. There is a free version for OSS projects and students.

Mike O'Brien wrote re: On being Culture Aware
on Mon, Nov 9 2009 4:05 PM

So do you own the company that makes it?

Steve Py wrote re: On being Culture Aware
on Mon, Nov 9 2009 4:31 PM

Should probaby add the TryParse and Convert.To* methods that also accept format providers.

As for a default CQL, why not. :)

Patrick Smacchia wrote re: On being Culture Aware
on Mon, Nov 9 2009 4:37 PM

Mike the company SMACCHIA.COM SARL, I am the CEO of the company and lead developper of the product. See the NDepend website if you are interested in the product:

http://www.ndepend.com/

Mike O'Brien wrote re: On being Culture Aware
on Mon, Nov 9 2009 5:39 PM

I noticed most of your blog posts talk about NDepend either directly or indirectly, so I was thinking there was some sort of affiliation.

PimpThisBlog.com wrote On being Culture Aware
on Mon, Nov 9 2009 6:09 PM

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

Travis Illig wrote re: On being Culture Aware
on Tue, Nov 10 2009 10:35 AM

I use both FxCop and NDepend (run in that order) and usually end up disabling the rules that are redundant. If FxCop, for example, is going to fail the build anyway, no need to re-run the rule in NDepend and clutter the report.

Bogdan wrote re: On being Culture Aware
on Wed, Nov 18 2009 11:31 AM

This is a good rule indeed.

I've hit this problem more than once, as it's really easy to forget about it.

Soma buy soma cheap soma soma online. wrote Buy soma watson brand online 150 tablets.
on Sun, Feb 21 2010 9:27 PM

Buy soma watson brand online 150 tablets.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us