Tips & Tricks

IntelliJ IDEA 8 Refactorings: Introduce Parameter Object

As you might already have noticed from the Features list, IntelliJ IDEA 8 brings you 7 new refactorings to ease the daily coding routine. One of these refactorings is Introduce Parameter Object. Its name speaks for itself: when you have a large group of parameters which go a long way through a chain of delegating method calls, it lets you create a wrapper class that you can then use instead. Another case is when you have a group of method parameters semantically tied one to another – a wrapper class comes handy as well.

Let’s have a look at an example. Say, we have this code:
drawEdge class before refactoring

Look at how many parameters are passed to drawEdge method – six! Imagine how lengthy the calls to this method are and how it all swamps the code. No worries – IntelliJ IDEA can help us. As we can see, the X and Y coordinates and the edgeWidth naturally go together, so we can move them to their own object.

Right-click the method name and select Refactor | Introduce Parameter Object from the context menu.In the Introduce Parameter Object dialog, we can select whether we want to create a new class, inner class, or use an existing one to wrap the parameters. For this case, we opt to the first choice. Then, select the parameters to be extracted. As we decided, these are edgeWidth, x1, x2, y1, and y2. Click OK, and voila – the things are a lot easier now!

drawEdge class after refactoring

new Parameter Object

image description