Created
July 15, 2014 17:56
-
-
Save Crydust/01ffc25ee8fa242beb2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String foo(boolean p1, boolean p2, long p3) { | |
String result = "a"; | |
if (p1) result += "b"; | |
if (p2) result += "c"; | |
if (p1 && p2 && p3 < 5L) result += "d"; | |
if (p3 == 1L) result = "e" + result; | |
result = "f" + result; | |
return result; | |
} | |
private String bar1() { | |
return foo(true, false, 6L); | |
} | |
private String bar2() { | |
return foo(true, true, 6L); | |
} | |
// Step 0. | |
// ============ | |
// make a spreadsheet with all callers and their parameters | |
// +--------+------+-------+----+ | |
// | caller | p1 | p2 | p3 | | |
// +--------+------+-------+----+ | |
// | bar1 | true | false | 6L | | |
// | bar2 | true | true | 6L | | |
// | | true | ? | 6L | | |
// +--------+------+-------+----+ | |
// Step 1. | |
// ============ | |
// refactor foo and run application with -enableassertions | |
public String foo(boolean p1, boolean p2, long p3) { | |
assert p1 == true; | |
assert p3 == 6L; | |
return p2 ? "fabc" : "fab"; | |
} | |
// Step 2. | |
// ============ | |
// remove unneeded parameters and alter all callers | |
public String refactoredFoo(boolean p2) { | |
return p2 ? "fabc" : "fab"; | |
} | |
private String bar1(){ | |
return foo(false); | |
} | |
private String bar2(){ | |
return foo(true); | |
} | |
// Step 3. | |
// ============ | |
// get rid of boolean parameters and use enums instead | |
public enum ImportantChoice { | |
OMIT_C, INCLUDE_C; | |
} | |
public String refactoredFoo(ImportantChoice p2) { | |
return p2 == INCLUDE_C ? "fabc" : "fab"; | |
} | |
private String bar1() { | |
return foo(OMIT_C); | |
} | |
private String bar2() { | |
return foo(INCLUDE_C); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment