Most beginner programmers have a bit of trouble with formatting their code. They aren’t consistent and when they are told to do it a particular way, they don’t like it. Experienced programmers understand that it has no effect on the actual program and it’s pure preference however it does make a statement as to how others view your code.
I put together a brief Java Formatting guide for beginners (only formatting is explained, variables, methods, structures are not) so they have a reference to use. Keep in mind that these are mere suggestions and are not universal, mandatory.
- Single line comments should have 1space between the last slash and the first word.
- The open and close of a multi-line comment should have no indentation while the contents should have the indentation of 1 tab.
- import statements do not have formatting.
- The class declaration (public class ClassName) does indentation.
- Any occurrence of variable { or ) } should be separated by 1 space.
- Each method in a class should have line of white space before another structure.
- A method’s declaration should be indented by 1 tab. The last ) and the first { of the method should be separated by 1 space.
- Things inside of the method should be indented by 1 space.
- If a multiple statements are on a single line, there should be a space after each semi-color.
- Declaration lists (int var1, var2, var3) should be separated by a comma followed by a space.
- Declaration lists that also initialize values should have a space on both sides of the assignment operator and also follow the comma space rule for declaration lists.
- A variable name should be lowercase letters if it’s only a single word.
- For two word variables (theCat, myMoney, somethingElse) the first letter of the first word is left lowercase and the first letter of the second word is capitalized.
- For multi-word variables, use the same format leaving the first word’s first letter lowercase but thereafter each first letter of each word should be capitalized. (This is called camelcase.)
- When concatenating a string, there should 1 space between any occurrences of “, + or variable name. (String n = “hello” + “world”;)
- if statements do not have an initial indentation.
- All operators inside of a conditional statement should have 1 space on their left and their right.
- Numbers (in other words, not variables) should always be listed first in a conditional. (10 == variableName)
- Things inside of an if statement should indented by 1 tab.
- If an if statement also has an else branch, the else should should start one space after the if branch’s last } and the first bracket of the else should be once space after the keyword else.
- In a switch statement, there should be 1 space between the colon and the next statement.
- A for, while or do loop should all have 1 their contents indented by 1 tab.
- There is no formatting for the for, while or do loop declaration excepting noting the conditional statement rule and the ) { rule.
- When declaring an array, the [] always go on the variable type.
- When declaring and initializing an array, the values should be separated by a comma followed by a space..
Do you know any other structures in java that may need special formatting rules? If so, leave a comment.
Additionally, you can download the source code of a program that show cases these formatting rules.
Once again, I don’t advocate enforcing formatting because I don’t even format things myself. I use the auto-formatter in NetBeans and if not then prettyprinter. Most respected PHP formatters can format java without much problem too.