Naming Conventions

S

Scott M.

I am looking at some best practices concerning the naming of identifiers in
C# and am puzzled on what to do with Public and Private identifiers that are
related, such as private fields and their corresponding public properties.

From "Visual C# .NET Step by Step" (pages 29 & 30):
//*********************************************************************
The Microsoft .NET Framework documentation makes several recommendations
about variable naming:

1. Don't use underscores.
2. Don't create identifiers that differ only by case.
3. Use Camel Case.
4. Don't use Hungarian notation.

* You should treat the first two recommendations as compulsory because they
relate to CLS compliance. If you want to write programs that can
interoperate with other languages, such as VB.NET, you need to comply with
these recommendations.
//*********************************************************************

Ok, now I can understand item #2 because VB.NET is not case-sensitive, so it
wouldn't be able to tell the difference between x and X.
But what about underscores? I like to name my private variables with an
underscore and the public property that abstracts the private variable
without so that something other than case can differentiate them, such as:

[VB.NET]
Private _numberEmployees As Short

Public Property NumberEmployees As Short
 
J

Jon Skeet [C# MVP]

So, if underscores are out and only differing by case is out, doesn't that
either leave different names completely (ex. numEmployees & NumberEmployees)
or Hungarian notation?

No - because what's not clear fro mthe docs is that it's only *non-
private* identifiers that are an issue. From a different assembly, only
public and protected members can be seen - VB.NET doesn't care if your
C# code uses

private string foo;
public string Foo
{
get { return foo; }
set { foo = value; }
}

(and indeed that's my personal style).
 
S

Scott M.

Ok, but addresses the case issue. What about the use of underscores and CLS
compliance?

Thanks.
 
M

Michael C

Scott M. said:
Ok, but addresses the case issue. What about the use of underscores and
CLS compliance?

Huge amounts of private vars in the framework itself use _ and m as a
prefix.

Michael
 
M

Michael Nemtsev

Hello Scott M.,

the power of underscope is in readability. Not always code is viewed in Visual
Studio. For example during code review you can use diff editors or print
you code on paper.

When u use _ for private/protected vars you always knows the access modifier
for the every variable in you code that u see without reffering to the variable
declaration. It really helps during code review

S> I am looking at some best practices concerning the naming of
S> identifiers in C# and am puzzled on what to do with Public and
S> Private identifiers that are related, such as private fields and
S> their corresponding public properties.
S>
S> From "Visual C# .NET Step by Step" (pages 29 & 30):
S> //*******************************************************************
S> ** The Microsoft .NET Framework documentation makes several
S> recommendations about variable naming:
S>
S> 1. Don't use underscores.
S> 2. Don't create identifiers that differ only by case.
S> 3. Use Camel Case.
S> 4. Don't use Hungarian notation.
S> * You should treat the first two recommendations as compulsory
S> because they relate to CLS compliance. If you want to write programs
S> that can interoperate with other languages, such as VB.NET, you need
S> to comply with these recommendations.
S> //*******************************************************************
S> **
S>
S> Ok, now I can understand item #2 because VB.NET is not
S> case-sensitive, so it
S> wouldn't be able to tell the difference between x and X.
S> But what about underscores? I like to name my private variables with
S> an
S> underscore and the public property that abstracts the private
S> variable
S> without so that something other than case can differentiate them,
S> such as:
S> [VB.NET]
S> Private _numberEmployees As Short
S> Public Property NumberEmployees As Short
S> .
S> .
S> .
S> End Property
S> Now, VB.NET and C# support underscores, so don't the other CLS
S> languages? And if they do, why should I stay away from using them?
S>
S> So, if underscores are out and only differing by case is out, doesn't
S> that either leave different names completely (ex. numEmployees &
S> NumberEmployees) or Hungarian notation?
S>
S> -Thanks
S>
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
J

Jon Skeet [C# MVP]

Scott M. said:
Ok, but addresses the case issue. What about the use of underscores and CLS
compliance?

The same thing - CLS compliance doesn't care about names that aren't
accessible outside the class.
 
G

Guest

Yes, acctually I don't thinkg it is a bad idea to use _ as the private
prefix. But you'd better keep the style through the whole prjects. I guess
some people hate _ becuase you need an additional SHIFT hit for it :), and
this is true for me.
 
S

Scott M.

I do use it consistently for private vars and feel that the extra keystroke
to get it is worth the readability of the public and private var having the
same name, except for the underscore.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top