Sunday, September 6, 2009

Coding convention – Database and files

In the last blog, we stopped at the naming convention for C# code. However, it is not only C# source that forms an application. We need to standardize naming convention for database objects, files and folders etc. We will focus on them in this blog.

Naming convention for database

The table below is a chart with the standards to be followed for database objects. As in the previous blog for source code, we have three columns here. The first column has the database unit, the second has the details about which case to be used (and special conditions, if any) and last column has an example to illustrate.

Object
      Convention
Database
  • Use Pascal case
  • No abbreviation
  • Format:
    {CompanyName}[_Scope][_Project]

    Example(s):
    MyCompany_Application_Main
Table
  • Use Pascal case
  • Use plural name (for junction tables, concatenate name in one to many relationship order)
  • No abbreviation or underscore
  • No prefix or suffix (avoid Hungarian notation)

    Example(s):
    Users
    UserRoles

Column
  • Use Pascal case
  • No abbreviation or underscore
  • Primary key field should be named as singular form of table name + "ID" (e.g. UserID for table Users)
  • Boolean fields should have prefix as "Is", "Has" etc
  • DateTime fields should have "Date" or "Time" or both as suffix

    Example(s):
    UserID
    FirstName
    IsActive
    JoiningDate

Index
  • Use Pascal case
  • Format:
    {TableName}_{ColumnName}_{U/N}{C/N}
    (U/N for unique or not and C/N for clustered or not)
  • No prefix or suffix (avoid Hungarian notation)

    Example(s):
    TaskDetails_IsCurrent_NN

Constraint
  • Use Pascal case
  • Format:
    {Type}{TableName}_{ColumnName}
    (Type: Pk for Primary key, Fk for Foreign key, Ck for Check and Un for Unique)

    Example(s):
    PkUsers_UserId

View
  • Use Camel case
  • No abbreviation or underscore
  • Prefix "vw"

    Example(s):
    vwUserRoles

Stored Procedure
  • Use Camel case
  • No abbreviation or underscore
  • Prefix "sp"

    Example(s):
    spDeactivateOldEmployees

Trigger
  • Use Pascal  case
  • Format:
    {TableName}[_Description]_{Type}
    (Type: U for Update, D for Delete and I for Insert)

    Example(s):
    Users_U
    Salary_CalculateTotal_U

Variable
  • Use Pascal case
  • No abbreviation or underscore
  • For boolean type, use prefix as "Is", "Has" etc.

    Example(s):
    @MaximumAge


Let us look at naming files and folders now.

Naming convention for files and folders

Files and folders should be named uniformly throughout the application. This is very important because name can identify the type, purpose, scope and validity of a file in an application. The table below presents the standards.

Type
      Convention
Folder
  • Use Pascal case
  • No abbreviation (except for system standards)
  • Use underscore sparingly (avoid as much as possible)
  • No prefix or suffix (avoid Hungarian notation)

    Example(s):
    Content
    App_Code

Code (Source) File
  • Use Pascal case
  • Use the same name as the main class
  • No abbreviation or underscore
  • No prefix or suffix (avoid Hungarian notation)

    Example(s):
    Users.aspx.cs
    CalculateInvoice.cs

Backup File
  • Format:
    {Date}_{Initials}_{FileName}
    (Date in format yyyyMMdd and Initials of the file creator)

    Example(s):
    20090905_NY_CalculateInvoice.cs

Log File
  • Format:
    {Date[Time]}{Description}.log.{Extension}
    (Date[Time] can be in format: yyyy[MM[dd[HH[mm[ss]]]]])

    Example(s):
    200905MonthlyImport.log.xls
    20090905142530System.log.txt

DLL Assembly
  • Use Pascal case
  • Use the same name as the containing namespace

    Example(s):
    SecurityTools.dll


We will also discuss naming convention for style tags and classes soon. However, a quick mention about HTML code is relevant here. In HTML, use XML standards to ensure compatibility with newer versions of HTML and XHTML. Primarily, use Pascal case for all elements and Camel case for their attributes, unless required otherwise by the environment.

0 comments: