Creating Statements

Variables

Tip: Variables should be initialized where they are declared and they should be declared in the narrowest possible scope. This ensures that variables are valid at any time.

Sometimes, it is impossible to initialize a variable to a valid value where it is declared. In these cases, it should be left uninitialized rather than initialized to some phony value.

Tip: Variables must never have dual meaning.

Enhance readability by ensuring that all concepts are uniquely represented. Reduce the chance of error by side effects.

Tip: Variables should be kept alive for as short a time as possible.

By keeping the operations on a variable within a narrow scope, it is easier to control the effects and side effects of the variable.

Loops

Tip: Loop variables should be initialized immediately before the loop.
	ready as Bool = true
	while ready do
	       //Do something
	end
	
Not:
	ready as Bool = true
	
	 // Other stuff....
	
	while ready do
	       //Do something
	end
	
Tip: The use of break and exit should be minimized.

These statements should be used only if they prove to give a higher readability than their structured counterparts.

Conditionals

Tip: Complex conditional expressions should be avoided. Introduce temporary boolean variables instead.
For example, the following code:
	if (elementNo < 0) or (elementNo > maxElement) 
	          or elementNo = lastElement then
	  //Do something
	end
	
Should be replaced by:
	isFinished as Bool = (elementNo < 0) or (elementNo > maxElement)
	isRepeatedElement as Bool = elementNo = lastElement

	if isFinished or isRepeatedElement then
    //Do something
	end
	

By assigning boolean variables to expressions, the program gets automatic documentation. The construction will be easier to read and debug.

Tip: The nominal case should be put in the if-part and the exception in the else-part of an if statement.
	isError as Bool = readFile (fileName)
	
	if not isError then
	  //Do something
	else 
	  //Handle the error
	end
	

Makes sure that the exceptions do not obscure the normal path of execution. This is important for both the readability and performance.

Tip: Executable statements in conditionals must be avoided.
	file = openFile (fileName, "w")
	if file /= null then
	       //Do something
	end
	
Not:
	if (file = openFile (fileName, "w")) is not null then
	    //Do something
	end
	

Conditionals with executable statements are very difficult to read. This is especially true for new programmers.

Miscellaneous

Tip: The use of magic numbers in the code should be avoided.
If the number does not have an obvious meaning by itself, the readability is enhanced by introducing a named constant instead.
	   d = s / SECONDS_PER_DAY
	   timeout = DEFAULT_TIMEOUT
	
Not:
	  d = s / 86400
	  timeout = 1000
	
Tip: Real and Decimal constants should always be written with a decimal point and at least one decimal:
total = 0.0
speed = 3.0
	
sum = (a + b) * 10.0;
Not:
total = 0;
speed = 3;
	
sum = (a + b) * 10;

This emphasizes the different nature of integer and floating point numbers, even if their values might happen to be the same in a specific case.

Moreover, as in the last example above, it emphasizes the type of the assigned variable (sum) at a point in the code where this might not be evident.

Tip: Real and Decimal constants should always be written with a digit before the decimal point.
	total as Real = 0.5f
	
Not:
	total as Real = .5f
	

The number and expression system in Studio is borrowed from mathematics and one should adhere to mathematical conventions for syntax wherever possible. In addition, 0.5 is much more readable than .5. There is no way it can be mixed with the integer 5.