|
Java Programming Style Guide |
IntroductionThe Java language gives you all the room you need to write code that would be very difficult for others to understand. Java also permits you to write code that is very easy to understand. Most development teams would prefer the latter. A style guide provides provides a map so that the code generated by a group of programmers will be consistent and, therefore, easier to read and maintain. Many people do not care for the style guide offered by Sun. This document is one alternative. This document covers most areas where there could be confusion or difference of opinion. Areas that have never been a problem in our experience are undocumented, but we try to provide at least one example to demonstrate proper use somewhere in the document. 1 - Formatting1.1 - IndentationAll indenting is done with spaces, not tabs. All indents are four spaces.Reasoning: All programs work well with spaces. Most programs will mix tabs and spaces so that some lines are indented with spaces and some with tabs. If your tabbing is set to 4 and you share a file with someone that has tabbing set to 8, everything comes out goofy. Matching braces always line up vertically in the same column as their construct.example:
void foo()
{
while (bar > 0)
{
System.out.println();
bar--;
}
if (oatmeal == tasty)
{
System.out.println("Oatmeal is good and good for you");
}
else if (oatmeal == yak)
{
System.out.println("Oatmeal tastes like sawdust");
}
else
{
System.out.println("tell me pleeze what iz dis 'oatmeal'");
}
switch (suckFactor)
{
case 1:
System.out.println("This sucks");
break;
case 2:
System.out.println("This really sucks");
break;
case 3:
System.out.println("This seriously sucks");
break;
default:
System.out.println("whatever");
break;
}
}
All if, while and for statements must use braces even if they control
just one statement.
Reasoning: Consistency is easier to read. Plus, less editing is involved if lines of code are added or removed.
if (superHero == theTick) System.out.println("Spoon!"); // NO!
if (superHero == theTick)
System.out.println("Spoon!"); // NO!
if (superHero == theTick) {
System.out.println("Spoon!");
} // NO!
if (superHero == theTick)
{
System.out.println("Spoon!");
} // YES!
1.2 - SpacingAll method names should be immediately followed by a left parenthesis.
foo (i, j); // NO!
foo(i, j); // YES!
All array dereferences should be immediately followed by a left square bracket.
args [0]; // NO!
args[0]; // YES!
Binary operators should have a space on either side.
a=b+c; // NO!
a = b+c; // NO!
a=b + c; // NO!
a = b + c; // YES!
z = 2*x + 3*y; // NO!
z = 2 * x + 3 * y; // YES!
z = (2 * x) + (3 * y); // YES!
Unary operators should be immediately preceded or followed by their operand.
count ++; // NO!
count++; // YES!
i --; // NO!
i--; // YES!
Commas and semicolons are always followed by whitespace.
for (int i = 0;i < 10;i++) // NO!
for (int i = 0; i < 10; i++) // YES!
getPancakes(syrupQuantity,butterQuantity); // NO!
getPancakes(syrupQuantity, butterQuantity); // YES!
All casts should be written with no spaces.
(MyClass) v.get(3); // NO!
( MyClass )v.get(3); // NO!
(MyClass)v.get(3); // YES!
The keywords if, while, for, switch, and catch must be followed by a space.
if(hungry) // NO!
if (hungry) // YES!
while(pancakes < 7) // NO!
while (pancakes < 7) // YES!
for(int i = 0; i < 10; i++) // NO!
for (int i = 0; i < 10; i++) // YES!
catch(TooManyPancakesException e) // NO!
catch (TooManyPancakesException e) // YES!
1.3 - Class Member Ordering
class Order
{
// fields (attributes)
// constructors
// methods
}
1.4 - Maximum Line LengthAvoid making lines longer than 120 characters. If your code starts to get indented way to the right, consider breaking your code into more methods.Reasoning: Editors and printing facilities used by most programmers can easily handle 120 characters. Longer lines can be frustrating to work with. 1.5 - ParenthesesParentheses should be used in expressions not only to specify order of precedence, but also to help simplify the expression. When in doubt, parenthesize.2 - IdentifiersAll identifiers use letters ('A' through 'Z' and 'a' through 'z') and numbers ('0' through '9') only. No underscores, dollar signs or non-ascii characters.2.1 - Classes and InterfacesAll class and interface identifiers will use mixed case. The first letter of each word in the name will be uppercase, including the first letter of the name. All other letters will be in lowercase, except in the case of an acronym, which will be all upper case.Examples: Customer 2.2 - PackagesPackage names will use lower case characters only. Try to keep the length under eight (8) characters. Multi-word package names should be avoided.Examples: common 2.3 - All Other IdentifiersAll other identifiers, including (but not limited to) fields, local variables, methods and parameters, will use the following naming convention. This includes identifiers for constants.Reasoning: Using all upper case, as traditionally done in C, is a violation of OO abstraction. For example, a variable which starts out as a constant may be refactored later to not be a constant. The first letter of each word in the name will be uppercase, except for the first letter of the name. All other letters will be in lowercase, except in the case of an embedded acronym, which will be all uppercase. Leading acronyms are all lower case.Examples: customer 2.3.1 Hungarian Notation and Scope IdentificationHungarian notation and scope identification are not allowed.Reasoning: Hungarian Notation, which specifies type as part of the identifier, violates OO abstraction. Scope identification specifies scope as part of the identifier, which also violates OO abstraction.
public class Person
{
private String sFirstName; // NO! (Hungarian notation: s for String)
private String firstName; // YES!
private String mLastName; // NO! (Scope identification: m for member variable)
private String _lastName; // NO! (Scope identification: _ for member variable)
private String lastName; // YES!
// ...
}
2.3.2 Test CodeTest code is permitted to use underscores in identifiers for methods and fields.Code for testing other code often needs to be able to refer to existing identifiers, but also be able to append other qualifiers to the name. This is easier to read if an additional delimiter is allowed.
double eatSomePie(double amount) may use variables such as:eatSomePie_counteatSomePie_amounteatSomePie_returneatSomePie_exception3 - Coding3.1 - Constructs to Avoid3.1.1 - Never use do..whileDo not usedo..while loops.
Reasoning: Consider that the programmer looking at your code is probably examining each method starting at the top and working down. When encountering a loop, the first thing the programmer wants to know is what terminates the loop. If you have that logic at the bottom, it is harder to read. Further, many less experienced programmers are not familiar with do..while, but may be required to modify your code. So rather than:
boolean done = false;
do
{
...
} while (!done)
use:
boolean done = false;
while (!done)
{
...
}
3.1.2 - Never use
|