-2

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)?

5
  • Sure, if that's what you like. Just be consistent or it may be confusing which represents true. May 22, 2020 at 3:51
  • 10
    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. May 22, 2020 at 5:10
  • 3
    Yeah. If I saw that name, I'd assume that you've done something like this elsewhere in the code 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_2d without consulting anyone (or feeling the need to do so) and check the code back in. May 22, 2020 at 6:14
  • 1
    BTW, this may be a symptom of organizational and/or design problems. It's important that variable names aren't misleading, but if you feel that the code should communicate more contextual info to the programmers reading it, you have several options - you can add a short helpful comment next to or under the else clause (e.g. // 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. May 22, 2020 at 12:45
  • @FilipMilovanović: or an enum
    – jmoreno
    May 23, 2020 at 3:25

3 Answers 3

3

Or in the name of a Boolean is fine when the Boolean indicates that one of two things is true.

Calling a Boolean a_or_b when it actually means a and not b that is absolutely terrible and misleading. In your example, call it is_2d_andnot_3d if you feel the need; I wouldn’t.

You might say is_2d_vs_3d. Or you could just use an enum with two cases, that would be the best.

10

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.

2
  • 1
    For the typical developers, "or" means that "true" applies to both possibilities, In your case, that would lead to a huge mis-understanding, i.e. "true" applying to 2D as well as 3D, only excluding 1D, 4D etc. May 22, 2020 at 14:25
  • I was following the convention where in a is_a_or_b structure, true means a and false means b. But your argument makes sense and enum is probably a way better choice May 23, 2020 at 6:32
7

The name with or is ambiguous for me. What would true mean in that case? I'd probably interpret it as being either if that made sense or experience a pronounced uptick in WTFs per minute otherwise.

Either stick to is_something or use an enum if it's available in your language and an unambiguous boolean name is not possible.

1
  • 4
    It’s actually not ambiguous, it’s completely misleading according to what OP says it should mean.
    – gnasher729
    May 22, 2020 at 7:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.