Hello Joris,
To address a few of your and Chris's questions/comments...
A few examples
- a string variable that holds an SQL statement: sql / strSql / sSql ?
If the variable is class-level then it would be sSql. If the variable is
function-level then it would be tSql.
- a boolean variable: isRequired / bRequired?
As a variable it would be bRequired, or bIsRequired. As a property that
refered to the variable it would be Required, or IsRequired.
I don't think you need any help guessing the datatype for clearly
names variables, like firstName, lastName, birthDate, isMarried,
numberOfChildren, emailAddress, ...
True, hoever.. The point of the prefix is to partially eliminate the need
to guess (think 6 months to a year down the road when you have forgotten
all about this code). Also it is for consistency.
Sometimes you will need some help, for example if you see "sql" as a
variable name, how do you know what type it is (string or
stringbuilder)? The IDE will help you: tooltips in de code editor,
intellisense while typing, ...
In this case it would be oSql for a StringBuilder and sSql for a string.
However I despise using both of those names. oSql doesnt tell me if this
is a StringBuilder, a SqlCommand, or a SqlConnection, etc.. I would probably
choose a name like: oSqlStringBuilder.
I have 2 exceptions to the "no prefix" rule:
- for UI controls, I still use them: txtFirstName, chkIsMarried, ...
because
it makes it easier to distinguish controls from variables and
procedures
I have this habit as well. I've been searching for a way to eliminate this,
but I havent found a good way.. so the habit sticks.
- an underscore charater as a prefix for "member"-variables (private,
class
scope), but no prefix for function arguments and local variables (and
I don't
use global variables).
I absolutely hate the underscore. It makes code very unreadable im my opinion.
as for Chris's comments about the case of the variable names.. I would have
to disagree. I haven't considered the impact to a case-sensitive language
at present, however vb is case-insensitive.. if someone were writing code
in say, notepad or any other editor besides VS.NET, then they could write
cat, Cat, CAT and all mean the same thing..
As others have mentioned, this is mostly personal choice.. just try to remember
that down the road you wont be the only person having to read and modify
this code. Try to choose a convention that others can easily follow as well.
-Bo