Showing posts with label clean code. Show all posts
Showing posts with label clean code. Show all posts

Tuesday, June 23, 2015

Negative logic, it's not great

Early in my programming efforts I learned that negative logic can and should be avoided.  Many developers don’t think twice about introducing negative constructs into a class - I think that is a mistake.

I'm going to use the following code snippet as an example.  It is from a well known open source project.  The goal of the method is to return a list of classes in a given class' hierarchy, excluding the Object class. It has an interesting negative condition that drives a while statement - I think it can be improved.

I believe negative logic suffers from the following issues:

  • Difficult to read / conceptualize.
    The while statement is difficult to understand Object.class is not equal to cls and it is not null?
  • Negative symbols are easily missed. Java's negation symbol of ! is easy to overlook - especially when used at the start of the condition: !Object.class.equals(cls);
  • Difficult to combine negative conditions. For example what is wrong with this condition (x != 1 || x !=3)? You need to swap “or” (||) for “and” (&&). This is a very common programming error, all values are not equal to 1 or 3.
  • Expands the number of test cases needed to verify correctness. There are many classes that are not equal to Object.class, only one that is equal.
Refactoring this condition to positive improves the readability.  Extracting the condition to a method allows me to give it a name.

There are cases were negative logic works better because it can be less verbose and concise.  However, take some time to evaluate your negative conditions and see if they can be refactored to positive.

Saturday, November 1, 2014

Your Great Code

Great code does not come from code reviews, code quality reports, using the latest tool or programming language.  Great code is built by developers who care about their work…

Where does this passion for code come from?  It comes from within.  Somewhere in your past this attitude or culture was impressed upon you. It makes up your value system, your personal philosophy.   

My journey began working on my first car with my Dad.  My feeble attempts to tape and rig my car into working order didn’t work.  Most of my repair attempts just made things worse.  Dad impressed on me two lessons;


  • If something is worth doing, it is worth doing right
  • Finding the correct way to fix a problem is always the best solution

We purchased a shop manual for the car and made regular trips to junk yards. Understanding the mechanics of the car and obtaining the correct parts made a big difference in my repairs.


I attended a small liberal arts college in Rome, GA - Berry College. All students were encouraged to work on campus.  My job was helping our stonemason in the repair and caring of the stone walkways and walls that made up parts of the campus.  I mixed concrete and hauled stones.

I learned to mix concrete and select the right stones for a repair. I also discovered that helping getting the correct slope on a walk way so rain doesn't pool imparted a degree of satisfaction. I learned the value of work done well.

Berry has a student code, during my college days it seemed like a relic from the past.  Time has helped me understand its wisdom.


"I promise not to be content with slip-shod or merely passable work.  I will take an interest in all my work and learn to do the right thing in the right way".
- The Berry Code circa 1920

How did you learn to care about your code?