-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathPointlessForwardingMethod.java
More file actions
41 lines (36 loc) · 1.21 KB
/
PointlessForwardingMethod.java
File metadata and controls
41 lines (36 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import static java.lang.System.out;
public class PointlessForwardingMethod {
private static class Bad {
// Violation: This print does nothing but forward to the other one, which is not
// called independently.
public void print(String firstName, String lastName) {
print(firstName + " " + lastName);
}
public void print(String fullName) {
out.println("Pointless forwarding methods are bad, " + fullName + "...");
}
}
private static class Better1 {
// Better: Merge the methods, using local variables to replace the parameters in
// the original version.
public void print(String firstName, String lastName) {
String fullName = firstName + " " + lastName;
out.println("Pointless forwarding methods are bad, " + fullName + "...");
}
}
private static class Better2 {
// Better: If there's no complicated logic, you can often remove the extra
// variables entirely.
public void print(String firstName, String lastName) {
out.println(
"Pointless forwarding methods are bad, " +
firstName + " " + lastName + "..."
);
}
}
public static void main(String[] args) {
new Bad().print("Foo", "Bar");
new Better1().print("Foo", "Bar");
new Better2().print("Foo", "Bar");
}
}