Variable Naming Conventions
-
Monday, August 24, 2009 9:56 PMI am looking for a good resource on variable naming conventions to illustrate variable type and where variables are declared. So I will have public variables, Private variables, private or local variables. I also may want to declare variables with the same name in different class code (i.e. in the code behind different forms). I am assuming good coding would dicatate a prefix for declaration location.
Any ideas?
Thanks
EM
All Replies
-
Monday, August 24, 2009 10:42 PMHello ExcelMonkey,
I hope I got your question correctly as I inferred that you want to know about "Scope and Accessibility ". This is a quick into to each type:
Scope and Accessibility with VB.NET
Working with .NET access modifiers
This is also a useful topic about scope:
Scope with VB.NET
Hope it helps
have a lovely day E.M. :-)
Waleed El-Badry ,Teaching Assistant, Faculty of Engineering , Misr University for Science & Technology -
Tuesday, August 25, 2009 12:59 AMHi EM
Ther are many schools out there, some long-standing and venerable and others that claim to be more up with the times. Honestly, just do a Google search for ".NET variable naming conventions"...
http://www.google.com/search?hl=en&source=hp&q=.net+variable+naming+conventions&aq=1&oq=.NET+variable+&aqi=g8
...and find what seems best to you.
My style -- I do not use prefixes (hungarian) because VS's intellisense and the IDE in general make it so easy to know what types you are working with as well as their scope. I name a variable so that it clearly explains its purpose, but sometimes I'll name components with prefixes juts as I do controls. As for plain, Value-Type variables though I find hungarian prefixes very distracting. Just MHO. As for naming things the same between classes - I don't see the problem and I don't see why they would need to be made distinct... the fact that it is in a different class should be good enough. But I'd like to hear a good argument to the counter.
When you do choose a style please post back. I, and I believe others here, would like to know what factors led you to your eventual conclusion. -
Thursday, August 27, 2009 12:48 AM
ExcelMonkey,
If you haven't seen them already here are the .NET 1.1 Naming Guidelines
http://msdn.microsoft.com/en-us/library/xzf533w0(VS.71).aspx
Another reference is the internal coding guidelines Microsoft uses (some are C# specific)
http://blogs.msdn.com/brada/articles/361363.aspx
In short though, prefixes are generally discouraged though I usually prefix class fields with an _ to distinguish them from the Properties that wrap them. VB is a case-insensitive langauge so there isn't a choice on whether to prefix or suffix - you need to use one or the other and rather than only prefix those members which have properties I just prefix them all. Also, in VB10 this _ prefix is used internally by the compiler for auto-implemented properties.
There is no need to use prefixes for declaration location, accessibility, or type - all this information is apparent from usage, qualification, IntelliSense, and conventions, e.g. to access Form2.Name you have to say "Form2" there is no need to name it "Form2.frm2Name", names are typically strings, not numbers or dates, camelCased variables are typically local variables or method parameters and class variables aren't usually public (or even non-private in most cases).
Anthony D. Green Solution Architect IRC: ##vb.net on FreeNode- Proposed As Answer by John Anthony Oliver Thursday, August 27, 2009 1:02 AM
- Marked As Answer by Martin Xie - MSFT Monday, August 31, 2009 10:43 AM
-
Thursday, August 27, 2009 1:13 AMHi EM,
I have proposed the above as the answer as the other blue links in the pages that Anthony D. Green
points to, deal with the naming conventions in all the other areas as far as I am aware.
For the most part if you are only starting with one FORM programs - applications then variables
within SUBs and FUNCTIONs will be declared starting wih DIM for the most part, any "at the top" or outside
of your Subs and Functions will be best left as PRIVATE ( or DIM ).
DIM defaults to PRIVATE at CLASS level anyway. << I think I have mentioned it to you recently?
When you start coding with more than one FORM or other CLASSes, then start to look into learning about PUBLIC
and SHARED and setting up PROPERTY declarations.
:-)
Classes can use the same PROPERTY name such as TEXT as you have a TEXT
property for the following ( and others ); BUTTON , LABEL , TEXTBOX , RICHTEXTBOX
Regards,
John- Edited by John Anthony Oliver Thursday, August 27, 2009 1:18 AM

