naming convention of temp local variables

后端 未结 11 1018
我在风中等你
我在风中等你 2021-01-23 05:40

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

相关标签:
11条回答
  • 2021-01-23 05:46

    My suggestion is to simply incorporate the original variable name, or some other identifier that alerts readers of its intented function.

    struct  Foo
    {
      double m_d;
    
    };
    
    
    void function (Foo* f)
    {
       double m_d_tmp = f->m_d;
    
           /***Other stuff***/
    
         f->m_d = m_d_tmp;
    }
    
    0 讨论(0)
  • I would call it saved_m_d, or simply m_d (but then, I would also give m_d a different name).

    0 讨论(0)
  • 2021-01-23 05:50

    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.

    0 讨论(0)
  • 2021-01-23 05:50

    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.

    0 讨论(0)
  • 2021-01-23 05:53

    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.

    0 讨论(0)
  • 2021-01-23 06:00

    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.

    0 讨论(0)
提交回复
热议问题