473,609 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.NET Naming Guidelines (C#)

Can anyone guide me what is wrong with the naming conventions
suggested below? I know that this is not recommended by many
(including Microsoft) but I need to know what exactly is the rationale
behind going for a different convention in .NET. The recommendations
do make sense (to me, at least but I may be wrong and would like
someone to correct me).

Scope Prefixes
Member Variable Prefix: Most of the people still use (and like the
idea of) prefixing member variables. Selection of prefix still remains
debatable. A lot of people use underscore to declare a member
variable. Use "m" as it also matches the convention generally followed
in VC++. Use of "this." in the code everywhere where a member variable
is used is clearly an overhead and there have always been a high
percentage of developers who tend to omit this causing standard non-
compliance. Hence this is not recommended.

Parameters Prefix: Length of a vast majority of functions in code
could easily go beyond 70 lines. Normal editors, viewers and IDE
support a maximum of 35 lines viewable at one time (at a resolution
1024 x 768). As one scrolls through lines of code (e.g. when reviewing
the code), it is important to readily identify if the parameter is a
local variable or is being passed in the function from outside. Use a
parameter prefix (e.g. "r" for inputs, "p" for out, ref).
It is also important to note that developers do not ALWAYS use Visual
Studio IDE to view / modify code hence IntelliSense cannot be taken
for granted. Examples where the code is viewed / modified outside IDE
is checking code file differences with Visual Source Safe (or other
such tools), doing code reviews, checking code files on deployment
machines and modifying ASP.NET code-behind files on deployment
machines to provide quick fixes.

Hungarian Notation / Type Prefix: This is now regarded as a bad
practice by Microsoft and many others. The full Hungarian Notation
(with every type having a unique type prefix) is impractical anyway.
However "Light" Hungarian, i.e. the use of Hungarian Notation only for
primitive types (i.e. a limited list; not for all types) still helps
to identify if a particular variable is being used appropriately (by
identifying its data type from prefix) and to check boxing / un-boxing
of variables without going back & forth between variable declaration &
usage.

Constants (not readonly fields) should be all uppercase with
underscore as separator (matches the convention for VC++ and Win32).

Feb 27 '07 #1
8 2286
>As one scrolls through lines of code (e.g. when reviewing
the code), it is important to readily identify if the parameter is a
local variable or is being passed in the function from outside.
It is? I can't say that it matters that often to me (unless it's an
output parameter).

>Use a parameter prefix (e.g. "r" for inputs, "p" for out, ref).
That's fine for private methods, but I wouldn't use it for publicly
accessible ones. Keep in mind that parameter names becomes part of
your public "interface" . Forcing your own naming conventions onto
consumers of your library isn't very nice.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 27 '07 #2
<mr***********@ hotmail.comwrot e:
Can anyone guide me what is wrong with the naming conventions
suggested below? I know that this is not recommended by many
(including Microsoft) but I need to know what exactly is the rationale
behind going for a different convention in .NET. The recommendations
do make sense (to me, at least but I may be wrong and would like
someone to correct me).

Scope Prefixes
Member Variable Prefix: Most of the people still use (and like the
idea of) prefixing member variables. Selection of prefix still remains
debatable. A lot of people use underscore to declare a member
variable. Use "m" as it also matches the convention generally followed
in VC++. Use of "this." in the code everywhere where a member variable
is used is clearly an overhead and there have always been a high
percentage of developers who tend to omit this causing standard non-
compliance. Hence this is not recommended.
I don't tend to care too much what people do with private variables -
it doesn't affect me.
Parameters Prefix: Length of a vast majority of functions in code
could easily go beyond 70 lines.
Not in my code. Any methods I have over a screenful will be refactored
where possible.

<snip>
Hungarian Notation / Type Prefix: This is now regarded as a bad
practice by Microsoft and many others. The full Hungarian Notation
(with every type having a unique type prefix) is impractical anyway.
However "Light" Hungarian, i.e. the use of Hungarian Notation only for
primitive types (i.e. a limited list; not for all types) still helps
to identify if a particular variable is being used appropriately (by
identifying its data type from prefix) and to check boxing / un-boxing
of variables without going back & forth between variable declaration &
usage.
Makes the code less "fluent" when reading it "aloud" (even mentally).
Constants (not readonly fields) should be all uppercase with
underscore as separator (matches the convention for VC++ and Win32).
Not nearly as easy to read as Pascal case.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 27 '07 #3
On Feb 27, 11:48 am, Mattias Sjögren <mattias.dont.w ant.s...@mvps.o rg>
wrote:
Thanks Mattias; would you like to comment on the other conventions
suggested (specially the one for member variables)?
That's fine for private methods, but I wouldn't use it for publicly
accessible ones. Keep in mind that parameter names becomes part of
your public "interface" . Forcing your own naming conventions onto
consumers of your library isn't very nice.
That's a very good point. However, with .NET, you are bound to
use .NET FCL. Does this mean that you should always follow the
conventions followed in the FCL? (I don't think otherwise there would
have been a universal coding conventions document for everyone working
with .NET Framework).

Feb 27 '07 #4
>Thanks Mattias; would you like to comment on the other conventions
>suggested (specially the one for member variables)?
Well I didn't intend to, since (just like Jon wrote) I don't really
care how people name their private members, as long as they are
consistent. Personally I use a _ prefix for fields, but I'm not saying
that's more correct than any of the other styles.

I've never really missed using Hungarian notation in C#. I don't agree
with your argument that it would help ensure proper use. I think the
compiler does a pretty good job with that anyway.

You also wrote

"Use of 'this.' in the code everywhere where a member variable
is used is clearly an overhead"

I hope you mean typing overhead when writing the code, because there's
certainly no runtime overhead if you chose to use 'this'.

>That's a very good point. However, with .NET, you are bound to
use .NET FCL. Does this mean that you should always follow the
conventions followed in the FCL? (I don't think otherwise there would
have been a universal coding conventions document for everyone working
with .NET Framework).
The .NET Framework design guidelines is as close as you get to a
"universal coding convention" to use for publicly accessible members.
But since many of the BCL classes were coded before or during these
guidelines were written, there are certainly violations in the BCL we
have to live with.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 27 '07 #5
Scope Prefixes
Member Variable Prefix: Most of the people still use (and like the
idea of) prefixing member variables. Selection of prefix still remains
debatable. A lot of people use underscore to declare a member
variable. Use "m" as it also matches the convention generally followed
in VC++. Use of "this." in the code everywhere where a member variable
is used is clearly an overhead and there have always been a high
percentage of developers who tend to omit this causing standard non-
compliance. Hence this is not recommended.
I personally use the "_". It groups the fields in a completely different
section of the member dropdown, and is one less character than C++ (plus,
C++'s naming conventions are an awful starting point given how many prefixes
got put in there due to macros. Also, variable fields should never be
exposed publically anyway (I don't even expose them protected), and as
private members aren't subject to guidelines, you can really name them
whatever you want; I just use the _ + the property name, though. FWIW, I
also always use this. as well as class name for static props (so I can tell
quicker what I'm acting on)
>
Parameters Prefix: Length of a vast majority of functions in code
Hungarian Notation / Type Prefix: This is now regarded as a bad
The primary reasoning (IIRC) behind this is that the IDE should be telling
you what your parameters are rather than building it into the text of your
code. Back in the day, when you didn't have tooltips in your IDE, putting as
many queues as possible into the name itself as to what the var is and how
it is treated by the compiler. Now that the IDE has enough widgets to tell
you that in other ways, to put them in your code becomes superflous and
potentially confusing (we're getting close to a time when developers won't
know the difference between pFoo and lpFoo)

Constants (not readonly fields) should be all uppercase with
underscore as separator (matches the convention for VC++ and Win32).
It's an old convention and inconsistent with the casing standards (Pascal
for readibility and because Anders Heijsberg is da man). They now shoot for
readability and consistency, and having screaming text (the all caps)
changes the feel of the language.
Additionally, I cannot recommend enough Framework Design Guidelines (by
Krzysztof Cwalina and Brad Abrams). It's a collection of stuff like this
PLUS explanations as to why MS made the choices they did.
Feb 27 '07 #6
Now that I've got the Framework Guidelines in front of me, a few things:
1) they actually do say "DO NOT provide instance fields that are public or
protected." I'm actually a bit surprised, since MS itself uses protected
fields in the BCL, but it could be pre-guideline code (but not a given; even
3.0 breaks the IsX boolean convention)
2) Cwalina says the downsides to Hungarian notation are: "cost of
maintenance, confusion if maintenance was not done properly, and finally,
Hungarian makes the API more cryptic". He also seems to say that OO
encapsulation itself is making the need for knowledge of exact type (he also
shows up in the forums, so he can correct me if I'm wrong or incomplete).
Another annotator - Jeffrey Richter - brings up the tooltip point and goes
with using "m_" for private instance fields and "s_" for private static
fields, saying that it doesn't really matter since guidelines don't cover
private members; I don't use the m or s because I only differentiate between
static and instance during declaration/instantiation and referencing with
this. vs. MyType. This Richter guy gets an A+ in my book because he also
does not reference private fields outside the property; some developers will
use the field if possible for performance, but as he says, you aren't
protecting yourself from code (via property logic) like you do for external
usages.
3. Regarding all-caps, all that is said is "We used the term SCREAMING_CAPS
to indicate an all-uppercase style. Luckily this style (and name) did not
survive in the final guideline." I think the general annoyance people have
towards all-caps (try posting a Usenet post in all-caps to see :) ) is what
led to it being abandoned. I also tend to think that the general lack of
readibility that's developed in C++ over the years (Managed C++....wow)
meant that there would be a relatively clean break from C++ standards.

Feb 28 '07 #7
Keith, Mattias; thank you very much for explaining the Pros & Cons of
different conventions in detail.
Although I had come across the guidelines before, I didn't really
understand the rationale as the MS Naming Guildelines documentation
tend to be very concise (specifies "Do this" and "Dont do that" and
doesn't explain why; sometimes leaving veteran C/C++ developers to
wonder why they have opted for something different in .NET).
Its clear to me now that the private (or member; members MUST always
be private as per guildelines) variable naming convention is up to a
development team to choose (should still be documented though!).
Anything exposed (public, protected) must (or should?) conform to the
MS Naming Guidelines.
I find FxCop to be a great tool to automate such things (I assume
that, since the tool runs on a compiled assembly file, it just checks
for exposed members and doesn't care about your private variables
conventions) hence could be an excellent choice if one chooses MS
Naming Guidelines.
Once again thanks a lot for the excellent explanations,
recommendations , guidelines etc. etc. ... :).

Keith Patrick wrote:
Now that I've got the Framework Guidelines in front of me, a few things:
1) they actually do say "DO NOT provide instance fields that are public or
protected." I'm actually a bit surprised, since MS itself uses protected
fields in the BCL, but it could be pre-guideline code (but not a given; even
3.0 breaks the IsX boolean convention)
2) Cwalina says the downsides to Hungarian notation are: "cost of
maintenance, confusion if maintenance was not done properly, and finally,
Hungarian makes the API more cryptic". He also seems to say that OO
encapsulation itself is making the need for knowledge of exact type (he also
shows up in the forums, so he can correct me if I'm wrong or incomplete).
Another annotator - Jeffrey Richter - brings up the tooltip point and goes
with using "m_" for private instance fields and "s_" for private static
fields, saying that it doesn't really matter since guidelines don't cover
private members; I don't use the m or s because I only differentiate between
static and instance during declaration/instantiation and referencing with
this. vs. MyType. This Richter guy gets an A+ in my book because he also
does not reference private fields outside the property; some developers will
use the field if possible for performance, but as he says, you aren't
protecting yourself from code (via property logic) like you do for external
usages.
3. Regarding all-caps, all that is said is "We used the term SCREAMING_CAPS
to indicate an all-uppercase style. Luckily this style (and name) did not
survive in the final guideline." I think the general annoyance people have
towards all-caps (try posting a Usenet post in all-caps to see :) ) is what
led to it being abandoned. I also tend to think that the general lack of
readibility that's developed in C++ over the years (Managed C++....wow)
meant that there would be a relatively clean break from C++ standards.
Mar 1 '07 #8
"Use of 'this.' in the code everywhere where a member variable
is used is clearly an overhead"

I hope you mean typing overhead when writing the code, because there's
certainly no runtime overhead if you chose to use 'this'.
Sorry, I missed that in my earlier reply. Yes, I did mean typing
overhead. One of my goals is to be 100% compliant with whatever
standards we finalize and I do not want to put anything unnecessarily
in standards that stands JUST as an overhead for the (lazy! :))
developers to follow.

Mar 1 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1519
by: Dibyendu Basu | last post by:
Hi, We are developing a ASP.NET based application with SQL Server 2000 as backend. Can you please tell us some resources/URL in Internet/MSDN which talks about naming standards and conventions to be used in a C# based ASP .NET application? This is a bit urgent.
3
5008
by: boxboy | last post by:
I am hoping someone from Microsoft could shed some insight into this question. Why did microsoft decide to use a verb based naming convtion for events rather such as Close and Click for rather than prepending them with On as in OnClose and OnClick? The way it is now, I encounter a lot of naming conflicts/ambiguity when trying to follow Microsoft's reccommened approach in naming my own events.
5
1883
by: Ted | last post by:
I'm trying to come up with naming conventions for my company as we move to C#. I'm looking through the Naming Guidelines section on MSDN, but I'm unable to find a recommendation for class scope variables. Working with WinForms, I believe it would be helpful to have some type of prefix. In Visual Basic we used m_variableName. But I can't find any suggestions on this. Any help on this would be great. Thanks,
3
1434
by: C# Learner | last post by:
Why can't people abide by naming conventions?! It seems that most people have to have their /own/ custom convention.
38
12906
by: news.microsoft.com | last post by:
Greetings, I am posting this message to both the SQL Server and C# news groups because this inquiry is more of a theoretical question that applies to both database and code naming conventions. I have currently been prefixing boolean object properties and database columns with Is .. for example (defining a CAT being declawed): IsDeclawed. Well, the prefix of Is doesn't always work ... for example: IsHasWiskers .. well, that doesn't...
3
4339
by: clintonG | last post by:
Does the use of DTD, XML Schema and similar constructs adopt the use of C# naming conventions? If so how do I make the distinction of how to apply C# conventions with XML elements, attributes and so on? Any referrals to resources that discuss or document XML Naming Conventions? -- <%= Clinton Gallagher, "Twice the Results -- Half the Cost" Architectural & e-Business Consulting -- Software Development NET...
3
2370
by: John Salerno | last post by:
Does Microsoft have any naming guidelines for variables? I've been reading their guidelines for just about every other type (methods, parameters, properties, etc.) but nothing about variables. Are variables treated the same as parameters (i.e., camel case)? I notice that Hungarian notation is being done away with. Also, is it good practice to use an underscore to begin the name of a field? I've seen that, and it seems like a good idea,...
114
7815
by: Jonathan Wood | last post by:
I was just wondering what naming convention most of you use for class variables. Underscore, "m_" prefix, camel case, capitalized, etc? Has one style emerged as the most popular? Thanks for any comments. --
35
12169
by: Smithers | last post by:
Is it common practise to begin the name of form classes with "frm" (e.g., frmOneForm, frmAnotherForm). Or is that generally considered an outdated convention? If not "frm" what is a common or recommended practise? Thanks.
0
8139
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8555
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8232
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7024
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4032
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4098
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.