Why you should consider brushing up on Boolean algebra

Dauvis' Dev Musings

Recently I was making changes to some code that was at least 10 years old. Being so old, the identity of the “guilty” party (or parties) has long since been lost to the sands of time. I cannot even say that I am not said party. 😁 The code in question looked similar to the following block of code.


if (inventTable.FlagField && custTable.FlagField)
{
    this.doSomeProcess();
}

if (!inventTable.FlagField || !custTable.FlagField)
{
    this.doSomeOtherProcess();
}

Think about this code for a moment.  I will wait.

AnimatedClock

What is wrong with the code is that the conditions of the two if statements are logically opposite (see De Morgan’s law). Actually, “wrong” might not be the correct word but it is unnecessarily complex. The block of code can be written as the following.


if (inventTable.FlagField && custTable.FlagField)
{
    this.doSomeProcess();
}
else
{
    this.doSomeOtherProcess();
}

In the field of electronics, people use Boolean algebra to design and to simplify electronic…

View original post 69 more words

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: