Refactoring Day 5 : Pull Up Field


</p>

Today we look at a refactoring that is similar to the Pull Up method. Instead of a method, it is obviously done with a field instead!

   1: public abstract class Account
   2: {
   3: }
   4:  
   5: public class CheckingAccount : Account
   6: {
   7:     private decimal _minimumCheckingBalance = 5m;
   8: }
   9:  
  10: public class SavingsAccount : Account
  11: {
  12:     private decimal _minimumSavingsBalance = 5m;
  13: }
</p>

In this example, we have a constant value that is duplicated between two derived classes. To promote reuse we can pull up the field into the base class and rename it for brevity.

   1: public abstract class Account
   2: {
   3:     protected decimal _minimumBalance = 5m;
   4: }
   5:  
   6: public class CheckingAccount : Account
   7: {
   8: }
   9:  
  10: public class SavingsAccount : Account
  11: {
  12: }
</p>

 

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

Refactoring Day 4 : Push Down Method