Local Variables Declaration


Link to this posting

Postby Ursego » 19 Feb 2013, 22:13

Declare all local variables in the beginning (on the top) of the script, before the first executable line.

That will:

1. Make it easier to detect all the variables, used in the script, 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) - not when the declaration line is reached. In fact, program flow ignores these line. In the debugger, try to put a breakpoint on a line which declares a variable. Good luck! :lol:

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
User avatar
Ursego
Site Admin
 
Posts: 143
Joined: 19 Feb 2013, 20:33



Ketones are a more high-octane fuel for your brain than glucose. Become a biohacker and upgrade yourself to version 2.0!



cron
Traffic Counter

eXTReMe Tracker