Refactoring Day 26 : Remove Double Negative

Today’s refactoring comes from Fowler’s refactoring catalog and can be found here.

This refactoring is pretty simple to implement although I find it in many codebases that severely hurts readability and almost always conveys incorrect intent. This type of code does the most damage because of the assumptions made on it. Assumptions lead to incorrect maintenance code written, which in turn leads to bugs. Take the following example:

   1: public class Order

   2: {

   3:     public void Checkout(IEnumerable<Product> products, Customer customer)

   4:     {

   5:         if (!customer.IsNotFlagged)

   6:         {

   7:             // the customer account is flagged

   8:             // log some errors and return

   9:             return;

  10:         }

  11:  

  12:         // normal order processing

  13:     }

  14: }

  15:  

  16: public class Customer

  17: {

  18:     public decimal Balance { get; private set; }

  19:  

  20:     public bool IsNotFlagged

  21:     {

  22:         get { return Balance < 30m; }

  23:     }

  24: }

As you can see the double negative here is difficult to read because we have to figure out what is positive state of the two negatives. The fix is very easy. If we don’t have a positive test, add one that does the double negative assertion for you rather than make sure you get it correct.

   1: public class Order

   2: {

   3:     public void Checkout(IEnumerable<Product> products, Customer customer)

   4:     {

   5:         if (customer.IsFlagged)

   6:         {

   7:             // the customer account is flagged

   8:             // log some errors and return

   9:             return;

  10:         }

  11:  

  12:         // normal order processing

  13:     }

  14: }

  15:  

  16: public class Customer

  17: {

  18:     public decimal Balance { get; private set; }

  19:  

  20:     public bool IsFlagged

  21:     {

  22:         get { return Balance >= 30m; }

  23:     }

  24: }

This is part of the 31 Days of Refactoring series. For a full list of Refactorings please see the original introductory post.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Sean Chambers

I am a Senior software developer from Palm Coast, Florida. An advocate of Domain Driven Design, Behavior Driven Development, creator of FluentMigrator and community activist. I am married to my beautiful wife Erin and am the proud father of two wonderful children. I currently reside at ACI, a local insurance industry/mortgage software company that excels in creating solutions using Agile methodologies.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.blogcoward.com jdn

    This series has been so not uninformative…LOL.

    Thanks, really enjoyed refresher on all of these.

  • http://fasudblog.freewhost.com/sitemap.html Samoys

    lot about you

  • http://blogs.ibibo.com/phentermine/buy-phentermine Roberto

    cool blog

  • http://vokiciy.angelfire.com dilandinga

    riPDV6 I bookmarked this link. Thank you for good job!

  • http://gospensblog.go.ohost.de/ds/viagra-cialis-and-levitra.html Eowienia

    I want to say – thank you for this!,

  • http://gospensblog.go.ohost.de/ds/cheap-10mg-cialis-without-a-prescription.html Kedirev

    It is the coolest site, keep so!,

  • http://ibuprofen.idoo.com/cialis-peak-blood-level.html Migodda

    I want to say – thank you for this!,

  • http://christmas.freevar.com/drinking-on-antibiotics.html Freak

    Great. Now i can say thank you!,

  • http://circadia1.angelfire.com/soma-hydrochloride-canada.html Onardossa

    I bookmarked this link. Thank you for good job!, Buy Soma Adipex Online, [url="http://dianare.angelfire.com/buy-soma-adipex-online.html"]Buy Soma Adipex Online[/url], http://dianare.angelfire.com/buy-soma-adipex-online.html Buy Soma Adipex Online,

  • http://gbkworks.angelfire.com/effexor-works-quickley-for-deprssion.html Gwerarei

    Great. Now i can say thank you!, Emily Keno, [url="http://hotellunabg.angelfire.com/emily-keno.html"]Emily Keno[/url], http://hotellunabg.angelfire.com/emily-keno.html Emily Keno,

  • http://compuchess.iespana.es/herpes-oral-sex-while-wearing-condom.html Unalelian

    Excellent site. It was pleasant to me., Herpes Moth, [url="http://compuchess.iespana.es/herpes-moth.html"]Herpes Moth[/url], http://compuchess.iespana.es/herpes-moth.html Herpes Moth,

  • http://dentaldoctor.us Doctorset

    This is the welcome page for the dentaldoctor.us Association web site.

  • http://dietguidance.us Dietroly

    This is the welcome page for the dietguidance.us Association web site.