| Code Indentation Many
programmers have found that indenting/structuring their code helps them immensely when
trying to follow their programs flow.
Typically, conditional statements (If, Select) or loops (While, Repeat, For) are indented to clarify
which lines of code are contained within their boundaries.
Take for example the two code snippets on the right. The upper snippet is not
indented, and the lower snippet is. They both do the exact same thing but the
indented version makes it much easier to understand what's happening within that code,
doesn't it?
With the indented version (lower snippet) you can quickly see which sections of code
are executed or not executed depending on the outcome of the If
statements. For example, if the If move_box = True
statement evaluated to False, you can quickly see that none of the code listed would be
executed.
However, with the non-indented version (upper snippet), if the If
move_box = True statement evaluated to False, you must really study the entire
snippet and read it through thoroughly to determine which parts will or won't be executed.
Clearly the lower snippet is easier to read and understand and is more efficient. |
If move_box = True
If KeyDown(30) TurnEntity box, 0, 1, 0
If KeyDown(32) TurnEntity box, 0, -1, 0
If KeyDown(31) MoveEntity box, 0, 0, -.01
If KeyDown(17) MoveEntity box, 0, 0, .01
If EntityCollided(box, terrain)
If KeyDown(17)
speed = speed + .001
If speed > .5 Then speed = .5
Else If KeyDown(31)
speed = speed - .02
If speed < -.5 Then speed = -.5
Else
speed = speed * .99
EndIf
MoveEntity box, 0, 0, speed
TranslateEntity box, 0, -.01, 0
Else
TranslateEntity box, x_vel, y_vel, z_vel
EndIf
EndIf
If move_box = True
If KeyDown(30) TurnEntity box, 0, 1, 0
If KeyDown(32) TurnEntity box, 0, -1, 0
If KeyDown(31) MoveEntity box, 0, 0, -.01
If KeyDown(17) MoveEntity box, 0, 0, .01
If EntityCollided(box, terrain)
If KeyDown(17)
speed = speed + .001
If speed > .5 Then speed = .5
Else If KeyDown(31)
speed = speed - .02
If speed < -.5 Then speed = -.5
Else
speed = speed * .99
EndIf
MoveEntity box, 0, 0, speed
TranslateEntity box, 0, -.01, 0
Else
TranslateEntity box, x_vel, y_vel, z_vel
EndIf
EndIf
|