Why is "one function do more than one thing" a disadvantage of "boolean parameter", but not in "polymorphism"?

According to Is it wrong to use a boolean parameter to determine behavior?, I know using boolean parameters to decide the behaviour is bad, for example, when using boolean parameters as the following: public void doSomething(boolean condition){ if (condition) { // do something when true } else { // do something when false } } obj.doSomething(true); //some code to call the function I should refactor it into 2 functions: public void doSomethingWhenTrue{ // do something when condition is true } public void doSomethingWhenFalse{ // do something when condition is false } obj.doSomethingWhenTrue(); //some code to call the function And I know it has similar reasons to tell a function with switch over enums is bad : How does this switch statement do "multiple things"?. However, what I don't know is, why is "one function do more than one thing" a disadvantage of "boolean parameters", but not in polymorphism? When I'm using polymorphism, I'm also calling the same function name but potentially resulting different behaviours: public interface Condition { public void doSomething(); } public class True implements Condition { public void doSomething() { // something to do when condition is true } } public class False implements Condition { public void doSomething() { // something to do when condition is false } } When calling doSomething: var condition = new True(); condition.doSomething(); It feels as if the boolean parameter is just moving from right hand side to left hand side only. But I guess you will not say polymorphism is bad because of "one function do more than one thing", right? So my question is, while both "boolean parameter" and "polymorphism" are fundamentally "calling same function name but can have different behaviour", why is it bad when the behaviour is determined by the boolean parameter at right hand side, but not bad (and even good) when it is determined by the object type at left hand side?

May 20, 2025 - 10:50
 0

According to Is it wrong to use a boolean parameter to determine behavior?, I know using boolean parameters to decide the behaviour is bad, for example, when using boolean parameters as the following:

public void doSomething(boolean condition){
    if (condition) {
        // do something when true
    } else {
        // do something when false
    }
}

obj.doSomething(true); //some code to call the function

I should refactor it into 2 functions:

public void doSomethingWhenTrue{
    // do something when condition is true
}

public void doSomethingWhenFalse{
    // do something when condition is false
}

obj.doSomethingWhenTrue(); //some code to call the function

And I know it has similar reasons to tell a function with switch over enums is bad : How does this switch statement do "multiple things"?.

However, what I don't know is, why is "one function do more than one thing" a disadvantage of "boolean parameters", but not in polymorphism? When I'm using polymorphism, I'm also calling the same function name but potentially resulting different behaviours:

public interface Condition {
    public void doSomething();
}

public class True implements Condition {
    public void doSomething() {
        // something to do when condition is true
    }
}

public class False implements Condition {
    public void doSomething() {
        // something to do when condition is false
    }
}

When calling doSomething:

var condition = new True();
condition.doSomething();

It feels as if the boolean parameter is just moving from right hand side to left hand side only. But I guess you will not say polymorphism is bad because of "one function do more than one thing", right?

So my question is, while both "boolean parameter" and "polymorphism" are fundamentally "calling same function name but can have different behaviour", why is it bad when the behaviour is determined by the boolean parameter at right hand side, but not bad (and even good) when it is determined by the object type at left hand side?