|
|
|
|
|
by fizlebit
731 days ago
|
|
The post is right in that the example code is terrible, Uncle Bob's changes make it much worse, but also the suggested fix is also bad. I think this is better, but the whole thing is silly. Obviously you'd want to write a comment for the function that explains its weird behaviour. String getCandidateCountString(char candidate, int count) {
if (count == 0 ) {
return String.format("There are no %ss", candidate);
} else if (count == 1) {
return String.format("There is 1 %ss", candidate);
} else {
return String.format("There are %s %ss", count, candidate);
}
}
There are 3 as. lol |
|
Beginner: I'll just create an if statement with 3 clauses.
Middle: No! that's clearly a ton of duplication - it's bad code!
Expert: I'll just create an if statement with 3 clauses.
And I can see how people get to the middle position. People often tell beginners to eschew duplication. They learn to see any trio of similar-looking lines as a code smell and jump to refactor. It takes a few years of doing that and getting burnt by it before you can really differentiate "this is duplicative and would benefit from refactoring" from "this only looks duplicative, and is actually cleanest with the repetition in place."