Saturday, July 14, 2012

Minh họa Thuật giải Vương Hạo

1. Benefit of code convention
Code conventions are important to programmers for a number of reasons:
· 40%-80% of the lifetime cost of a piece of software goes to maintenance.
· Hardly any software is maintained for its whole life by the original author.
· Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.
· If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create.

2. Pascal casing and Camel casing
· Pascal casing is a style of writing compound words or phrases with elements or words written together, without spaces or other punctuation and capitalizes the first character of each word.
Example:
ShowDialog
GetPrimeNumber
Etc…
· Camel casing is like Pascal casing but the first character of the first word is written in lower case.
Example:
showDialog
getPrimeNumber
Etc…
3. Naming Conventions
a) Constant and Variable
· Use Pascal casing for constant names:
clip_image001public class DemoClass
clip_image002 {
const int MaxNumber = 10;
}
· Use Camel casing for local variable names and method arguments.
void DemoMethod(int inputNumber)
clip_image003 {
int number;
clip_image004 }
clip_image005
· Prefix private member variables with m_. Use Pascal casing for the rest of a member variable name following the m_.
public class DemoClass
{
private int m_Number;
}
· Use Pascal casing for property names
public class DemoClass
{
public int Number { get; set; }
}
· Avoid single character variables names, such as i and t.Use index and temp instead.
· Do not abbreviate words (such as num instead of number).
· Use meaningful namespaces such as the product name or company name.
· Use a noun, noun phrase, or an adjective for variable names.
· Declare a local variable as close as possible to its first use.
b) Class, Method, Interface
· Using Pascal casing for Class and Method names
public class DemoClass
{
public void GetNumber()
{
}
}
· Prefix interface name with “I” and use Pascal casing for the rest of an interface name following the “I”.
interface IDemoInterface
{
}
· Use a noun, noun phrase for class names because a class is normally representing something in the real world.
public class Home
{
}
· Name methods using verb-object pair. Example: GetPrimes(), SetState().
· Method with return values should have a name describing he value returned. Example:
GetPrimesNumber()
GetPeopleInCar()
4. Summary
Here is some standard for code convention about naming when coding in C# language. I hope you will have a fun time when reading the article :D .
Source:
· Internet
· IDesign (http://www.idesign.net/)

No comments: