Naming Conventions which we need to follow in every language.
Similarly for C# and sql server we have some specifications to follow.
In this article i am explaining the each one with example.
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 Conventions for C# 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 Conventions for Database(sql server)
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.
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 conventions 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 conventions 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 conventions.
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:
Post a Comment