Los Techies : Blogs about software and anything tech!

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.

Kick It on DotNetKicks.com
Posted Aug 26 2009, 08:08 AM by schambers

Comments

on 08-26-2009 7:36 PM

Agile/ALT.NET/Software Design Building Your Own Data Access Layer: Hydrating Entities A guide into OR/M implementation challenges: Hydrating Entities Build Your Own Data Access Layer: Session Level Cache Talking about StoryTeller and Executable Requirements

jdn wrote re: Refactoring Day 26 : Remove Double Negative
on 08-28-2009 7:10 PM

This series has been so not uninformative...LOL.

Thanks, really enjoyed refresher on all of these.

Samoys wrote re: Refactoring Day 26 : Remove Double Negative
on 10-03-2009 12:03 PM

lot about you

Roberto wrote re: Refactoring Day 26 : Remove Double Negative
on 10-04-2009 6:39 AM

cool blog

dilandinga wrote re: Refactoring Day 26 : Remove Double Negative
on 10-04-2009 1:09 PM

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

200 Million Years of VFX, John Carter of Makeup, & Spore Gets … | Cosmetics Beauty Wisdom wrote 200 Million Years of VFX, John Carter of Makeup, &amp; Spore Gets &#8230; | Cosmetics Beauty Wisdom
on 10-07-2009 1:37 AM

Pingback from  200 Million Years of VFX, John Carter of Makeup, & Spore Gets … | Cosmetics Beauty Wisdom

Eowienia wrote re: Refactoring Day 26 : Remove Double Negative
on 10-10-2009 2:14 PM

I want to say - thank you for this!,

Kedirev wrote re: Refactoring Day 26 : Remove Double Negative
on 10-11-2009 4:48 AM

It is the coolest site, keep so!,

Migodda wrote re: Refactoring Day 26 : Remove Double Negative
on 10-11-2009 6:45 PM

I want to say - thank you for this!,

Freak wrote re: Refactoring Day 26 : Remove Double Negative
on 10-12-2009 9:30 AM

Great. Now i can say thank you!,

31 Days of Refactoring « Vincent Leung's .NET Tech Clips wrote 31 Days of Refactoring &laquo; Vincent Leung&#039;s .NET Tech Clips
on 10-28-2009 9:28 AM

Pingback from  31 Days of Refactoring « Vincent Leung's .NET Tech Clips

Onardossa wrote re: Refactoring Day 26 : Remove Double Negative
on 11-02-2009 7:41 PM

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

Gwerarei wrote re: Refactoring Day 26 : Remove Double Negative
on 11-03-2009 12:48 PM

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

Unalelian wrote re: Refactoring Day 26 : Remove Double Negative
on 11-03-2009 5:00 PM

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

Doctorset wrote re: Refactoring Day 26 : Remove Double Negative
on 11-20-2009 10:21 AM

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

Dietroly wrote re: Refactoring Day 26 : Remove Double Negative
on 11-20-2009 11:02 PM

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

PetterLiu wrote 31 Days of Refactoring
on 11-27-2009 4:02 AM

Refactoring Day 1 : Encapsulate Collection Refactoring Day 2 : Move Method Refactoring Day 3 : Pull ...

PetterLiu wrote 31 Days of Refactoring
on 11-27-2009 4:04 AM

Refactoring Day 1 : Encapsulate Collection Refactoring Day 2 : Move Method Refactoring Day 3 : Pull ...

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems