Personal Coding Standard

by Bobby Cannon July 07, 2010 09:12 PM

So I wanted to share with the world my personal coding standard. This is the coding standard I use everyday when I develop software.

Naming

How you name your members are very important. If helps you parse the code in our mind when reading the code. If I’m reading a bit of code and I run across a variable named “_userCount” I immediately know this is a class field. If I am reading code and I see a variable like “WaitingRoom.UserCount” I know it’s not a field because all fields are prefixed with an underscore character.

  1. Never abbreviate, never.
  2. Prefix private fields with the underscore character (_). This will be the ONLY time you will use the underscore character when naming.
    1. private int _userCount;
    2. private string _sqlConnectionString;
  3. Never use the underscore character for namespace, class, method, enum, enum value, event, read-only, const, interface and property names.
  4. Use Pascal casing for namespace, class, method, enum, enum value, event, read-only, const, interface and property names.
  5. Use Pascal casing for abbreviation like HTML should be Html. Ex. XML should be Xml.
    1. The only exception is two character abbreviations like “IO” and they should always be capitalized.
  6. Use camel casing for local variables and method arguments. Ex. userCount, phoneNumber
  7. Prefix component / control variables with the class name. Ex. LabelName, TextBoxAddress
  8. Name interfaces with the “I” prefix. Ex. IConfigurable, IExtendable
  9. Suffix exception classes with “Exception”. Ex. InputInvalidException, TypeNotFoundException
  10. Use descriptive naming.
    1. Avoid one character variable names, such as i or t. Use index or temp instead.
    2. Simple “for” loops are the only exception to one letter variable names.
    3. Never use Hungarian notation for public or protected members. Ex. strName, intNumUsers
    4. Do not abbreviate variables with names like num instead of number. See Naming item #1.
  11. Avoid fully qualified type names. Use the “using” statement instead.

Style

Some would call this formatting but it’s essential that you develop a style (format) of coding. This will greatly assist in the reading / parsing of code. There are many of the items below that are personal preferences and are not “the” way of coding. However once you have develop your style be very consistent in using it when you code.

  1. Maintain strict indentation. Always use “TAB” for indentation and never use spaces.
  2. All comments should pass spell checking. Misspelled comments indicate sloppy development.
  3. All comments need to be grammatically correct. Use punctuation to help with readability.
  4. All methods and properties should be separated by only one blank line.
  5. Always use curly brace scope for “if”, “for”, etc… statements, even if it’s a single statement.
  6. Always place an open curly brace “{“ on a new line.
  7. Always have one new line after an if, for, foreach, etc block statement.
  8. Always have one new line after the break statement in a switch block.
  9. Always have one new line before and after the beginning and ending region statements.
  10. Avoid multiple classes in one file. Always put classes, structures, enumerations in their own file.
  11. Always instantiate fields inside the constructor. Never directly initialize fields unless it is required like read-only / constant fields.
  12. Always use the C# auto property if possible.

Design

Following the following will prevent you from falling into problems that can be easily avoided. Issues like having to convert a field into a property then having to recompile code that used the field. This may not seem like a big deal unless you are releasing an assembly but you should always be conscience of your design even if it’s only use by yourself.

  1. Always use properties instead of accessible data members. Chapter 1 Item 1 of Effective C#.
  2. Always use readonly instead of const unless absolutely required or it just makes since.
    1. public const int DaysOfTheYear = 365; Using a constant makes since being that the days of the year will never change.
    2. public readonly int ReadTimeout = 10; Readonly is required because it’s possible that you may want to change the read timeout in the future.

File Structure

All files should contain the following regions. Each region may contain child regions but these should be the only root regions.

  • References (using statements)
  • Fields
  • Constructors (and de-constructors)
  • Properties
  • Methods
  • Events
  • Delegates

Tools

  • ReSharper – A must have for any C# developer.
  • Visual Studio 2010 Extensions
    • Spell Checker – spell checks comments.
    • Triple Click – triple click to select the whole line.
    • Ankhsvn - Open source SVN source control provider.

More to come…

Tags:

Programming

Add comment




biuquote
  • Comment
  • Preview
Loading



Month List