|  | 
 
 
Good programming style is nothing you can measure, really. More of an art, than a science -- but isn't that true
for most aspects of game programming?
 
With good programming style I mean quality in all the areas you really don't see except when looing at the source code. Programming
style covers syntax, patterns used, the structure you break your program into. But why even bother if there's no difference in 
the actual executed game?
 
 
 Some other important issues however are not directly connected to programming style: 
 These concepts are not exclusive to games programming, and certainly not exclusive to Blitz Basic. Some things you find to 
implement easier in a certain language, for other things there are no "native" constructs; but if the language supports some 
basic features, the lack of certain constructs won't keep you from implementing your own.
 BASIC flavors (like Blitz Basic, or QBasic),in my opinion, are ideally suited for a good programming style: 
 
 Some BASIC flavors don't support OOP, Object Oriented Programming. Natively, that is. Others, like Microsoft Visual Basic 6 (and soon, Visual Basic .Net), include the concept of classes and events. Spacing First, I think practically everybody does it, tabbing or spacing: Not too good (example without spacing): Better (example with spacing): The structure here is much more obvious. Explicit names 
 
If you didn't hear of magic numbers, you certainly experienced them. It's those numbers used everywhere within 
the program where there's no clear explanation to them.
 Not too good (example with magic numbers): Better (example without magic numbers): 
You might also use a way to test the boundaries of the array you iterate through if you want to make the loop boundaries more
obvious.
 
 
 Vertical spacing You can use double returns to more clearly seperate parts of the program. (There are no easy rules I would know for that, like there are in horizontal spacing/ tabbing.) Avoiding abbreviations Abbreviations might save time when writing a line of code (depending on the programming enviroment and its auto-completion features, or your capabilities of making use of copy-and-paste shortcuts, it might not). But it might cost a lot of time writing a long program, since readability suffers. Especially for other people reading your program; or yourself, when you dig through older code. What might seem obvious to you might not seem obvious to others; or even your future self. Not too good (example with abbreviations): Better (example with verbose variables): You might make rare exceptions from the rule of avoiding abbreviations; there are idioms, variable-names every programmer might know, where the verbose version would decrease readability. (Think the "for i" loop.) Mixed case 
Variable names should be written in mixed case if they consist of several sub-words. The first letter of each word which comes 
in the middle of a variable name should be written in upper-case.
 Sometimes, mixing case might hint that using types might be even better. So speedx might become speedX, which might later become speed\x. In general, build-in commands of the language will be formatted by the programming environment you use. Typing endif might result in EndIf. Not too good (example with all lowercase): Better (example with mixed case): Of course, every good program is split up in sub-functions. They will enhance reusability and also make the program easier to understand. The smaller and more precise a function, the higher the chance you can reuse it, even from within slightly different contexts. 
There is no easy rule-of-thumb as to how long a function could be, but if you have to scroll your function to see it completely
on a 1024*768 resolution... you might ask yourself if you can find ways to break it up into more functions.
 Not too good (pseudo-code example with overlong function): Better (pseudo-code example with small, precise functions): Note that the variables used in the three different parts have a shared variable scope in the first, bad example, and a seperated variable scope in the second, better example. Seperating variable scope clearly will prevent errors like wrong initial values of variables, or having to use alternative variable names. To see another bonus of this approach, imagine you would need to use "do this" again: 
 
 Phil donated this tutorial from his website, http://www.outer-court.com/basic/style.htm. Please be sure to check out Phil's site for other great resources and information! 
 For a printable copy of this article, please click HERE. 
 This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.   |