Refactoring Day 5 : Pull Up Field

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: }
   5: public class CheckingAccount : Account
   6: {
   7:     private decimal _minimumCheckingBalance = 5m;
   8: }
  10: public class SavingsAccount : Account
  11: {
  12:     private decimal _minimumSavingsBalance = 5m;
  13: }

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: }
   6: public class CheckingAccount : Account
   7: {
   8: }
  10: public class SavingsAccount : Account
  11: {
  12: }


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

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.
  • I don’t like that refactoring, kind of. I think it’s incomplete. It’s all about preferences, though, but I think protected fields are a code smell:

    - if you need access to a base class field, you probably have some kind of feature envy. Try Templated Method or something similar.

    - There is nothing in the subclass telling you that this field exists, you just have to know it. It hurts readability.

  • Ed

    I would say pulling anything up into a base class should be done with caution

    It’s likely to lead to a fragile base class. Favour composition over inheritence, using inheritence as a mechanism for reuse iis picking the option with the tightest coupling from the word go

  • nfma

    @Thomas Eyde

    Some refactorings are just steps on other bigger refactorings. Pull up method might make sense in the context…