c# Naming Conventions recommendation – level 100
Recently my team began the process of revising out coding standards document. We are revisiting this issue because the standards were developed when we used VB 6 and ASP 3.0. I have offered my opinions to the team, and I’ll post them here. Comments are welcome.
All,
To throw in my opinion, I would agree with the below proposed changes with the following exceptions:
All pulic, protected, or internal interfaces should use PascalCasing.
All private members should use camelCasing.
All private class members should be prefixed with “_”
“p_”, “m_”, “g_”, should not be used, and Hungarian notation should not be used.
For instance:
namespace GpdsIT.SomeRandomApp {
public class IDBadge {
private Guid _uniqueID;
private string _badgeName = string.Empty;
public Guid UniqueID {
get { return _uniqueID; }
set { _uniqueID = value; }
}
public string Name {
get { return _badgeName; }
set { _badgeName = value; }
}
public IDBadge(string badgeName) {
_uniqueID = Guid.NewGuid();
_badgeName = badgeName;
}
}
}
I’ll refer you to the MSDN Design Guidelines for Class Library Developers at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconNETFrameworkDesignGuidelines.asp.
The Naming Guidelines link has naming convention recommendations that support the above rules, and the other links have very valuable information about .Net best practices.
And FxCop will warn you when you violate the naming guidelines, which is the only way to realistically enforce them.
IMHO, you should explain the difference between:
All private members…
All private class members…
I would identify the first one as:
All local variables or parameters
I would identify the second one as:
All private type members
my .02
why are you guys wasting your time with such nonsense. Just follow the MSDN Design Guidelines.
I found this (devAdvantage) which I like much better than FXCop. You can easily adjust the rules to do what you want so it isn’t as annoying. the only downside is that it does not handle spelling yet. the really cool thing though is that it can do the renaming for you. definitely check it out.
http://www.anticipatingminds.com/Content/Products/devAdvantage/devAdvantage.aspx
The only argument I ever heard against
indicating the private variable type
in the variable name prefix, is,
what if you have to change from a bool
to an int. Well, all you are doing is
changing Is to Num instead of changing b to i.
You are still re-naming every occurrence of
the variable, unless your new int variable
still logically deserves Is as the prefix.
Then you have strings, 45 different prefixes
whereas you used to look at sz and know it
was a string. None of the dupes of MSDN
who are against type notation are even
willing to admit the only valid reason,
which is, because they say so. Why should
Saddam Husseins picture be in my office?
Because they say so.