What is the standard way to name a temp variable in the local function? let me give you an illustration of what I am doing. I get a pointer to a structure, so I want store one o
For your information: Code Complete has a chapter decicated to variable naming.
In your example one problem is that the member variable in Foo
is not very descriptive to start with, which makes it hard to find a useful name for the placeholder local variable.
For example, I would do something like this:
struct Foo
{
double mValue; // I don't use underscores here
// however, you can do as you please.
// Also 'mValue' is just for the sake of example,
// you should find a more descriptive name :D
};
void function (Foo* f)
{
double oldValue = f->mValue;
/***Other stuff***/
f->mValue = oldValue;
}
I use m_ for member variables and do not use any prefixes for the local variables. So in this case it would be double d;
Do the same thing you would for any other variable: Give it a concise, expressive name. How about using the original name of the member variable you're copying (possibly leaving off the m_
)? That's the best way to make the connection between the two explicit.
There are some 'frequently used shorthands', such as i,j and m and n for loops, but if you are going to have many loops in a function, it's probably best to have something more expressively.
I would use the rules for good variable naming for most cases. If you are doing C++, the question I think is 'how am I going to differentiate member variables' instead of local variables.
I'd say try to find something that most specifically describes the purpose of the variable and at the same time distinguishes it from any other variables used in that function.
So, assuming that "d" actually represents some name that already describes the meaning of your variable, then I'd go with something like cached_d
or copied_d
. That way you can have more (cached_a
, cached_b
, cached_c
, etc) without confusion among them.
And then I would further suggest including a comment that states specifically why you made that local copy. Perhaps something like:
double cached_d = f->m_d; // cached to avoid further de-referencing
That way anyone looking at that code in the future should have no problems figuring out what you're doing and why.