fxCop
We use FxCop to check wether DelftShell sources follow correct coding standards such as
- correct CapITalizaTION
- exception throwing
- naming
The reports are currently generated for core functionality only.
An fxcop report can be generated manually by running the fxcop project on Teamcity. To inspect the results we can use FxCopGraph and FxCopReport.
Common mistakes
Often I find people use System.Exception instead of a more specific one like ArgumentException or ReadOnlyException etc.
Assemblies are often missing the ClsCompliant attribute and SecurityPermission Attribute.
Example
[assembly: CLSCompliant(true)] //fxcop prerequisite [assembly: SecurityPermission(SecurityAction.RequestMinimum, Execution = true)] //fxcop prerequisite
When using a format statement or a ToString() function, the CultureInfo is usually missing. At least specify CultureInfo.InvariantCulture so the conversion always yields the same result when intended:
String comparison:
- String comparison for equality – Specify StringComparison.Ordinal or OrdinalIgnoreCase. This is true when comparing against string resources, and particularly important when comparing file names.
- String comparison for sorting - Specify StringComparison.CurrentCulture when sorting lists
- String.ToUpper - Use ToUpper rather than ToLower, and specify InvariantCulture in order to pick up OS casing rules
- Determining BiDi languages, for example to load correct icons - Continue to specify CurrentUICulture for these cases.
- String formatting including numbers, and int.ToString - Specify CurrentCulture
- String formatting of strings only (no numbers) - the Culture arg here is a no-op. Use CurrentCulture.
examples:
myInt.ToString(CultureInfo.InvariantCulture) string.Format(CultureInfo.InvariantCulture, "Tomorrow I will be {0} years old", 37)
Links
I found the following links on internet about coding standards while fixing some issues:
document on coding standards
Exception handling
FxCop, locale installatie
Using CultureInfo.InvariantCulture