3

So I'm working on a codebase, and there's a utility class that deals with generating excel documents for users.

It has a method called putDataInRowColumn(row, column, data)

It has quite a few methods like putObjectsIntoExcel(myBigClass blah) and putObjectsIntoSpecialExcelType(myBigClass blah)

which calls a load of methods like putObjectIntoSpecialRowType(blah.foo(), rowIndex, specialConditions) and putObjectIntoTotallydifferentRowType(blah.bar(), rowIndex, specialConditions)

The point of all this is that the method putDataInRowColumn(row, column, data) gets called a metric buttload, from a bunch of different places. like 100+.

Now, given this legacy code, I needed to modify the method to take additional parameters - styling information. 99% of the methods will now take 'null' as a fourth argument, and 1% will receive an object which contains styling information.

I modified the method signature, to receive the additional parameter, but I found myself having to write a regex to find/replace all of the method calls. it worked, but this felt like the wrong way to go about this.

How should I have done it?

Paul
  • 3,318
  • 8
  • 36
  • 60
  • What about using automatic refactoring tools ? `Change Method Signature...` – gontard Feb 26 '15 at 14:18
  • @Paul, do you consider your question answered? If so, please consider accepting one of the answers or otherwise elaborate on the question so the answers can be improved. – aioobe Mar 02 '15 at 10:19

5 Answers5

6

You create a new overloaded method that accepts a fourth argument and let the old method call the new method with null as fourth argument.

Before:

public void putDataInRowColumn(int row, int column, int data) {
    // implementation
}

After:

// 99% calls this
public void putDataInRowColumn(int row, int column, int data) {
    // Delegates to new method with null as "default" argument
    putDataInRowColumn(row, column, data, null);
}

// Called by new code
public void putDataInRowColumn(int row, int column, int data, Style style) {
    // implementation
}

Related:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
5

Let the old putDataInRowColumn(row, column, data) live on, and let it delegate its calls to putDataInRowColumn(row, column, data, null).

public static Something putDataInRowColumn(int row, int column, Whatever data){
    return putDataInRowColumn(row, column, data, null);
}

You could also have used a modern IDE to refactor the method with "add parameter" and provide a sensible default value for the new parameter, null in your case. :)

On that note, I also think you should avoid null as a "no styling" value, because it makes very little sense when you see code like putDataInRowColumn(0, 5, data, null) - isn't putDataInRowColumn(0, 5, data, Styling.NONE) a whole lot easier to read?

gustafc
  • 28,465
  • 7
  • 73
  • 99
2

write another overload method putDataInRowColumn(row, column, data, additional) in the same class, and 99% of classes calls putDataInRowColumn(row, column, data) while change 1% of classes to call this new method.

//edit old method to call newer one.
putDataInRowColumn(row, column, data) 
{
putDataInRowColumn(row, column, data, null)
}
anurag gupta
  • 379
  • 1
  • 5
2

You have to overload the method.

The new method sign would be:

putDataInRowColumn(SomeType row, SomeType column, SomeType data, SomeType style)

In this method you will check style parameter.

Then putDataInRowColumn(SomeType row, SomeType column, SomeType data) implementation will be:

putDataInRowColumn(row, column, data) { putDataInRowColumn(row, column, data, null); }

If you follow this you don't have to do a refactor of previous code.

Diego
  • 304
  • 1
  • 8
1

If 99% of the calls don't need the 4th argument, it would make more sense to use method overloading, so that 99% of the calls remain unchanged.

For example (I made some arbitrary assumptions on the types of the arguments and the return type) :

Existing method :

putDataInRowColumn(int row, int column, SomeType data) {
    putDataInRowColumn(row, column, data, null);
}

New method :

putDataInRowColumn(int row, int column, SomeType data, SomeOtherType) {
    the actual logic
}
Eran
  • 387,369
  • 54
  • 702
  • 768