Code Quality
     © XKCD.com

Style Rules

When you write a paper for English, a lab report for Chemistry or Biology, a research report for Psychology, or any of dozens of other kinds of submissions for a class you're taking, you're expected to follow some guidelines on how that submission is supposed to look. It needs to show your name and student number. It needs to have a title clearly written on it. It needs certain subsections, and maybe citations in the text. The subsection titles need to be laid out properly, as do the citations and the reference section. If your submission does not follow the guidelines, you may well lose points.

This course also has guidelines for how your submissions need to look. Just as it should be easy to flip thru a report looking for a particular section, it should be easy to flip thru a computer program looking for a particular method or other piece of code. If you ever go on to get a job as a programmer in a company with multiple programmers, you will find that they have a "house style" -- a set of rules for how you should format your code. The computer doesn't care how you format your code, but the people you work with will. Well-formatted code is much easier to read, and therefor much easier to debug. You will be spending a lot more time debugging your code than you expect. If you use poor programming style the task will be that much harder, and take you that much longer. If you use bad style, the grader will have a hard time reading your code, and that will make them grumpy. You do not want a grumpy grader (!), so make sure the code you submit follows the style guidelines. But even before that -- when you ask me to help you find a problem with your code, I will need to read it. If it's not written in a good style, it will be harder for me to find the problem. If I can't find the problem, and the code is a mess, I'll probably tell you to start over again. Thus you should be using good style as you go along.

Different places will have different "house rules" -- it is a matter of style, after all -- but they are broadly similar. Many of the rules I give below will apply wherever you go, but some may be different. Even if you learned a different set of rules in another course (or in a programming job you had before), these are the rules for this course, and you will be graded on how well you follow them.

Naming

Indentation & Line Length

Helpful hint: NetBeans has a Format command that'll fix the indentation and spacing for you. To use the format command, just right-click on a blank part of your code window and select the Format command in the menu that pops up.

The format command doesn't fix the line length, but NetBeans does have a skinny red line running down the right side of the window. It shows you where the 80th character ends -- so don't let your code reach (or go over) that line.

The format command won't line up (all) your comments properly, either, so you'll have to fix some of those yourself. Mostly it's the ones you didn't indent at all and the ones that are at the end of a line after code.

Spacing

Comments

Program Output