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)?
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 tois_2d
without 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.