Here’s something I notice in a lot of other people’s code I look at, which has always bothered me. This isn’t language-specific — I see it everywhere.
Programmers create logical expressions with the boolean operators And, Or, and Not. It seems to require a sort of sideways view of things to apply the additional operators that work with boolean values, Eqv and Xor (or to use their non-bitwise counterparts which more people might recognize offhand, = and != (<> in LotusScript).
This neglect often results in inefficient and hard-to-read logical expressions. For instance, suppose the code needs to test the assertion: “Either a and b are both 0, or neither is 0.” I often see such a test written as:
If (a = 0 And b = 0) Or (a <> 0 And b <> 0) Then
Instead, we could write this:
If (a = 0) = (b = 0) Then
People reading this code and seeing the admittedly unusual way we’re using the = operator, might do a double take. But it shouldn’t be unusual. It’s completely legit, takes fewer comparisons, and, once you get that = can also compare logical values, easier to verify that the expression is doing what you intended.
In LotusScript I could’ve used Eqv here instead of = and gotten the same result, but a lot of people would’ve had to look up Eqv, whereas they surely already know what = does.
Another weird thing I see people doing with boolean values, is using an unnecessary test for equality, e.g.:
If isFixed = True Then
So they plainly realize the equality test is legitimate on booleans, but here it wasn’t necessary — one can just write:
If isFixed Then
I suppose the programmer in this case felt, I don’t know, it would look a little naked without an operator? They thought it added clarity?
I think the way to add clarity in expressions such as this is to name your variables and functions in a way that makes it clear they’re logical values. If the name starts with “is” or “has” it’s pretty clear. I also tend to use the prefix “b” to indicate a Boolean variable.
Anyway, just something to think about when coding.