Page 1 of 1

Local Variables Declaration

PostPosted: 19 Feb 2013, 22:13
by Ursego
Declare local vars in the beginning (on the top) of the method, before the first executable line.

That will:

1. Make it easier to detect all the variables, used in the method, and to follow them. Most programmers immediately look in the beginning of a program for variables declaration.

2. Keep as little stuff as possible in executable code fragments, where programmers should concentrate on business logic.

In PowerBuilder (in contrast to Java), the local variable declaration is not an executable command. The memory is allocated on the stack when the function is being called - together with the parameters.

So, declaring a variable inside an if block does not improve performance – memory will be allocated even when the program flow doesn’t go into the if. The following two fragments produce a same binary code:

Fragment 1:

Code: Select all
long ll_row
if <condition> then
      ll_row = …
end if

Fragment 2:

Code: Select all
if <condition> then
      long ll_row
      ll_row = …
end if