Suppose, I have some method that performs some job. In this example, it's going to be void, but it doesn't matter
public void doSomething() {
// doing something
}
But then, let's imagine, I got my team mates complaining that my method, on occasion, doesn't actually do "something". It's because it includes some condition check. It could be a state check or input validation, but since my sample method doesn't take any arguments, let's assume it's the former
public void doSomething() {
if (isCondition()) {
/*
I guess I could ask a separate question
on whether doDoSomething() is good naming
*/
doDoSomething()
}
}
I have two options:
- Document it
- Incorporate that information in the method's name
I recall from Bob Martin's Clean Code that comments should always be seen as a failure — a failure to write code that explains itself. Obviously, I don't want to fail (I want to win, win, win). So I opt for the latter option
public void doSomethingIfCondition()
Then, another change. For whatever reason, I now have to check one extra condition. Since I don't want my method to lie (lying methods is a big no-no in clean code), I now have to change the method name again
public void doSomethingIfConditionOneOrConditionTwo()
That's ugly. But suppose I can somehow group those conditions semantically
public void doSomethingIfSuperCondition() {
if (isSuperCondition()) {
doDoSomething()
}
}
private boolean isSuperCondition() {
return isConditionOne() && isConditionTwo();
}
A spec overhaul. Now, neither "condition one" nor "condition two" matter one bit. It's "condition three" that really does. But here's the problem: my method became quite popular. There are a ton of clients, and it's not feasible to fix them. Or, perhaps, I don't even control all of my clients anymore. It became opensource or something. So the best I can come up with is this
/**
@deprecated use {@link #doSomethingIfConditionThree} instead
*/
public void doSomethingIfSuperCondition() {
if (isSuperCondition()) {
doDoSomething()
}
}
public void doSomethingIfConditionThree() {
if (isConditionThree()) {
doDoSomething()
}
}
I can keep making this story up. The point is, I start to doubt that naming convention. I want to write clean, but trying to do so, in my fictional example, resulted in a pretty nonsensical code. I am not at all sure it's what Bob Martin intended
So, what do you think?
DoSomethingbest way you see fit and dont bother me with condition details"ifConditionDoSomething()? Or better yet,if (condition()) doSomething();This doesn't need to be a separate function; you're just bloating your code.