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
Linus Torvalds - Linux Kernel coding style from Linus Torvalds :
In general, I just use descriptive names. If it's an attribute, I make the first letter uppercase and apply camel-casing. For local variables, I keep everything lowercase. For variables that store attribute names, I use lower-case with an underscore as prefix. I avoid using any shorthand notations and generally don't need shorthand either. Code Completion is very useful with long names. :-)
Several IDE's will use Code Highlighting which is practical if you need to know if something is a class or variable. Thus I don't make many differences between class names and attribute names. (Except in Delphi, where I still prefix every class with a T since that's a standard convention in Delphi.)
And no, I don't use tmp. I write it out as Temporary, just in case I can't come up with a different name. Or I, J, K, L or M in case of index-numbers. (No, just those 5 letters, always uppercase.) In your case, I'd use "oldvalue" or "oldm_d" instead.
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
orcopied_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:
That way anyone looking at that code in the future should have no problems figuring out what you're doing and why.
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;
I would call it
saved_m_d
, or simplym_d
(but then, I would also givem_d
a different name).