Say we have a boolean variable indicating whether an input array is 2D or 3D (just an example). Is it acceptable to name it is_2d_or_3d instead of is_2d, to make it clearer that if false, it indicates the input is 3D?
I've personally been doing this for quite some time. I don't remember if I picked it up from some guidelines or just thought of it myself. Is there any conventions suggesting this? If not, are there any drawbacks of this approach (besides not being recommended by guidelines)?
Seems like you really want an enum with two possible values 2D and 3D.
Booleans only make sense when it's obvious what true and false mean: questions that can be answered with yes/no, on/off etc. is_2d is okay if all you care about is whether it is 2D or not, but it's not obvious that false means 3D. is_2d_or_3d is even more confusing cause it's not clear which option maps to true and which maps to false.
Is it acceptable to name it is_2d_or_3d instead of is_2d, to make it clearer that if false, it indicates the input is 3D?-- A variable so named would lead me to believe that true indicates that the state is either 2d or 3d, and not some other state; and that false indicates that the state is neither 2d nor 3d.
is_2d_or_3d = is_2d OR is_3d. And in a team setting, if I discovered that's not what the variable means, I would immediately change all occurrences to
is_2dwithout consulting anyone (or feeling the need to do so) and check the code back in.
// it's a 3D array), or you can extract the bodies of the if branches into separate methods and give them descriptive names (
if (is_2d) Handle2DArray(arr); else Handle3DArray(arr);). You can also potentially push the check elsewhere and use polymorphism.