Java Reference
In-Depth Information
54
System.out.println("underweight");
55
} else if (bmi < 25) {
56
System.out.println("normal");
57
} else if (bmi < 30) {
58
System.out.println("overweight");
59
} else { // bmi >= 30
60
System.out.println("obese");
61
}
62
}
63 }
This solution interacts with the user the same way and produces the same results
as the unstructured solution, but it has a much nicer structure. The unstructured pro-
gram is in a sense simpler, but the structured solution is easier to maintain if we want
to expand the program or make other modifications. These structural benefits aren't
so important in short programs, but they become essential as programs become
longer and more complex.
Procedural Design Heuristics
There are often many ways to divide (decompose) a problem into methods, but some
sets of methods are better than others. Decomposition is often vague and challenging,
especially for larger programs that have complex behavior. But the rewards are worth
the effort, because a well-designed program is more understandable and more modu-
lar. These features are important when programmers work together or when revisiting
a program written earlier to add new behavior or modify existing code. There is no
single perfect design, but in this section we will discuss several heuristics (guiding
principles) for effectively decomposing large programs into methods.
Consider the following alternative poorly structured implementation of the single-
person BMI program. We'll use this program as a counterexample, highlighting
places where it violates our heuristics and giving reasons that it is worse than the pre-
vious complete version of the BMI program.
1 // A poorly designed version of the BMI case study program.
2
3 import java.util.*;
4
5 public class BadBMI {
6
public static void main(String[] args) {
7
System.out.println("This program reads data for one");
8
System.out.println("person and computes his/her body");
9
System.out.println("mass index and weight status.");
10
System.out.println();
11
12
Scanner console = new Scanner(System.in);
13
person(console);
 
Search WWH ::




Custom Search