Since the language is similar in many ways to the 'C' programming language then a number of coding style suggestions can be taken from the common 'C' programming style. Programming style is important since it makes a program more readable to a human, thus making debugging and code maintenance easier for the programmer.
For example, the following code is perfectly valid and will compile successfully, but is difficult to read by a human programmer:
main int i; int j; for(i=0;i<10;i++) for (j=0;j<10;j++) int tot; tot=i*j; applog("tot=",tot); endfor endfor endmain
Whereas the following code is much easier to read:
main
int i;
int j;
for(i=0;i<10;i++)
for (j=0;j<10;j++)
int tot;
tot=i*j;
applog("tot=",tot);
endfor
endfor
endmain
As you can see, by using appropriate indentation and having only one statement per line the code becomes much more readable. Typically another level of indentation should be used for each new statement block, and the if...else..endif, while..endwhile, for..endfor statements should all have the same level of indentation.
Also, since it is possible to nest if..else..endif statements within each other, then a decision needs to be made about how to indent these nested statements. It is suggested that when several if..else..endif statements are nested within each other then those statements which are at the same logical level should be indented to the same extent. What is meant by the same logical level will depend upon the context, but will typically be where a certain variable is been tested against various values.
For example in the following code there are a number of tests being made against the variable x in a set of nested if..else..endif statements, and it is tempting to indent each of these nested statements one indent further for each statement block as follows:
if(x==0)
applog("Got x=0");
else
if(x==1)
applog("Got x=1");
else
if(x==2)
applog("Got x=2");
else
if(x==3)
applog("Got x=3");
else
applog("Got something else");
endif
endif
endif
endif
The above code is perfectly valid but as you see the nested if..else..endif statements are being indented further and further to the right, making the code more difficult to interpret (especially as the code gets larger).
Since the test of each value of x is at the same logical level then a better coding style would be to keep the indent of each if..else..endif statement at the same indentation as follows:
if(x==0)
applog("Got x=0");
else if(x==1)
applog("Got x=1");
else if(x==2)
applog("Got x=2");
else if(x==3)
applog("Got x=3");
else
applog("Got something else");
endif endif endif endif
Notice that the number of endif statements at the end then just needs to match the number of if statements that have previously appeared at the same logical level and indentation.