naming convention of temp local variables

2019-03-01 06:38发布

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

11条回答
迷人小祖宗
2楼-- · 2019-03-01 07:23

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.

查看更多
Fickle 薄情
3楼-- · 2019-03-01 07:25

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.

查看更多
闹够了就滚
4楼-- · 2019-03-01 07:25

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.

查看更多
干净又极端
5楼-- · 2019-03-01 07:25

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;

查看更多
相关推荐>>
6楼-- · 2019-03-01 07:33

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

查看更多
登录 后发表回答