Los Techies : Blogs about software and anything tech!

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: }
   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: }

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: }

 

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 05 2009, 07:39 AM by schambers

Comments

on 08-05-2009 9:30 PM

ASP.NET AJAX/ASP.NET 4.0/ASP.NET MVC Get early access to ASP.NET AJAX in Action, Second Edition - This was THE ASP.NET AJAX book per ScottGu and others in the original ASP.NET 4.0 Series Index Post ASP.NET 4.0 AJAX Enhancements Beginning ASP.NET MVC 1

Thomas Eyde wrote re: Refactoring Day 5 : Pull Up Field
on 08-06-2009 3:07 AM

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 wrote re: Refactoring Day 5 : Pull Up Field
on 08-19-2009 12:07 PM

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

张荣华 wrote 31天重构指南之五:提升字段
on 09-28-2009 1:52 AM

今天的重构指南和“提升方法”有点类似,不过要提升的不是方法,而是字段,让我们来看下面的代码:1:publicabstractclassAccount 2:{ ...

nfma wrote re: Refactoring Day 5 : Pull Up Field
on 10-24-2009 10:23 AM

@Thomas Eyde

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

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:00 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:02 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