views:

495

answers:

10

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

+18  A: 

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.

Martin B
+1, making sense makes sense.
Michael Krelin - hacker
What if I don't have a m_. I am using a very old c code structures? thx
vehomzzz
having `m_` stinks, anyway ;-) Seriously, just make sense of the variable. If you save the value of d, feel free to save it to `old_d`, `temp_d` or whatever pleases you and makes sense to you.
Michael Krelin - hacker
@enigma: Then just use the same name as the structure field you're copying. There won't be a naming conflict because to access the field, you explicitly have to prefix it with the structure that it's part of.
Martin B
+3  A: 

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.

Konrad Rudolph
+2  A: 

I would call it saved_m_d, or simply m_d (but then, I would also give m_d a different name).

Martin v. Löwis
I would too, m_d is just for illustration...
vehomzzz
Unfortunately, "for illustration" defeats the purpose of your question. You can get good recommendations only if you give a realistic example.
Martin v. Löwis
A: 

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;

Naveen
A: 

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.

Secret Agent Man
A: 

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.

Extrakun
+3  A: 

Linus Torvalds - Linux Kernel coding style from Linus Torvalds :

LOCAL variable names should be short, and to the point. If you have some random integer loop counter, it should probably be called "i". Calling it "loop_counter" is non-productive, if there is no chance of it being mis-understood. Similarly, "tmp" can be just about any type of variable that is used to hold a temporary value.

If you are afraid to mix up your local variable names, you have another problem, which is called the function-growth-hormone-imbalance syndrome.

Sorry – but Linus Torvalds is simply no good reference when it comes to code style, see his diatribes against `goto` opponents [http://kerneltrap.org/node/553/2131]. In that particular case he *is* (kind of) right (IMHO) but as appeals to authority go, this one is a fallacy.
Konrad Rudolph
@Konrad:What you said is an ad hominem argument: an argument that attacks the person who holds a view or advances an argument, rather than commenting on the view or responding to the argument....
I do not always support Linus' opinions, but I do agree with *both* samples (the temporaries and goto). What he said in both cases — don't blindly stick to stupid rules until you understand the implication of both following and contradicting them. And, although this point is strongly opposed in the world of enterprise droids, it does make perfect sense if you deal with mentally-enabled human beings.
Michael Krelin - hacker
I hate the idea of using 'i' or any single letter as a variable name. Have you ever tried to search through the code looking for all uses of 'i'! You stop in nearly every sentence in every comment and whole bunch of other identifiers, its painful. Keep them short but not that short.
Martin York
@Martin: Then what do you propose one calls a series of variables that are used in nested 2-line for loops, other than `i`, `j`, `k`, etc.? Their scope is for only the for loop, so why be any more verbose?
greyfade
Martin, I'm not sure about specific syndrome name, but searching through code for the loop variable is enough of a problem in itself.
Michael Krelin - hacker
A: 

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.

Workshop Alex
+1  A: 

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;
}
StackedCrooked
A: 

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.

TheUndeadFish