Thursday, September 08, 2011

Naming Convention for Code and DB

First let’s understand different types of casing styles.

  • 1)    UpperCase – All letters in uppercase. Example: ISITEMREQUIRED
  • 2)     LowerCase – All letters in lowercase. Example: isitemrequired
  • 3)     CamelCase – first letter in identifier is lowercase and each subsequent concatenated word is capitalized.  Example: isItemRequired
  • 4)     PascalCase – first letter in identifier and each subsequent concatenated word is capitalized.  Example: IsItemRequired

Naming Convention for Coding

Identifier
Casing Style to use
Example
Local variable declarations
Camel casing
userName
Private variables
Camel casing
statusMessage
Property declaration
Pascal casing
ForeColor
Public variables
Pascal casing
ErrorCode
Const, Static or Readonly fields
Pascal casing
IsMembershipRequired
Method Name
Pascal casing
ProcessApplication()
Enum
Pascal casing
MembershipLevels
Class Name
Pascal casing
MemberDetails
Interface
Pascal casing
IDisposable, *Using “I” in front of interface name to avoid confusion between other class, while inheriting.
Events
Pascal casing
SubmitButtonClick, use “Functionality name” + “Event name”
Namespace
Pascal casing
CompanyName.ProjectName

To summarize it easily, anything which is public in nature then use pascal casing.  Avoid using underscore “_” or hyphen “-“ while naming identifier.



Naming Convention for Database

Table name convention.
·         It should be in UpperCase
·         It should not have Spaces
·         Multiple words should be split with Underscore, since some of DB Client always shows DB name in uppercase, using case will not be good choice.
·         It should be Plural (more than one in number) - Example: EMPLOYEES Table, rather than EMPLOYEE.  If it contains multiple words only last word should be plural.  Example: EMPLOYEE_PHOTOS


Field name convention.
·         It should not have Spaces
·         Multiple words should be split with Underscore.
·         It should be Singular - Example: EMPLOYEE_ID column name, rather than EMPLOYEES_ID or EMPLOYEE_IDS.

For Datatype consideration, refer my article.

Procedure name convention
·         Procedure name should be defined as TableName_ProcedureFunctionalityName.  Example: Employees_SelectAll,  Employees_Insert, Employees_Update, Employees_Delete.  If table name is too long, it is also better to use short name of table rather than full tablename prefix, Example: Emp_SelectAll, Emp_Insert.  If table name contains multiple words like Employee_Locations then it is better to give name like EL_SelectAll, EL_Insert.  If short name are getting duplicate, then you can change of one of short name to avoid duplication or confusion.
·         If you are creating procedure which is general in nature or combines 2 or more tables or mainly business logic which cannot be associated with any table, then it is better to use as BusinessLogicName_ProcedureFunctionalityName.  Example:  procedure for employees quarterly sales report should be named something like Reports_Emp_Quaterly_Sales.  That way you can combine all reports procedure together to easily find them in a complex database structure.
·         Remember, naming convention is to help finding things easily and a standard which can be easily explain to anyone joining a development team.  So always name considering this scenario in mind.

Function name convention
·         Function name are mostly generic utilities, but incase if they are associated with table, then follow procedure naming convention approach, else use meaningful name.  Example:  AgeFromDOB  - If you pass a valid date, this function will return age, no. of years between current date and DOB.

Primary Key convention
·         Primary key should be name as PK_TableName.  Example:  PK_Employees.   If you are using SQL Server, whenever you are creating primary key in table designer, it will automatically follows above naming convention.

Foreign Key convention
·         Foreign key should be name as FK_PrimaryTableName_ForeignTableName.  Example:  PK_Employees_Departments.   If you are using SQL Server, whenever you are creating foreign key in table designer, it will automatically follows above naming convention.

Constraint name convention
·         Constraint name should be name as ConstraintShort_ConstraintColumnName.  Example: 
Default value constraint for IsActive column field in employe table should be 1 (or true).  DF_IsActive.  Here DF stands for Default value constraint and IsActive is column field in Employees Table.

Index name convention
·         Index name should be name with prefix idx_ColumnName.  Example: 
Idx_Employee_ID

No comments:

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape