Importance of style guidelines
Defining and following the coding style guidelines is not just nice to have, but it is very very important. It doesn’t matter whatever guidelines you choose, but it is crucial to choose something. Why? Because it leads to cleaner, readable and maintainable source code. And maintaining code is a part of development, not just something in the future.
My preferred style guidelines
Here are guidelines which I always use (when it is possible). Most of these guidelines are blessed by creators of C# language, but I did some meaningful changes.
Naming guidelines
Private members
- _lowerCamelCase (with prefix _) and use readonly where possible.Local variables
andmethod parameters
- lowerCamelCasePrivate methods
,properties
andevents
- lowerCamelCaseNon private methods
,properties
,fields
andevents
- UpperCamelCaseConstants
,static readonly fields
- ALL_UPPERInterfaces
- IUpperCamelCaseTypes
andnamespaces
- UpperCamelCaseGeneric types
- TUpperCamelCase- Avoid this. unless absolutely necessary.
- Always specify the visibility (private string _foo)
- Use Allman style braces, where each brace begins on a new line. A single line statement block can go without braces but the block must be properly indented on its own line and it must not be nested in other statement blocks that use braces
Coding guidelines
- Include unit tests when adding new features. When fixing bugs, start with adding a test that highlights how the current behavior is broken.
Order of items in classes
- Constant Fields
- Fields
- Constructors
- Finalizers (Destructors)
- Properties
- Public methods
- Private methods
- Events
- Implementation of interfaces (each in own region)