Los Techies : Blogs about software and anything tech!

Refactoring Day 16 : Encapsulate Conditional


Sometimes when doing a number of different checks within a conditional the intent of what you are testing for gets lost in the conditional. In these instances I like to extract the conditional into an easy to read property, or method depending if there is parameters to pass or not. Here is an example of what the code might look like before:

   1: public class RemoteControl
   2: {
   3:     private string[] Functions { get; set; }
   4:     private string Name { get; set; }
   5:     private int CreatedYear { get; set; }
   6:  
   7:     public string PerformCoolFunction(string buttonPressed)
   8:     {
   9:         // Determine if we are controlling some extra function
  10:         // that requires special conditions
  11:         if (Functions.Length > 1 && Name == "RCA" && CreatedYear > DateTime.Now.Year - 2)
  12:             return "doSomething";
  13:     }
  14: }

After we apply the refactoring, you can see the code reads much easier and conveys intent:

   1: public class RemoteControl
   2: {
   3:     private string[] Functions { get; set; }
   4:     private string Name { get; set; }
   5:     private int CreatedYear { get; set; }
   6:  
   7:     private bool HasExtraFunctions
   8:     {
   9:         get { return Functions.Length > 1 && Name == "RCA" && CreatedYear > DateTime.Now.Year - 2; }
  10:     }
  11:  
  12:     public string PerformCoolFunction(string buttonPressed)
  13:     {
  14:         // Determine if we are controlling some extra function
  15:         // that requires special conditions
  16:         if (HasExtraFunctions)
  17:             return "doSomething";
  18:     }
  19: }

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 16 2009, 06:29 AM by schambers

Comments

Reflective Perspective - Chris Alcock » The Morning Brew #413 wrote Reflective Perspective - Chris Alcock » The Morning Brew #413
on 08-17-2009 3:38 AM

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #413

张荣华 wrote 31天重构指南之十六:封装条件
on 10-11-2009 9:49 PM

有时当我们的条件关系比较复杂时,代码的可读性会比较差,所以这时我们应该根据条件表达式是否需要参数将条件表达式提取成可读性更好的属性或是方法,如果条件表达式不需要参数则可以提取成属性,如果条件表达式需要...

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

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

PetterLiu wrote 31 Days of Refactoring
on 11-27-2009 4:01 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:03 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