The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Extract to Component Refactoring

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
Extract to Component Refactoring Posted: Sep 8, 2005 8:06 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by James Robertson.
Original Post: Extract to Component Refactoring
Feed Title: David Buck - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/buck-rss.xml
Feed Description: Smalltalk can do that
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From David Buck - Blog

Advertisement

There's a nice refactoring in the Refactoring Browser which a lot of people don't know about. Extract to component is similar to extract method but instead of moving the code to a new method in the same class, it moves it to a new method in another class.

For example, here's a snippet of code from a puzzle-solving program I wrote:

Piece
matchesToRight: aPiece  
  | leftPicture rightPicture |
  rightPicture := self right. 
  leftPicture := aPiece left.
  ^rightPicture color = leftPicture color and: [rightPicture part ~= leftPicture part] 
 

Select the last line and choose "Extract Method to Component". It asks for the variable to delegate to. Choose "rightPicture". It then asks what class the method should be moved to. Select Picture. Finally, it asks for a method name. Choose "belongsNextTo:". Here's the result:

Piece
matchesToRight: aPiece
  | leftPicture rightPicture |
  rightPicture := self right.
  leftPicture := aPiece left.
  ^rightPicture belongsNextTo: leftPicture
 
Picture
belongsNextTo: leftPicture  
   ^self color = leftPicture color and: [self part ~= leftPicture part] 
 

The refactoring extracts the code and moves it into another class. This is useful if the wrong class is doing the work. You can easily move the operation to the proper class.

Read: Extract to Component Refactoring

Topic: Very Short JUnit Tutorial Previous Topic   Next Topic Topic: The development multiplier effect

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use