IntelliJ 8 provides 7 new refactorings. One new powerful refactoring is the Introduce Parameter Object. The name speaks for itself: it introduces a parameter object! But what does it really do? The refactoring promotes good encapsulation. It encapsulates fields by introducing a new object for them.
For example, you might encounter or write the following piece of code:
public class Parser {
public void parse(String firstName, String lastName) {
// firstName and lastName handling here
}
public void testParser() {
new Parser().parse("erik", "pragt");
}
}
The method call consists of two Strings, but a better approach might be to encapsulate those fields into a new class, for example 'Name'. This refactoring does exactly that. Just select the 'parse' method, go to Refactor -> Introduce Parameter Object, which will look like:
public class Name {
private final String firstName;
private final String lastName;
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
public class Parser {
public void parse(Name name) {
// name handling here
}
public void testParser() {
new Parser().parse(new Name("erik", "pragt"));
}
}
After refactoring, you're code will look like this, which is a much better typed version of the previous one!
Written by
Erik Pragt
Our Ideas
Explore More Blogs

The Unwritten Playbook: Leading Without a Title
The Unwritten Playbook: Leading Without a Title Leading Without a Title: The Consultant’s Guide to Stealth Influence “Why should we listen...
Jethro Sloan

The Unwritten Playbook: Winning as a Team
The Unwritten Playbook: Winning as a Team Team Whispering: Navigating the Human Dynamics No One Prepared You For "We’ve gone through three...
Jethro Sloan

