views:

152

answers:

8

I have a naming strategy for denoting the nature of code entity (variable, method, etc.) which accounts for the most common permutations of scope, entity type, and mutability, but I have not been able to choose a way of denoting private static member fields (not properties).

What are some recommended ways of denoting this?

Update: For what it's worth, I do understand that this has fallen out of fashion, and that for C# particularly, Microsoft argues against it. I should specify that my goal in this is not to provide a level of "documentation", but simply to be able to get as much information as possible with as little motion and cross referencing as possible.

Personally I feel it would be ideal if identifiers could be restricted to conveying information about purpose, and leave code context information to the IDE to convey. However, since VS2008 does not appear to provide highlighting/styling options which are based on scope or mutability, and I don't feel that tooltips are particularly efficient in this because of the required mouse motion and waiting, I find myself limited in my options.

For reference, I primarily program in C#, and below is a sampling of my current naming conventions:

  • Method argument or local variable: thisIsMyVariable
  • Instance-level private field: _thisIsMyVariable
  • Public instance- or class-level property or method: ThisIsMyVariable
  • Constant: THIS_IS_MY_VARIABLE

For private static fields I've been considering these:

  • s_thisIsMyVariable
  • _ThisIsMyVariable
+4  A: 

I use s_ for static members and m_ for instance members.

However what you're asking for, and what I do, contravene the Microsoft-recommended naming conventions for C# which are listed here: Names of Type Members.

ChrisW
+1 for informative
annakata
+1  A: 

I don't differentiate between private static or instance variables. They're both camel-cased with a leading underscore:

private static readonly ILog _someLog = ...;

And I don't think I've ever been left scratching my head as a result.

HTH, Kent

Kent Boogaart
A: 

I have no doubt this will degenerate into an argument about who's way is better (put those braces on the same line dammit!) but I don't like doing or seeing anything like this apart from the constants. I can see your reasoning behind this but unless you're a one-man shop it will just cause confusion when someone else is editing your code and doesn't stick to it.

I personally prefer to not care if its method, instance, static etc. scope and let the IDE help me on that when I do need to know. Besides, aren't there naming conventions published for your language?

MrWiggles
I see lot of "let my IDE take care of that" answers. What if you use a plain editor? If you print your code? If you show it on the Net? Etc.
PhiLho
A: 

Like Kent, I camel case with a leading underscore. Prefixing declarations has not been in vogue for some time. I find that organizing my declarations into regions and the tools tips that you get in the IDE are sufficient to keep track of this stuff.

Jim Petkus
I don't feel that tooltips are sufficient, or efficient. I want to know at-a-glance, not have to move the mouse, hover, and wait.
Chris Ammerman
A: 

I wouldn't use underscores, as everything public should look nice.

As for me, I use prefix the, like theApplication, theSettings, etc.

Quassnoi
"the"? StaticClass.theSettings doesn't seem right. Agree about the underscores.
annakata
+1  A: 

I use g_ , as static variable is nothing but a glorified global variable

Naveen
+1 for the "glorified global variable" !
Guillaume
+2  A: 

You should stick to the conventions already in place for your language. Your IDE should make clear which variable is public / private, static / instance ... There was a need to put the visibility or the type of a variable in its name before we had syntax coloring editors. But now that we have it, I find the code much cleaner and easier to read if it isnt cluttered with prefixes.

Guillaume
I feel there IS a need for being able to gather this information at-a-glance, rather than having to focus in on the identifier, hover for tooltip, or whatever. My IDE (VS2008) doesn't support special highlighting for static, AFAIK, so I'm limited in my options in that regard.
Chris Ammerman
+3  A: 

I let my IDE format them differently. Way easier to see that italic=static. Then, when I have to refactor code I don't have to worry about renaming as well.

Marc Hughes
I like this idea.... but unfortunately I don't believe VS2008, which is what I work in, offers this level of customization.
Chris Ammerman