Computer Science 15-100 (APEA Section E), Summer 2008
Homework 6
Due: Tue 29-Jul-2008 at 5:30pm (online submission and physical copy)
(no late submissions accepted).
Here is a non-exhaustive list of style issues you should deal with:
* Use top-down design, writing well-chosen helper methods where appropriate.
* Do not duplicate code.
* Use test methods and test particularly for boundary cases. For Tetris, it would be easy to do this. For example, set up a piece by hand, make it rotate, then confirm that the resultant piece matches expectations. Or, set up a board manually (I’d suggest making the board smaller than the 22x10 used in the “real” game), and call the various doLeft, doRight, etc, methods, confirming after each one that the piece’s resultant location is correct. In any case, you must test, test, test your code!
* Use appropriate instance vs. local variables. Instance variables should be limited to those variables that must be instance variables because their values simply must persist beyond a method call. The classic example: setting instance variables in response to timer, keyboard, and mouse events so that the ensuing paint methods can refer to them.
* Use well-named variables, methods, and classes. Besides the obvious (eg, someone named a variable “rowCount” even though it had nothing to do with counting rows – bad name!), it is also true that smaller details matter. For example, class names should all start with an Uppercase letter and variable and method names should not.
* Use proper indenting and spacing. No triple-spaced lines (double-spaced at most, and then sparsely). Single space after “if”, “for”, “while” and before the parenthesis. Single space after closing parenthesis and ensuing the left-brace. Etc.
* Use reasonable commenting. Not too much, not too little. Comment anything that may be confusing or unexpected to an experienced programmer reading your code for the first time. Do not overcomment. Be brief and to the point.
* Do not use magic numbers! Our safe rule-of-thumb: any number except -2, -1, 0, +1, +2 belongs in a well-named variable. As we discussed in class, there are some reasonable exceptions to this rule, but to be safe, you can always follow the rule strictly and you’ll be fine.
There are more style rules we have discussed, but these cover most of the bases.
Carpe diem!