Los Techies : Blogs about software and anything tech!

Refactoring Day 28 : Rename boolean method


Today’s refactoring doesn’t necessarily come from Fowlers refactoring catalog. If anyone knows where this “refactoring” actually comes from, please let me know.

Granted, this could be viewed as not being a refactoring as the methods are actually changing, but this is a gray area and open to debate. Methods with a large number of boolean parameters can quickly get out of hand and can produce unexpected behavior. Depending on the number of parameters will determine how many methods need to be broken out. Let’s take a look at where this refactoring starts:

   1: public class BankAccount
   2: {
   3:     public void CreateAccount(Customer customer, bool withChecking, bool withSavings, bool withStocks)
   4:     {
   5:         // do work
   6:     }
   7: }

We can make this work a little better simple by exposing the boolean parameters via well named methods and in turn make the original method private to prevent anyone from calling it going forward. Obviously you could have a large number of permutations here and perhaps it makes more sense to refactor to a Parameter object instead.

   1: public class BankAccount
   2: {
   3:     public void CreateAccountWithChecking(Customer customer)
   4:     {
   5:         CreateAccount(customer, true, false);
   6:     }
   7:  
   8:     public void CreateAccountWithCheckingAndSavings(Customer customer)
   9:     {
  10:         CreateAccount(customer, true, true);
  11:     }
  12:  
  13:     private void CreateAccount(Customer customer, bool withChecking, bool withSavings)
  14:     {
  15:         // do work
  16:     }
  17: }

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 28 2009, 08:12 AM by schambers

Comments

nathan wrote re: Refactoring Day 28 : Rename boolean method
on 08-28-2009 10:14 AM

more a case of refactor methods with lots of arguments isn't it? unsure if this is a good example / good advice tbh

nice series though!

rhu wrote re: Refactoring Day 28 : Rename boolean method
on 08-29-2009 6:40 PM

Or use a builder:

BankAccount ba = new AccountBuilder().customer(cust).withChecking().withSavings().create();

on 08-31-2009 11:56 AM

Agile/ALT.NET/Software Design A guide into OR/M implementation challenges: The Session Level Cache Build Your Own Data Access Layer: Lazy Loading A guide into OR/M implementation challenges: Laxy Loading Building Your Own Data Access Layer: Executing

Eric Smith wrote re: Refactoring Day 28 : Rename boolean method
on 09-08-2009 8:21 AM

How about a [Flags] enumeration:

CreateAccount(customer, AccountCreationOptions.WithChecking | AccountCreationOptions.WithSavings)

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

张荣华 wrote 31天重构指南之二十八:为布尔方法命名
on 11-05-2009 10:15 PM

今天要说的重构我不确定是否是来自于Fowlers的重构目录,如果有人知道今天要说的重构的实际出处,请告诉我。 今天要说的重构并不是普通字面意义上的重构,它有值得讨论的地方。当一个方法带有大量的布尔型...

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