• Files Organization
    • Create folder for each used namespace and put project source files according to their namespaces.
    • For best understanding of classes functionality, put each class it's own file and don't exceed 1000 lines of code. If it's necessary, you can declare class as "partial" and divide it's functionality in several files: Use ClassName.cs as a part, that contains private, protected (protected internal) or constant variables, used by this class, ClassName_SomeMethods.cs, ClassName_OtherMethods.cs to determine for what type of methods each class part serves.
  • Comments
    • There is one simple rule: Comment everything using xml documentation style. When there are about 30000 lines of code in different projects, about 2000 methods you realy will not remember what does one method and what other. Dont commit any class or method if it's not documented!!!

docs.gif
  • Naming
    • Use uppercase character on class, struct, public variable names as a first char.
    • Use short, understandable names.
    • Private variables must start with "m_" prefix.
    • Use lowercase character as local variable first char name. If local variable is used for iterations, use i, j, k... names.
    • Method parameters must be started with lowercase character and reflect it's usage.

variables.gif
  • Classes / structs and their members construction
    • Always remember, that struct initalization is much faster then class, use classes only if you can't use structs.
    • If you know that class (struct, method, variable) will be used in current assembly only, mark it as internal
    • If you know that class will not have child classes, mark it as sealed
    • If class contains only static methods, mark it as static
    • If class is abtract or designed to be parent class for other classes, mark all it methods as virtual, and private variables or methods as protected virtual
  • Methods and variables
    • Always take care about integral variables type, for example if variable value can be in range {0,255}, don't make it int, make it byte. More information about sizes and ranges of the integral types may be found in MSDN
    • If you know how, it's prefered to use unsafe code ( carefully :) ).
  • Debugging
    • If you want to debug part of code, use local defines, like #define DEBUG_MY_CLASS, DO NOT use #if DEBUG or #if TRACE in your code. After debugging is complete, leave your local define in source code commented. Example:

dbgdef.gif
  • Misc
    • Don't forget to remove unnecessary using directives. In Visual Studio 2008 you can do it by "View > IntelliSence > Organize Usings > Remove and Sort" or just "right-click > Organize Usings > Remove and Sort"
    • If you are not sure that your method is working correctly or can be optimized, mark it with [System.Obsolate("Your comment goes here")] attribute. It'll help other developers to fix / rewrite your method or confirm that it works good.

Last edited Apr 4, 2008 at 7:50 PM by Silence, version 26

Comments

No comments yet.