Archived

This topic is now archived and is closed to further replies.

kordova

[java] Your Variable Naming Conventions

Recommended Posts

So, what does everyone here use typically for general variable naming? (This seems rather unimportant but I am interested to know whether people use the myVariableName or _myvariablename etc.) Thanks ahead of time for any responses.

Share this post


Link to post
Share on other sites
Normally, I use the first one. All small letters, with capitals for the start of every word except the first.
int someReallyUsefulVariable = 5;


~~~~~~~~~~
"Usewhitespacetoenhancereadability" - Steve McConnell, Code Complete

Share this post


Link to post
Share on other sites
Why is the first letter not capitalised?

I usually just capitalise the first letter of every word in the variable name:

int SomeReallyUsefulVariable

When I make a class I prefix the class name with two letters describing what it is. For example, a Lightwave object:

class lwObject;


--------------------

Never eat anything bigger than your own head.

Share this post


Link to post
Share on other sites
Magmai''s convention is closest to mine: attempt to convey as much info as possible in the variable name about ownership, scope and type. I also employ the trailing underscore for parameters, but I haven''t used usffixes for units (nice idea, though!)



I wanna work for Microsoft!

Share this post


Link to post
Share on other sites
The standards layed out by Sun are as follows.

variables always start with a lowercase letter and each word after that is capitalized to enhance readability.

classes always start with an uppercase letter.

in this way even if you dont use variable names that describe what they are.. ie firstNameStr ( with Str meaning String ) you will at least know that firstName is a variable and FirstName is a class.



-= programing is almost a performance art. We create the illusions on screen, knowing full well that in the background things may be very different. =-

Share this post


Link to post
Share on other sites
I use a slightly modified version of the Hungarian notation, i.e. classes begin with capital C, member variables begin with m_ etc. All names are in lowercase letters except the first in every word, e.g.:

class CMyClass
{
int m_nAnInteger;
};

Share this post


Link to post
Share on other sites
I use a mixture of casing and modified Hungarian notation to determine variable scope.

Prefix
I interface
C class
s_ static
m_ member variable
v_ volatile
g_ global
auto-local vars have no prefix

For functions, I use casing to differentiate function scope.
''Lower-Upper'' spelling (eg fooBar) for all static functions, protected members and ''Upper-Upper'' for all publically visible functions and public members.

Share this post


Link to post
Share on other sites
The hungarian notation is NOT C++ specific. It was developed to standardize and enhance the readability of source code.

In my opinion SUN''s Java coding guidelines are too "shallow" to be followed. They only differentiate classes from variables, while I like to immediately see the scope of the variable, if it has any special significance (array or enumeration) and it''s type. That''s why I''ve taken in use the hungarian notation as I see it best fitting the Java language.

Class and interface names always start with capital letter. I used to use capital "C" to specify class and capital "I" to specify interface, but it looked too strange to many Java developers so I gave up using it in my GF4J library.

My variable notation (used in GF4J library) is as follows:

1) Primary prefixes that always precede any other prefixes.
These specify the scope of the variable:
m_ class member variable
mStatic_ static class member variable

2) Secondary prefixes that precede terniary prefixes.
These specify if the variable has special purposes:
a array (followed by the type of the array
if primary type like int, byte etc. or nothing
if object type).
e enumerated type (followed by the variable
type if not int).

3) Terniary prefixes that precede variable name.
These specify the type of the variable:
object type (has no prefix)
b byte type
c char type
d double type
f boolean (flag) type
fl float type
i int type
l long type
s short type
str String type

The constant (final) values are always written in all capital with spaces replaced with "_" character.

An example:

public class CMyClass
{
public static final int STATE_INACTIVE = 0;
public static final int STATE_ACTIVE = 1;

private static int m_eState = STATE_INACTIVE;

private int m_iIndex = 0;
private byte[] m_abBuffer = new byte[10];

public void setIndex( int iIndex )
{
m_iIndex = iIndex;
}
}


I''ve considered starting to use prefixes "in" and "out" in method parameters to specify if the parameter is "in" type (provides only input to the method), "out" type (is used only to return some result from the method) or "inout" type (is used as both input and output carrier for the method).

I''ve also thought about changing "m_" to "my" and "mStatic_" to "the", but these mess up with the rest of the rules too much (e.g. myabBuffer instead of m_abBuffer).

The notation that you are using is mostly to do with personal preferences or company rules. As a guide I''d suggest that everyone aiming to one day become a professional developer should start using some sort of notation. It is much easier to change your notation according to the rules set by the company you start working for than it is to learn to use one if you have no experience in using a notation.

Share this post


Link to post
Share on other sites
I wasn''t implying that hungarian notation was C++ specific, I was refering to the fact that some of the examples given include pointers, C array declarations, volatile variables and other things that don''t not apply to Java.

- Kaijin

"If you find a job that you love you''ll never have to work again."

Share this post


Link to post
Share on other sites