Whenever i put up a code for review from professional programmers they tend to point out that \"using a variable named temp is bad\" but no one seems to know why.
Why is
temp
indeed doesn't mean anything useful. A better question is: does it have to?
Without knowing the context (how is it used in your code?), it's hard to say what it's for, and whether temp
is a good name. If you use a variable often or non-locally, the name must be descriptive. A name like temp
can be fine if you use it, say, three times in three adjacent lines.
void swapIntPointers(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
It's immediately obvious what this function should do (from its name) and what it actually does (from its structure). In this specific case, I strongly prefer short (and automatically nondescript) names. I'd even say that temp
may be a little too long here ;)
However:
It's because temp
suggests something about the longevity of the variable (temporary) but nothing about the meaning or significance of its content. Variables are generally best named to reflect what their underlying value is intended to represent.