I'm often presented with these two options. From a design point of view, which of these is subjectively better, and why?
Option A
class foo {
private boolean bar() {
//Stuff
}
private void A() {
if(bar())
B();
}
private void B() {
// more stuff
}
}
Option B
class foo {
private boolean bar() {
//Stuff
}
private void A() {
B();
}
private void B() {
if(!bar())
return;
// more stuff
}
}
Let's assume that code duplication isn't an issue here, and the division is only to separate, logically, the code for better readability/understanding (A, B, and bar have meaningful names in context). Is it more readable to have the reader know that B might not do anything when looking at just A? Or is the encapsulation of state better put into B? Obviously, this can be refactored to separate B and bar() into a separate class entirely, which separates concerns even more, but assuming mostly shared state, which is better?