3

I am reading Martin Fowlers Refactoring book. On page 110 he talks about 'Replace Method with Method Object' where he turns a method with local variables into a class with instance variables.

The outcome is a class called: PriceCalculator with three instance variables i.e. primaryBasePrice, secondaryBasePrice and teritary base price.

I recently asked a question about local variables and instance variables: When should local variables be used over instance variables?. I agree with Samuels answer and Timothy Truckles' answer - I have always used the principle of least visibility in my work.

However, Fowlers refactoring seems to contradict this and is confusing me. Also all the class diagrams in my UML book seem to contradict the principle of least visibility i.e. they use a class with instance variables regardless of the scenario like this one: http://www.newthinktank.com/wp-content/uploads/2012/12/Object-Oriented-Design.png. I understand that you have to compromise in this game when considering performance etc. Is it "better" design to have instance variables rather than local variables?

Doc Brown
  • 218,378
w0051977
  • 7,139

2 Answers2

5

A refactoring is not necessarily an improvement, it is just a structural change. Turning a method into a method object is a refactoring, but so is the opposite, turning a method object into a plain method.

Refactorings are often steps performed in order to make it possible to introduce new features. For example, if you need to introduce the ability to undo or replay select methods, then a first step might be to turn the methods into method-objects, since this will allow you to implement the command pattern as the next step.

But in isolation, turning a method into a method object will not improve your code.

JacquesB
  • 61,955
  • 21
  • 135
  • 189
3

To cite Timothy Truckels' correctly:

Turning local variables into instance variables (for no particular reason) violates the least visibility scope principle

But what is a "particular reason"? I think you will agree a sensible reason is when a method exceeds a certain complexity. And that is what Fowlers "Replace Method by Method Object" refactoring is for, no less, no more. Opposed to @TSar, I don't find Fowler's example in "Replace Method with Method Object" particulary bad. He clearly states it is for "a long method that uses local variables in such a way that you cannot apply Extract Method" - which is a clear difference to the Calculator example in your former question. Note also Fowler's catalogue tries to give a comprehensive description for each of the refactorings, he surely did not want to fill five pages in his book with the code from a real program to make sure anyone believes the method he turns into a class is really a long, complex one.

If you take the "least visibility scope principle" to the extreme just because you want to follow it in a cargo cult manner, you get either very long methods with hundreds of LOC and dozens of local variables, just for not introducing instance variables. Or you get clusters of methods with dozens of parameters, passing the local variables from one method to another, for the same reason. That is surely not what you want to achieve.

Doc Brown
  • 218,378