2010 25 Mar
“The beginning of wisdom is to call things by their right names” – Chinese Proverb
“Meaningful” is the keyword in naming. By meaningful names, I mean concise names that accurately describe the variable, method or object.
In this article of my series software coding standards we'll see how this would be in C#:
Namespaces – Names should be meaningful and complete. Indicate your company or name, product and then your utility. Do not abbreviate.
//Good
namespace CompanyName.ProductName.Utility
//Bad namespace CN.PROD.UTIL
Classes – Class names should always be a noun and, again, should be meaningful. Avoid verbs.
//Good
class Image
{
…
}
class Filters
{
…
}
//Bad
class Act
{
…
}
class Enhance
{
…
}
Methods – Always use a verb-noun pair, unless the method operates on its containing class, in which case, use just a verb.
//Good
public void InitializePath();
public void GetPath();
public void ShowChanges();
public void System.Windows.Forms.Form.Show();
//Bad
public void Path();
public void Changes();
Methods with return values – The name should reflect the return value.
//Good
public int GetImageWidth(Bitmap image);
//Bad
public int GetDimensions(Bitmap image);
Variables – Do not abbreviate variable names. Variable names should again be descriptive and meaningful.
//Good
int customerCount = 0;
int index = 0;
string temp = “”;
//Bad
int cc = 0;
int i = 0;
string t = “”;
Private member variables – Prefix class member variables with m_.
public class Image
{
private int m_initialWidth;
private string m_filename;
…
}
Interfaces – Prefix all interface names with I. Use a name that reflects an interface’s capabilities, either a general noun or an “-able”.
interface IClock
{
DateTime Time { get; set; }
…
}
interface IAlarmClock : IClock
{
void Ring();
DateTime AlarmTime { get; set; }
…
}
interface IDisposable
{
void Dispose();
}
interface IEnumerable
{
IEnumerator GetEnumerator();
}
Custom attributes – Suffix all attribute class names with Attribute. The C# compiler recognizes this and allows you to omit it when using it.
public class IsTestedAttribute : Attribute
{
public override string ToString()
{
return “Is Tested”;
}
}
//”Attribute” suffix can be omitted
[IsTested]
public void Ring();
Custom exceptions – Suffix all custom exception names with Exception.
public class UserNotExistentException :
System.ApplicationException
{
…
}
Delegates – Suffix all event handlers with Handler; suffix everything else with Delegate.
public delegate void ImageChangedHandler();
public delegate string StringMethodDelegate();
Database Object Naming
Database objects should follow the same concept in naming the objects
- Views should have the prefix “vw_”
- Stored Procedures should have the prefix “sp_”
- Triggers should have the prefix “trg_”
Good article. Having worked in several maturing database environments, I would suggest to no longer prefix database objects. You want to be able to change the database later without adjusting the application. We can decide to functionally replace a table with a view or a stored procedure, which the application will access using the old name. I have found no other reason to prefix other than getting things sorted in documentation.



