Developing Style

[ LiB ]

Everyone has their own style of coding; it's an inevitable fact. No two people enjoy their code the same way. Basically, to create a style for yourself, you just have to discover what is right for you and stick with it. Lesson one in the style primer: Be Consistent .

Let's start out with the most basic form of style: white space. Examples of white space are tabs, spaces, and new lines. Under most circumstances, you can add as much white space as you like to the beginning and end of most lines. White space can also be included between test commands (such as < and >) and what is being tested , as in Figure 4.1.

Figure 4.1. White space.


White Space and Indentation

The easiest way to see the use of white space is with examples. The first code snippet I will show you is a block of code using white space. This is what your code should look like. You can ignore what the program actually does and just see how it looks.

 For x = 0 To 10     If x > 5         Print "x is greater than 50; it's equal to " + x + "."         If x > 7             Print "Wow, x is really high, it's " + x  + " ."         EndIf     Else         Print "Too bad, x is less or equal to 50."     EndIf Next 

Not a bad looking format, eh? It's pretty easy to figure out which If statement goes with which EndIf statement, right? Well, the next example is the same code with absolutely no white space. Try understanding it now!

 For x = 0 To 10 If x > 5 Print "x is greater than 50; it's equal to " + x + "." If x > 7 Print "Wow, x is really high, it's " + x + " ." EndIf Else Print "Too bad, x is less or equal to 50." EndIf Next 

This code is much harder to understand. If you wanted to actually comprehend its meaning, you would have to look closely and try to follow through the If commands. Now, imagine this code block was 10 or 15 times the length of the one I just showed you (programs can commonly grow that large in big games ). It would be terribly difficult and a waste of time to try to understand all that code!

One thing you should know: adding white space does not affect the output of the program at all. There will be neither extra spaces nor new lines anywhere in the output of your program. See Figures 4.2 and 4.3 to see the outputs of both programs: white space and no white space.

Figure Figure 4.2 and Figure 4.3. Output of programs with white space (left) and without white space (right).


[ LiB ]


Game Programming for Teens
Game Programming for Teens
ISBN: 1598635182
EAN: 2147483647
Year: 2004
Pages: 94
Authors: Maneesh Sethi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net