Good Programming Practice
Good Programming Practice
1. Save your program regularly. You could lose hundreds of lines because of
accidental typing/mouse click, or a system crash.
2. Organise your code in clearly named folders and sub folders where you know
no one else can access it (U:\) using Explorer.
3. Give your classes names that indicate what they are about. Standard
practice is for class names (and thus the name of your code, eg public
class Drawcircle, Drawcircle.java) to start with an upper case
letter.
4. Similarly give your variables and methods names which say what they are, eg
amountOfChange. Note that standard coding practice is lower case to
start the first word, upper case for any subsequent words. Underscores are
not generally used, but can be.
5. Make your programs easy to read – not only for you. Coding is done by you
now, but in the real world it may be done by many people, and may be
amended long after you have left a company. So:
a. Use spaces to make your layout and thinking clearer. It costs nothing
in terms of compiled code space. Line spaces, and spaces after
commas in lists are both good
b. Use comments wherever you can, not just at the top. If you have a
large number of variables, it can be useful to declare all of them,
together with comments, at the top of a class.
c. Title comments could include program name, your name, date of
coding (or date last updated), what the code does, reference to any
other documentation, other classes you use with this one, shoe size
… too much is always better than too little.
d. There is no agreed standard for using curly brackets, but make sure
you are consistent in whatever layout you prefer. This is typical:
if (cost>50){
g.drawstring(“No change necessary”);
}
Note that the end bracket comes directly under the ‘if’.
If you type one bracket, {, always type its partner, }, before you type
what goes inside. Helps you not to get lost! Some people annex a
comment to the lower bracket to say what the bracket terminates.
This version is also often used:
if (cost>50)
{ g.drawstring(“No change necessary”);
}