Page 797 - Introduction to Programming with Java: A Problem Solving Approach
P. 797
int doIt()
{
}
<statements>
{
}
while (...)
{
}
<statements>
Appendix 5 Java Coding-Style Conventions 763
3. Brace alignment is a contentious issue. Sun’s Java Code Conventions Web site recommends putting the opening curly brace at the end of the previous line. This is one place where this document’s conventions diverge from Sun’s conventions. We recommend that you put the opening curly brace on its own line because that helps make compound statements stand out.
4. For empty-bodied constructors, place the opening and closing braces on the same line and separate them with a space, like this:
public Counter()
{}
Theelse ifConstruct
1. If the body of an else is just another if, form an else if construct (put the else and the if on
thesameline).Seetheabovebraceplacementsectionforanexampleofaproperelse ifconstruct. Alignment and Indentation
1. Align all code that is logically at the same level. See the above brace placement section for examples of proper alignment.
example:
for (...)
2. Indent all code that is logically inside other code. That is, for nested logic, use nested indentation. For
Apago PDF Enhancer
3. You may use an indentation width of two to five spaces. Once you choose an indentation width, you should stick with it. Use the same indentation width throughout your program.
4. When a statement is too long to fit on one line, write it on multiple lines such that the continuation lines are indented appropriately. If the long statement is followed by a single statement that is logically inside of the long statement, use braces to enclose the single statement. Use either of the following techniques to indent continuation lines:
• Indent to a column position such that similar entities are aligned. In the example below, the entities that are aligned are the three method calls:
while (bucklingTest(expectedLoad, testWidth, height) &&
stressTest(expectedLoad, testWidth) &&
slendernessTest(testWidth, height))
{
}
numOfSafeColumns++;