Tuesday, May 8, 2007

coding standards

having standards makes it much easier to read code you didn’t write, so i do as much as i can to encourage adherence to the list here.

Definitions:
  • “UpperCamelCased” = capitalize first letter of each keyword (.NET style); do not use underscores)
  • “lowerCamelCased” = captialize first letter of each keyword except the first (java-style); do not user underscores
  • “Hungarian Notated” = prefix variable name with indicator of its type (i.e. string sMyString or strMyString)

C#: (in general, follow Microsoft .NET standards)

Functions

  • Public functions “UpperCamelCased”
  • Private functions “lowerCamelCased”
  • Parameters “UpperCamelCased”

Variables

  • Module-level variables begin “m_”
  • Local function variables either:
    o “lowerCamelCased”
    o Keywords separated with “_”
    o Prefixed with “tmp_”
    o “Hungarian Notated”

Enums

  • “UpperCamelCased”
  • Type name pluralized (i.e. “MyItems”, not “MyItem”)

Constants

  • “UPPER_CASE” (all capitalized; keywords separated with “_”)

For example:

public class MyClass
{

public const THIS_IS_MY_CONSTANT = 0;

public enum MyItems
{

None = 0
,SomeItem = 1
,AnotherItem = 2

}

private string m_MyVariable;

public void TellMeEverything(string Input1)
{

bool localVar = false;
bool local_var = false;
bool tmp_LocalVar = false;
bool bLocalVar = false;

}

private void tellYouNothing(string Input1)
{

}

}


Database Tables

  • Table names “UpperCamelCased_WithAnyExtras” (capitalize first letter of each key word and use a single “_” character as desired)
  • Column names “UpperCamelCased”
  • T-SQL keywords “UPPERCASE” (all capitalized)
  • T-SQL Datatypes “alllowercase” (do not capitalize any character)

For example:

CREATE TABLE Groups
(
GroupId int
,GroupName varchar(50)
)

Database Sprocs

  • Sproc names “UpperCamelCased”
  • Separate input parameters or returned columns with comma on line of subsequent parameter or column name
  • Clearly separate SELECT/FROM/WHERE or UPDATE/SET/WHERE clauses using carriage returns or tabs

For example:

CREATE PROCEDURE UpdateUser

@UserId int
,@StatusId int
,@
UpdateByUserId int


AS

UPDATE Users

SET StatusId = @StatusId
,ChangedBy = @UpdateByUserId

WHERE UserId = @UserId

GO


No comments: