Refactoring
to
maintainability

Negation

2023-08-25
Thinking

The setup

Sometimes, when I write (or speak) quickly, some (or all) the nuance of what I am trying to express gets lost. Which is why I prefer to take my time listening and responding to avoid not expressing myself correctly. Nothing good comes from communicating without thinking.

As an example we have this Linkedin Post by Daniel Moka. My contribution barely manages to make the point (or maybe not all)

So, give me a bit more of time, and you get this post.

The Disclaimer

I 100% agree with Daniel Moka’s comment about avoiding those double negatives. They are difficult to read.

Syntax

Syntax can make things easier or difficult to read.

if (!notValidUser) {}

with ! being read as not. That makes the sentence to read if not notValiduser. Which sounds strange.

A language like python straight up uses the word not:

if not not_valid_user:
    pass

which makes even more visible the issue.

But the name for the logical operation is negation. not is a nice short name. So, what if we use our own named function?:

if (negate(notValidUser)) {}

Now it becomes slightly easier to read and understand. We can even go a bit further: negation is an inversion of logical value, so what if we change the name of that function?

if (invert(notValidUser)) {}

To me that helps in understanding that I have a truth table that I need to process

notValidUser invert
F T
T F

which is something I am used to (when debugging conditional that weren’t very clear in the past I always resorted to checking the truth table).

Though, clearly hiding that somewhere and using

if (isValidUser) {}

is easier in English

Double negative

And that brings me to the other point. Languages are varied.

When I was working in Spain, at the beginning of my career, we were mixing Spanish and English in our code. Any function/method/object that we created was in Spanish, while the programming language and their libraries were in English.

When reading something like

if !(usuarioNoEsValido)

the effect is not the same as with the only english version, because the separation of the actual languages was creating a separation in the mental processing of such lines. I was reading that more like the example with negate/invert. I would process first the meaning of usuarioNoEsValido and then process the if !. So the recommendation that started this post wasn’t really as necessary (or at all). The language separation made it superfluous.

Double negation in QueenKing’s English does an inversion (two negatives become a positive). But that is not necessarily the case in other languages (or even dialects of English). In English you can either say I know nothing or I do not know anything. But in Spanish you would say Yo no se nada, which would literally translate as I do not know nothing. Even English versions on the South of USA are mixed with French grammar, and you can get the double negation that does not invert.

Languages modify the way we think (check this TED talk for example, or this Psychology Today article)

What my seem strange to one person, to another (due to a different context) may seem normal


Return to Index

Unless otherwise stated, all posts are my own and not associated with any other particular person or company.

For all code - MIT license
All other text - Creative Commons Attribution-ShareAlike 4.0 International License
Creative Commons BY-SA license image