Coding Style
Posted: 21 Jan 2013, 01:17
Style is an important part of the code. The coherent style:
- creates a consisten look to the code, so that readers can focus on content, not layout,
- enables readers to comprehend the code more quickly by making assumptions based on previous experience,
- facilitates copying, modyfing, and maintaining the code.
Layout Conventions
Commenting Conventions
Also, remember:
* Don't comment the obvious.
* Always try to write the code the way it is self-explanatory, so that comments are unneeded.
* Use Visual Studio auto formatting often. Shortcut: Ctrl + E,D
* When you finish, clean up using statements: right click on the code and select: "Organize Usings"->"Remove and Sort".
Although you are not strictly required to stick to the above conventions, you should follow them, as it will lead to the creation of a more consistent and readable code.
- creates a consisten look to the code, so that readers can focus on content, not layout,
- enables readers to comprehend the code more quickly by making assumptions based on previous experience,
- facilitates copying, modyfing, and maintaining the code.
Layout Conventions
- Write only one statement per line.
- Code: Select all
if (p == null)
{
Help(p);
return;
}- Code: Select all
if (who.invisible)
return;- Code: Select all
if (p == null) { Help(p); return; }
- Write only one declaration per line.
Example:
Right- Code: Select all
int x;
int length;
int height;
Wrong- Code: Select all
int x, length, height;
- Leave one blank line between methods bodies.
- Don't use more than one blank line withing the method body.
Example:
Right- Code: Select all
bool SendSomeMessage()
{
counter++;
Player.SendMessage(p, "Some message.");
return true;
}
Right- Code: Select all
bool SendSomeMessage()
{
counter++;
Player.SendMessage(p, "Some message.");
return true;
}
Wrong- Code: Select all
bool SendSomeMessage()
{
counter++;
Player.SendMessage(p, "msg");
return true;
}
Example:
Right
Also right
Wrong
Commenting Conventions
- Place the comment on a separate line, not at the end of a line of code.
- Begin comment text with an uppercase letter.
- End comment text with a period.
- Insert one space between the comment delimiter (//) and the comment text.
Example:
Right- Code: Select all
// The following code works only for MySQL database.
Also, remember:
* Don't comment the obvious.
* Always try to write the code the way it is self-explanatory, so that comments are unneeded.
* Use Visual Studio auto formatting often. Shortcut: Ctrl + E,D
* When you finish, clean up using statements: right click on the code and select: "Organize Usings"->"Remove and Sort".
Although you are not strictly required to stick to the above conventions, you should follow them, as it will lead to the creation of a more consistent and readable code.