“not” – Think Positive

Self help books never get tired of singing praise of positive thinking.

I have investigated why positive thinking succeeds. I have realized that it is much easier to think positive than to think negative.

If that is also your opinion, what do you think of all the !’s spread in the code? Is it compassionate to the maintainer to force to think in negative term?

Also, think about all the double negatives! “If the customer is not “not logged out”, “

Theoreticians will jump with a gun in their hand for me! How can I think of logical system without a “not”! Forget about Boolean algebra, even a Group structure can’t stand without an inverse! Have I gone nuts?

The world has come a long way from Leibnitz like notations to express logic. The same Boolean logic can be expressed without explicit not in most cases. For example:

if (not )
else

can be easily converted into

if ()
else

That way, even if your hardware design has negative fields, your code does not need to go double negative.

Many of us will remember the famous negative:
while (!feof(fp))

This idiom has been so ingrained in our thinking, one of our very good engineer used it against my proposal. His main line of argument was:

“Use ‘not’ if it falls in normal language – just like what we do in case of feof.”

I say it is reverse case! feof is an example of how lower layer convenience went up the problem domain. If I were K&R, feof was the best way to avoid additional logic because eof was so detectable a condition!

How do we really think while we debug in this case?

“How does the software behave when data is available?” In other words, we think of “fif” (file pointer in file”).

Frankly, I am convinced a little care to reflect the problem domain language could avoid a lot of inverted logic.

The only case where I find “not” useful is in cutting short error handling i.e. in situations where “catch” is unavailable. For example, if you want to know whether an object existed in Tcl, rather than coding:

if { [ info exists x ] } {

# action of 100 lines

} else {

# error handling

}

and leaving the maintainer guessing about the error handling, we can error handle it before and terminate his interest earlier on. It will be only a few lines to skip if he is interested in what happens if x existed.

if { ![ info exists x ] } {

# error handling

} else {

}

Programming by contract will cut this kind of negative logic significantly.

Someday I may write why it is so difficult to think negative. However, that is not current focus.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s