Good Programming practices With C #
Posted by
Unknown
at
11:59 AM
1. Avoid writing very long methods. A method should typically have 1~25 lines of code. If a method has more than 25 lines of code, you must consider re factoring into separate methods.
2. Method name should tell what it does. Do not use mis-leading names. If the method name is obvious, there is no need of documentation explaining what the method does.
Good:
void SavePhoneNumber ( string phoneNumber )
{
// Save the phone number.
}
Not Good:
// This method will save the phone number.
void SaveDetails ( string phoneNumber )
{
// Save the phone number.
}
3. A method should do only 'one job'. Do not combine more than one job in a single method, even if those jobs are very small.
Good:
// Save the address.
SaveAddress ( address );
// Send an email to the supervisor to inform that the address is updated.
SendEmail ( address, email );
void SaveAddress ( string address )
{
// Save the address.
// ...
}
void SendEmail ( string address, string email )
{
// Send an email to inform the supervisor that the address is changed.
// ...
}
Not Good:
// Save address and send an email to the supervisor to inform that
// the address is updated.
SaveAddress ( address, email );
void SaveAddress ( string address, string email )
{
// Job 1.
// Save the address.
// ...
// Job 2.
// Send an email to inform the supervisor that the address is changed.
// ...
}
4. Use the c# or VB.NET specific types (aliases), rather than the types defined in System namespace.
int age; (not Int16)
string name; (not String)
object contactInfo; (not Object)
Some developers prefer to use types in Common Type System than language specific aliases. |
5. Always watch for unexpected values. For example, if you are using a parameter with 2 possible values, never assume that if one is not matching then the only possibility is the other value.
Good:
If ( memberType == eMemberTypes.Registered )
{
// Registered user… do something…
}
else if ( memberType == eMemberTypes.Guest )
{
// Guest user... do something…
}
else
{
// Un expected user type. Throw an exception
throw new Exception (“Un expected value “ + memberType.ToString() + “’.”)
// If we introduce a new user type in future, we can easily find
// the problem here.
}
Not Good:
If ( memberType == eMemberTypes.Registered )
{
// Registered user… do something…
}
else
{
// Guest user... do something…
// If we introduce another user type in future, this code will
// fail and will not be noticed.
}
6. Do not hardcode numbers. Use constants instead. Declare constant in the top of the file and use it in your code.
However, using constants are also not recommended. You should use the constants in the config file or database so that you can change it later. Declare them as constants only if you are sure this value will never need to be changed.
7. Do not hardcode strings. Use resource files.
8. Convert strings to lowercase or upper case before comparing. This will ensure the string will match even if the string being compared has a different case.
if ( name.ToLower() == “john” )
{
//…
}
9. Use String.Empty instead of “”
Good:
If ( name == String.Empty )
{
// do something
}
Not Good:
If ( name == “” )
{
// do something
}
10. Avoid using member variables. Declare local variables wherever necessary and pass it to other methods instead of sharing a member variable between methods. If you share a member variable between methods, it will be difficult to track which method changed the value and when.
0 comments:
Post a Comment