- •C# Coding Standards and Best Programming Practices
- •1. Author
- •2. License, Copyrights and Disclaimer
- •3. Revision History
- •4. Introduction
- •5. Purpose of coding standards and best practices
- •6. How to follow the standards across the team
- •7. Naming Conventions and Standards
- •8. Indentation and Spacing
6. How to follow the standards across the team
If you have a team of different skills and tastes, you are going to have a tough time convincing everyone to follow the same standards. The best approach is to have a team meeting and developing your own standards document. You may use this document as a template to prepare your own document.
Distribute a copy of this document (or your own coding standard document) well ahead of the coding standards meeting. All members should come to the meeting prepared to discuss pros and cons of the various points in the document. Make sure you have a manager present in the meeting to resolve conflicts.
Discuss all points in the document. Everyone may have a different opinion about each point, but at the end of the discussion, all members must agree upon the standard you are going to follow. Prepare a new standards document with appropriate changes based on the suggestions from all of the team members. Print copies of it and post it in all workstations.
After you start the development, you must schedule code review meetings to ensure that everyone is following the rules. 3 types of code reviews are recommended:
6.1. Peer review – another team member review the code to ensure that the code follows the coding standards and meets requirements. This level of review can include some unit testing also. Every file in the project must go through this process.
6.2. Architect review – the architect of the team must review the core modules of the project to ensure that they adhere to the design and there is no “big” mistakes that can affect the project in the long run.
6.3. Group review – randomly select one or more files and conduct a group review once in a week. Distribute a printed copy of the files to all team members 30 minutes before the meeting. Let them read and come up with points for discussion. In the group review meeting, use a projector to display the file content in the screen. Go through every sections of the code and let every member give their suggestions on how could that piece of code can be written in a better way. (Don’t forget to appreciate the developer for the good work and also make sure he does not get offended by the “group attack”!)
7. Naming Conventions and Standards
Note : The terms Pascal Casing and Camel Casing are used throughout this document. Pascal Casing - First character of all words are Upper Case and other characters are lower case. Example: BackColor Camel Casing - First character of all words, except the first word are Upper Case and other characters are lower case. Example: backColor |
7.1. Use Pascal casing for Class names
public class СHelloWorld
{
...
}
7.2. Use Pascal casing for Method names
void SayHello(string name)
{
...
}
7.3. Use Camel casing for variables and method parameters
int totalCount = 0;
void SayHello(string name)
{
string fullMessage = "Hello " + name;
...
}
7.4. Use the prefix “I” with Camel Casing for interfaces ( example: IEntity ). Use the prefix “C” with Camel Casing for classes ( example: CHelloClass )
Исключение из этого правила: класс Engine
7.5. Use Hungarian notation to name variables. All variables should use camel casing.
Table 7.1.
Type |
Prefix |
bool |
is |
byte |
byt |
sbyte |
sbt |
char |
chr |
short |
sht |
ushort |
ush |
int |
int |
uint |
uit |
long |
lng |
ulong |
ulg |
float |
flo |
double |
dbl |
decimal |
dcm |
object |
obj |
string |
str |
enum |
en |
Date |
dat |
Array |
arr |
Example: local variable - strName
class member variable - mvStrName / mvObject / private ColCountries mvCountries - Type by self defined collections not using
module variable - modStrName
object variable - objName
7.6. Use Meaningful, descriptive words to name variables. Do not use abbreviations.
Good:
string strAddress
int intSalary
Not Good:
string nam
string addr
int sal
7.7. Do not use single character variable names like i, n, s etc. Use names like index, temp
One exception in this case would be variables used for iterations in loops:
for ( int i = 0; i < count; i++ )
{
...
}
If the variable is used only as a counter for iteration and is not used anywhere else in the loop, many people still like to use a single char variable (i) instead of inventing a different suitable name.
Note: you can to use the rule - to designate objective variables at viewing collections in in cycle for each with the prefix х.(example xObject.)
7.8. Do not use underscores (_) for local variable names.
7.9. All member variables must be prefixed with mv (member variant) so that they can be identified from other local variables.
7.10. Do not use variable names that resemble (ключевых, зарезервированных) keywords.
7.11. Prefix boolean variables, properties and methods with “is” or similar prefixes.
private bool isFinished
private bool mvIsFinished // for member variant
7.12. Namespace names should follow the standard pattern
<company name>.<product name>.<top level module>.<bottom level module>
Example: LizenzaDevelopment.ADAICA.adADR
7.13. Use appropriate prefix for the UI elements so that you can identify them from the rest of the variables.
Use appropriate prefix for each of the ui element. A brief list is given below. Since .NET has given several
controls, you may have to arrive at a complete list of standard prefixes for each of the controls (including third
arty controls) you are using.
Table 7.2
Control |
Prefix |
Label |
lbl |
TextBox |
txt |
DataGrid |
dtg |
Button |
btn |
ImageButton |
imb |
Hyperlink |
hlk |
DropDownList |
ddl |
ListBox |
lst |
DataList |
dtl |
Repeater |
rep |
Checkbox |
chk |
CheckBoxList |
cbl |
RadioButton |
rdo |
RadioButtonList |
rbl |
Image |
img |
Panel |
pnl |
PlaceHolder |
phd |
Table |
tbl |
Validators |
val |
ListView |
lvw |
TreeView |
tvw |
Menu |
mnu |
7.14. File name should match with class name.
For example, for the class HelloWorld, the file name should be HelloWorld.cs
7.15. Use Pascal Case for file names.
7.16. To use the table 7.3 for last element codes (priority behind the set forth above rules).
Table 7.3.
|
|
|
must be |
example |
Application |
|
|
ADAICA == Advanced Documents, Assets & Information Control Application |
|
DB-User |
|
|
ASOADAC == Advanced Service Orientated Architecture (SOA) Data Access Components (DAC) |
|
DB-Objects – it was prefix AS, better should AD, because AS is a reserved word, we take also the prefix AD_ |
||||
|
PL/SQL-Routine (Package, stored procedure) |
|
prefix AD_ |
AD_OBJECT |
|
Package procedure/function |
|
prefix object name (can be short) |
AD_OBJECT.OBJECT_INSERT |
|
View |
|
prefix AV_ and may be any group prefix Example AV_EL_---- |
AV_OBJECT, AV_EL_SWITCH |
|
Data table |
|
thematic prefix (3 chars) |
|
|
exemples for thematic prefixes |
SICAD-Tables |
A_-alpha data, UT_ - table for all object, E_ - power, G - gas, W - water |
A_ADDRESS, UT_OBJECT, E_PART, G_ARMATURE, W_TANK |
|
|
our tables |
VUO_-objects, VUD_-documents, VUM_-maintenance, VUB_-basic, VUS_-services |
VUO_MAST, VUD_DOCUMENT, VUB_CLASS_TYPE, VUS_EXC_SEND |
|
Configuration Table |
|
prefix CONF_thematic prefix |
CONF_VUB_MASK_LABEL |
Oracle programm code |
||||
|
variable |
|
|
|
|
|
global |
prefix gl_ |
gl_name |
|
|
module |
prefix mod_ |
mod_name |
|
|
local |
prefix v_ |
v_name |
|
constant |
|
|
|
|
|
global |
prefix GC_, to shift |
GC_NAME |
|
|
module |
prefix MC_, to_shift |
MC_NAME |
|
|
local |
prefix C_, to shift |
C_NAME |
|
parameter |
|
|
|
|
|
input |
Prefix in |
name |
|
|
output |
prefix out |
out_name |
|
|
input/output |
prefix uni |
uni_name |
Namespace |
|
|
prefix ad |
adObject |
Project |
|
|
|
|
|
Project User Control (Control library) |
|
prefix ctr, camel Case |
ctrObject.ctl |
|
Project DLL(Class library)-Assembly |
|
prefix AD, to shift, 5 max 9 letters |
ADOBJ.dll |
|
Project EXE(Windows application) |
|
prefix AD, Ungarn Case |
ADObject.exe |
|
|
|
|
|
.NET-Objects |
|
|
|
|
|
User defined |
|
|
|
|
|
User Control |
prefix ad, camel Case |
adObject |
|
|
Class |
Prefix C, Ungarn Case |
CObject |
|
|
Collection |
prefix col, singular, camel Case |
colObject |
|
|
Form |
prefix win, camel Case |
winObject |
|
|
Module |
prefix mod, camel Case |
modObject |
|
|
|
|
|
|
.NET defined |
|
|
|
|
|
Tab |
prefix tab, camel Case |
tabObject |
|
|
Picturebox |
prefix pbx, camel Case |
pbxObject |
|
|
Combobox |
prefix cmb, camel Case |
cmbObject |
|
|
Another Controls (for example split container) |
prefix ada, camel Case |
adaObject |
|
|
|
|
|
.NET Programm code |
|
|
|
|
|
Procedure/Function |
|
|
|
|
Prefixes for names of variables |
|
str, int, lng, dbl, bln, all user defined classes must with prefix obj (Obj) |
|
|
Variable |
|
|
|
|
|
global |
prefix glDatatype (initcap) |
glStrName / glObjName |
|
|
module |
prefix modDatatype (initcap) |
modStrName / modObjName |
|
|
local |
prefix datatype |
strName / objName |
|
Parameter |
|
|
|
|
|
input |
prefix Datatype, Camel case |
strName / objName |
|
|
output |
prefix outDatatype |
outStrName / outObjName |
|
|
Input / output |
prefix uniDatatype |
uniStrName / uniObjName |
|
Constant |
|
|
|
|
|
global |
prefix cGlDatatype, Name to shift |
cGlStrNAME |
|
|
module |
prefix cModDatatype, to_shift |
cModStrNAME |
|
|
local |
prefix cDatatype, to shift |
cStrNAME |
|
|
|
|
|
|
Enum |
|
prefix en |
enName |
|
Event |
|
prefix Ev |
EvReadyBySave |