Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare variables at top of function or in separate scopes?

Which is preferred, method 1 or method 2?

Method 1:

LRESULT CALLBACK wpMainWindow(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {     switch (msg)     {         case WM_PAINT:         {             HDC hdc;             PAINTSTRUCT ps;              RECT rc;             GetClientRect(hwnd, &rc);                         hdc = BeginPaint(hwnd, &ps);             // drawing here             EndPaint(hwnd, &ps);             break;         }         default:              return DefWindowProc(hwnd, msg, wparam, lparam);     }     return 0; } 

Method 2:

LRESULT CALLBACK wpMainWindow(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {     HDC hdc;     PAINTSTRUCT ps;     RECT rc;      switch (msg)     {         case WM_PAINT:             GetClientRect(hwnd, &rc);              hdc = BeginPaint(hwnd, &ps);             // drawing here             EndPaint(hwnd, &ps);             break;          default:              return DefWindowProc(hwnd, msg, wparam, lparam);     }     return 0; } 

In method 1, if msg = WM_PAINT when wpMainWindow function is called, does it allocate memory for all the variables on the stack at the beginning? or only when it enters the WM_PAINT scope?

Would method 1 only use the memory when the message is WM_PAINT, and method 2 would use the memory no matter what msg equaled?

like image 353
Kaije Avatar asked Sep 22 '10 20:09

Kaije


People also ask

Should you declare variables at the top of a function?

It's best to declare variables when you first use them to ensure that they are always initialized to some valid value and that their intended use is always apparent. The alternative is typically to declare all variables in one location, typically at the top of the block or, even worse, at the top of a function.

Where is the best place to declare variables?

The recommended practice is to put the declaration as close as possible to the first place where the variable is used. This also minimizes the scope. From Steve McConnell's "Code Complete" book: Ideally, declare and define each variable close to where it's first used.

What are the benefits of keeping declaration at the top?

Declare variables at the top of the function as a means of documenting all variables used in one place. It also avoids confusion resulting from someone imagining that a variable is block-scoped when it is in fact not, as in the following: var i=0; if (true) { var i=1; } // what is i?

Should you declare variables inside or outside a loop?

Declaring variables inside or outside of a loop, It's the result of JVM specifications But in the name of best coding practice it is recommended to declare the variable in the smallest possible scope (in this example it is inside the loop, as this is the only place where the variable is used).


1 Answers

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.

like image 108
AnT Avatar answered Sep 23 '22 15:09

AnT