DEV Community

Discussion on: Use strong types instead of bool parameters

Collapse
 
pgradot profile image
Pierre Gradot • Edited

I guess the exception is not where you wanted it to be. Maye in a default label?

Adding -Wswitch force to cover all cases of all enumerations. This has consequences, especially in existing code base. It would be OK for a new project.

For a simple if/else with a single bool parameter, using a switch case would be fine. But there are other cases, more complex, where the solution won't be that easy.

Examples:

void searchLocation(bool accessible) {
    puts(accessible ? "yes it is" : "no it isn't");
}

bool function(bool A, bool B, bool C) {
   return (A and B) or (not B and C) or (not A);
}
Enter fullscreen mode Exit fullscreen mode

I think you get my point: a two-value enum isn't a boolean. It can be nice in public API. It's not always friendly for implementers.

Thread Thread
 
sandordargo profile image
Sandor Dargo

The exception is exactly at the place I wanted it to be. And exactly because of the warnings it would introduce in existing codebases. As such, I can either make it clearer or actually find and fix/document bugs.

I agree that it's not always straightforward.