Chiming in on the C# coding standards.

Lance has posted a nice little tidbit on C# coding standards here.

I pretty much concur with the coding style that Lance has put forth in his coding standards document. I have a possible suggestion about how to deal with constants. Consider creating a static class (could be nested) to contain your constants. Given this “grouping” if you will of constants I would prefer to name the constants using the Pascal case rather than all caps. Here is an example:

public class Constants
{
    // Make this class static.
    private Constants() {}
    public const string AppName = "MyApp";
    ...

}

I'm not suggesting that you put all of your constants in a single class, just simply place them into logical groupings. From the code window usage standpoint they tend to feel more like enums then. P.S. 9 times out of 10 if you are using numeric constants they can be converted to enums.

6 Comments

  • You can use inner clases to add grouping.



    public class Constants

    {

    // Make this class static.

    private Constants() {}

    public const string AppName = "MyApp";

    public class ConstantsGroupA

    {

    // Make this class static.

    private ConstantsGroupA() {}

    public const string Something = "GroupA Something";

    ...

    }





    }



  • After a quick glance, Juval's coding standards look pretty good. I do disagree with a number of his assertions, and differ a bit on style such as "m_" prefix, but the document seems very thorough.



    My goal is to achieve something similar, but purposefully more conservative and mainstream in various areas.



    Thanks for the link!



    Lance

  • I'm gonna be a broken record.

    Those coding standards appear ok, but how do they compare to the FxCop basic rules and is there a set of FxCop rules to check them.



  • So far, they have 100% passed the default FxCop rules. Although, I am still testing this.



    I am working on creating some new rules, but am probably going to wait for MS to publish the IntrospectionRule API since some of the best practices are a bit more complex than RefleactionRules allow.



    I'll post the FxCop rules on my blog when I finish them.



    Lance

  • I am looking into FxCop these days. And after doing a lot of trial and error with the new introspection engine, and when I thought I had finally nailed it, I find this tool. Can anyone tell me how does this tool compare to FxCop ?

    -thanks
    Nick

  • These days in c#, you can declare the class as static - eliminating the need for a private constructor.

Comments have been disabled for this content.