Program style Program style does not influence the running of a program. Good style improves the readability and aids in the understanding of program code. Conventions and rules provide consistency between programs. Naming conventions Named constant variables are in upper case letters. Class names start with an upper case letter. Other identifiers, method names start with a lower case letter. Placement of braces, parenthesis and spaces Spaces after commas, after semicolons in for statements, before and after operators, +, =, <=, etc., before { and (, after } and ), (but not before and after the . operator.) * Remember that these are conventions and that they may vary from one programmer to another. For example, ProgramLive and the St George instructor do not include a space before the = operator and before and after ( and ). The important point is that a programmer follows a good, readable style consistently. Braces open brace at the end of a class, constructor or method header statement, close brace aligned with the first letter of the header for the structure it closes. Indentifiers Identifiers should be meaningful. Identifiers that are made up of more than one word start successive words with an upper case letter. Restrict the use of magic constants. Instead define a named constant and use the constants' name in the program. Indenting When you start a new structure, eg. a loop, indent the body of the structure by 4 spaces. When the structure is complete outdent by the same amount. If a statement extends over more than one line indent the second and subsequent lines. Once the statement is complete return to the previous level of indenting. Blank lines Use blank lines to indicate the structure of a program or program fragment. Leave a blank line after instance variables, between methods, before and after a loop or if. Group declarations of variables with the same scope together. Start each statement on a new line. Develop programs in modular reusable parts. Limit the dependency of parts of a program. Encapsulate the parts making instance variables private and restrict access by the use of methods. Use private helper methods to reduce the size of methods and simplify the logic of your program. Comments Comments should not describe program implementation. They should describe what is done, the purpose, not how it is done. Comments should not just repeat the program code. Most program files include header comments that list the name of the programmer, date the program was written and programs' purpose. There is usually a class comment that precedes each class definition describing the purpose of the class. There is usually a method comment that precedes each method describing the purpose of the method. Comments are also used to explain any tricky logic.