Page 369 - Introduction to Programming with Java: A Problem Solving Approach
P. 369
groupings. On any of the cards you can change any of the wordings in either of the two panes. If you decide that one of the classes is no good, you can click on its X box to throw it away, reactivate the Command Prompt window, and create more new CRC cards with different class names. When you’re through with everything, use Ctrl-PrtScr to print the screen to record your thinking, and then enter ‘q’ in the Command Prompt window to terminate the program.
Summary
• Begin every class with a prologue. Include program name, author(s), and a brief description of what the class does.
• Provide a descriptive comment above or after any code that an experienced Java programmer would not understand.
• Use meaningful names for everything. Do not be cryptic.
• Enclose logical blocks of code in braces. The opening and closing braces should be in the same column
as the start of the line that precedes the opening brace.
• Supply a // end <block-name> comment after a block’s closing brace to improve readability.
• Declare each variable at the beginning of its class or method, or in its for loop header. Normally use
one line per variable and follow each declaration with an appropriate descriptive comment.
• Use subordinate helper methods to simplify large methods and reduce code redundancy. Make helper
methods private to minimize clutter in the class interface.
• Use instance variables for object attributes (state information) only. Use local variables and input pa-
Apago PDF Enhancer
rameters for calculations within a method and to transfer data into a method. Use return values and/or
input reference parameters to transfer data out of a method.
• Plan to test the software you develop frequently and thoroughly as you go along. Include typical, bound-
ary, and unreasonable cases.
• Top-down design is appropriate for large projects that have well-understood objectives. Proceed from
general to specific, using stubs to defer implementation of subordinate methods.
• Bottom-up design allows you to give priority to critical details. It fosters re-use of existing software, which reduces development cost and improves system reliability. But this methodology makes large
projects hard to manage.
• Expect to go through several design iterations. Use prototyping to help customers get a clearer under-
standing of what they want, but avoid the trap of trying to convert a clumsy prototype directly into a final product. In each subsequent iteration, select that design strategy which best addresses the greatest current need or concern. A successful program will require ongoing maintenance, and you can make this easier if you preserve and enhance elegance as the program changes and grows.
• To facilitate modular testing, provide a main method with every class.
• If there is no name ambiguity, you may omit the this prefix when accessing an instance member.
Review Questions
§8.2 Coding-Style Conventions
1. One should avoid inserting blank lines between different code sections (because that leads to wasted paper
when the program is printed). (T / F)
2. In order, list the seven items that we recommend you include in a file prologue.
Review Questions 335