Saturday, October 16, 2010

C# Coding Standards

Purpose:

  • To develop reliable and maintainable applications

Types of Casing:

  • Camel
    (First character of all words, except the first word are Upper Case and other characters are lower case e.g. variableName)
  • Pascal

(First characters of all words are Upper Case and other characters are lower case. E.g. VariableName )

  • Hungarian

(The prefix gives extra information of the variable. E.g. pAccountName, strName etc.)

Naming Convention

1) Variables and Methods

  • Local variables-> Camel Case
  • Private Member Variables->Camel Case beginning with Underscore(‘_’)
  • Public Member Variables-> Pascal
  • Private Method Name-> Pascal
  • Public Method Name-> Pascal
  • Method Parameters-> Camel
  • UI Controls-> Pascal
  • Event Handler->Pascal(suffix with EventHandler)
  • Exception-> Pascal( Suffix with Exception)

2) Namespace, Class & Interface

  • Interface-> Pascal (start with ‘I’)
  • Class Name-> Pascal
  • Namespace->

...

  • File Name-> Pascal(Match with class Name)

Note:

  • Do not use variable names that resemble keywords
  • Do not use single character variable names
  • Do not use Hungarian notation to name variables
  • Do not use underscore(‘_’) for local variable names

Indention & Spacing

1) Use TAB for indention. DON’T use spaces. Use TAB size=4.

2) Comments should be in the same level as the code.

3) Curly brackets ({}) should be in the same level as the code outside the bracket.

4) Curly brackets should be in separate line, not in the same line.

5) Use // or /// comments. Avoid using /*…..*/

6) Use single space before and after operator and braces.

7) Logical groups of code should be separated by a blank space.

8) Use region to group the related pieces of code together.

9) Keep private member variables, properties, methods at the top and public member variables, properties and methods at the bottom.

10) There should be one and only one blank line between methods inside a class.

Good Programming Practices

1) Always use multilayer (N-Tier) architecture.

2) Use relevant function names and variable names.

3) Do not write comments if the code is easily understandable without comments.

4) Write as fewer lines of comments as possible.

5) Always log every exception with detail information. But give friendly message to user.

6) Don’t write very large try-catch blocks.

7) Prevent all possible exceptions, so that we need handle exception.

8) Don’t create more than one class in a file.

9) A method should do only one job, not more than that.

10) Don’t create a very large file; if file is more than 1000 lines of code, then it is necessary to split the file into multiple files.

11) Limit the size of method to 25 lines, if more than that, think of splitting the method.

12) Do not use large objects in session.

13) Always use style sheets to control the look and feel of the pages.


(Prepared for Deerwalk Inc.)

Your comments are welcome.

No comments:

Post a Comment