naming convention of temp local variables

后端 未结 11 1017
我在风中等你
我在风中等你 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 06:08

    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.

提交回复
热议问题