Wednesday, November 25, 2009

The Best Programming Practices

A type is a content template, it could be a data type or a class or a structure etc. For all the following types:

  1. Classes

  2. Namespaces

  3. Enumerations

  4. Structures

  5. Assemblies

Use the Pascal case naming conventions like CodeGenerator. The name should be noun and not a verb. For example the class for generating code will be called CodeGenerator not CodeGeneration. Class names carry no prefixes like C or c.

Types of classes and their naming conventions

Always follow the fact that a noun will be used for naming any sort of class.

  1. Class: Simple Pascal cases no prefixes.

  2. Collection Class: Pascal cases with the suffix of collection. E.g. ErrorCollection

  3. Delegate Class: Pascal cases with the suffix delegate. E.g. ThreadCallbackDelegate.

  4. Exception Class: Pascal cases with suffix exception. E.g. InvalidTransactionException.

  5. Attribute Class: Pascal case with suffix attribute. E.g. DataSetAttribute.

Naming interfaces

Interface names also should be a noun. And be prefixed with an I. I should be in upper case. And the interface name should be in Pascal case. E.g. ICollections.

Naming of variables and objects

Follow the camel case naming conventions. The object or variable should signify what it does. For example the object of the ErrorCollection class would be objectErrorCollection. For variables in the class use camel case with no prefixes e.g. int recordID.

Naming of functions

Use Pascal case here. This should be a verb describing what the function is doing. Let’s consider a function that is used to add two numbers. The function should be called Add() and not Adder() or Addition().

Naming of properties

The name of the property should be in Pascal case, because the variable names are in camel case. Hence for a variable recordID, the property would be Record ID.

Naming of parameters

Same as naming of variables; use camel case and describe what value the parameter would accept. Like if a parameter accepts the First name of an individual, then the parameter must be string firstName.

Naming of constants

All constants must always follow the all caps rule, if it needs more than one word to describe it split the words using an underscore. Hence, constants would be named as CONNECTION_STRING.

Naming of UI components

Use the standard notation of the data being handled by the control followed by the control name. As usual this being an object we use the camel case notation. So the Text box which accepts the user ID would be named as: userIDTextBox.

Event handlers

Private by default, these handler have to use camel case for giving the handler name. Use and underscore to separate the event from the event object. Parameters must be object sender followed by the object of the EventArgs Class, which should be always e.

Private submitButton_Click(object sender, EventArgs e)

Access specifiers

Use private for all class level variables. Expose this variable through a property if needed. The method which have to be accessed outside the class have to be made public. Other methods can be made private.