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 of its members locally to avoid a de-referenced, and then any modification assign back to the pointer.
To be more concrete:
struct Foo
{
double m_d;
};
void function (Foo* f)
{
double tmp=f->m_d;
/***Other stuff***/
f->m_d=tmp;
}
I don't like tmp. If I have many of them in a function, they only add a confusion.
Thanks
What do you store in the
tmp
variable? Use that description as a variable name, if it isn’t too long. For three-line functions (swap
…)tmp
is just fine. For almost everything else, be descriptive.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:
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.
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.My suggestion is to simply incorporate the original variable name, or some other identifier that alerts readers of its intented function.
If a more descriptive them can't be thought of, the convention we practice at work is to use the name "my[object]" in lieu of a better name. Above all, make it descriptive so that your code is easier to understand and maintain. Self-documenting code is another benefit of doing this.