Declare variables at top of function or in separate scopes?

后端 未结 9 1115
温柔的废话
温柔的废话 2020-12-01 01:47

Which is preferred, method 1 or method 2?

Method 1:

LRESULT CALLBACK wpMainWindow(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    switch (         


        
相关标签:
9条回答
  • 2020-12-01 02:05

    Variables should be declared as locally as possible.

    Declaring variables "at the top of the function" is always a disastrously bad practice. Even in C89/90 language, where variables can only be declared at the beginning of the block, it is better to declare them as locally as possible, i.e. at the beginning of smallest local block that covers the desired lifetime of the variable. Sometimes it might even make sense to introduce a "redundant" local block with the only purpose of "localizing" the variable declaration.

    In C++ and C99, where it is possible to declare variable anywhere in the code, the answer is pretty straightforward: again, declare each variable as locally as possible, and as close as possible to the point where you use it the very first time. The primary rationale for that is that in most cases this will allow you to supply a meaningful initializer to the variable at the point of declaration (instead of declaring it without initializer or with a dummy initializer).

    As for the memory usage, in general a typical implementation will immediately (as you enter the function) allocate the maximum space required for all variables that exist at the same time. However, your declaration habits might affect the exact size of that space. For example, in this code

    void foo() {
      int a, b, c;
    
      if (...) {
      }
    
      if (...) {
      }
    }
    

    all three variables exist at the same time and generally the space for all three has to be allocated. But in this code

    void foo() {
      int a;
    
      if (...) {
        int b;
      }
    
      if (...) {
        int c;
      }
    }
    

    only two variables exist at any given moment, meaning that space for only two variables will be allocated by a typical implementation (b and c will share the same space). This is another reason to declare variables as locally as possible.

    0 讨论(0)
  • 2020-12-01 02:14

    Since it's the compiler's job to optimize my code, and an hour of compiler-time is way cheaper than an hour of my time, and my time gets wasted if I need to scroll up and down the code to see where a variable was declared, I think my company wants me to keep everything as local as possible.

    Not even am I talking about 'the smallest block', but 'as near to the place where it is used'!

    LRESULT CALLBACK wpMainWindow(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) 
    { 
        switch (msg) 
        { 
            case WM_PAINT: 
            { 
                RECT rc; 
                GetClientRect(hwnd, &rc);            
    
                { // sometimes I even create an arbitrary block 
                  // to show correlated statements.
                  // as a side-effect, the compiler may not need to allocate space for 
                  // variables declared here...
                  PAINTSTRUCT ps; 
                  HDC hdc = BeginPaint(hwnd, &ps); 
                  // drawing here 
                  EndPaint(hwnd, &ps); 
                }
                break; 
            } 
            default:  
                return DefWindowProc(hwnd, msg, wparam, lparam); 
        } 
        return 0; 
    } 
    
    0 讨论(0)
  • 2020-12-01 02:14

    You can't know at what point the stack reservation is done.

    For readability I would go with C99 (or C++). That allows you the declaration of a variable really there where first use it.

     HDC hdc = BeginPaint(hwnd, &ps);
    
    0 讨论(0)
提交回复
热议问题