Naming variables and classes

I have such a big problem naming my variables and classes that i spend more time thinking about how to name them then programming actual code or debugging it. This might sound really stupid but this bother me alot. Isnt there some standard way of naming them, Some set of rules that could apply to all variables and classes i will create in the future?
The only rule is that a naming scheme is consistent, and is meaningful. A counter should probably have counter in the name. Anything with coordinates should probably have x for the x value, and y for the y value. Just name it for what it is. If you have a variable for the gas mileage of a car, call it gasMileage.
Well, there's a few conventional names that are used when using common constructs.
i, j, k are the most common indices for for loops (personally, I prefer a, b, c, but that's just me).
accum is used when something needs to be accumulated. For example, to get the sum or the product of a bunch of numbers.
count is used to keep track of how many elements in a container or space match some condition. For example, getting the average of the elements in a vector would use a sum accumulator and a count, although it may not be necessary to actually compute the count.
file, input_file, output_file or variations thereof are commonly used when a function or block of code uses one or two files. It's rare to have more than two files open in the same lexical scope. When that happens, it's usually obvious what to name them.
buffer is used when binary data is read from or written to some kind of virtual or physical I/O device. input_buffer and output_buffer are also common variations.
x, y, z are used to name some variable that moves in one or more dimensions. x is also often used as just a general mathematical variable, as in
1
2
3
double square(double x){
    return x*x;
}


Of course, there's no general set of rules to name variables because it would have to cover every possible program.
In general, you should name variables based on the semantics of their contents. You shouldn't have to get creative to come up with names. If you have a variable that stores the second degree coefficient for a quadratic equation, you don't name it 'Superman' or 'qwerty'. You name it 'coefficient2', or 'coeff2'. If what the variable does isn't obvious to you, it's because you haven't thought what you're doing all the way through.
Last edited on
There are no golden rules, but this is what I recommend:



Classes and structs are nouns and should be a general description of what an instance of the class represents.

Examples might be File, String, Rectangle, Window, etc


Functions are verbs and should describe the task the function performs. Examples might be RunMainMenu, AttackPlayer, OpenFile, etc.

Status query functions that return a bool should ask a yes/no question. IsFileOpen, DoesPlayerHaveItem, etc


Variables should generally be nouns and should describe specifically what this particular object represents. Contrary to class names which should be a general description.

1
2
File mapfile;  // a file that contains a map
int playerlife;  // how much life the player has 


boolean variables should ask a yes/no question like status query functions. If they don't specifically ask a question, they can suggest one (you can paraphrase the question)

1
2
bool isalive;  // is it alive?
bool keeplooping;  // do we want to keep looping? (paraphrased) 


To avoid double-negative confusion, boolean functions and variables should be named so the more "positive" result is true, rather than false. That is, "isalive" is better than "isdead". "hasjob" is better than "nojob", etc.
And (arguably) just as important:
1
2
3
4
5
//bool isalive; //should be
bool isAlive; //or
bool is_alive;
//because I for one don't want a boolean named
bool island; //does that mean "is land" or "is an island" 

Last edited on
I agree with Mathhead200, all variable names consisting of multiple words should definitely contain either capital letters or underscores to help distinguish each word!

eg:

1
2
3
int thisIsMyVariable; // good
int this_is_my_variable; // good
int thisismyvariable // bad 



Otherwise you will inevitably run in to issues as described by mathhead200 :)
Last edited on
Topic archived. No new replies allowed.