• Home
  • RSS Feed
  • Log in

IntelliJ 8: Introduce Parameter Refactoring
Posted by Erik Pragt late at night: November 6th, 2008

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!

  • Share/Bookmark

Tags: IntelliJ, intellij 8, refactoring
Filed under IntelliJ, Java | 7 Comments »



7 Responses to “IntelliJ 8: Introduce Parameter Refactoring”



    Vikas Hazrati Says:
    Posted at: November 7, 2008 at 12:46 am

    Eclipse has it :)

    Select the parameters, Alt+Shft+T –> Introduce Parameter Object



    Erik Pragt Says:
    Posted at: November 7, 2008 at 8:58 am

    Oh!!! Just wait for the next blog!!



    raveman Says:
    Posted at: November 7, 2008 at 9:44 am

    I bet IntelliJ had that feature first :P lol



    Erik Pragt Says:
    Posted at: November 7, 2008 at 10:17 am

    Well, ehhmm,…this is a *new* refactoring… But I guess it’s good that IntelliJ finally borrows something back! ;)



    florin Says:
    Posted at: November 7, 2008 at 6:16 pm

    And then you have to introduce another “Parameter Object” to the Name class that will take two parameters:

    @Constructor
    public Person(PersonParameters parameters) {
    this.firstName = parameters.getFirstName();
    this.lastName = parameters.getLastName();
    }

    with resulting code:
    parser = new Parser(new Name(parameters));

    etc…

    is @Re-curse a reserved annotation or a keyword to be filtered?

    I love it.



    Aziz Kadhi Says:
    Posted at: November 7, 2008 at 9:39 pm

    Which version are you using? I am using 8.0M1 and I don’t seem to be able to find it.

    Thanks



    Aziz Kadhi Says:
    Posted at: November 7, 2008 at 9:42 pm

    Never mind. I have just learned that the final 8.0 release is out.



Leave a Reply

Click here to cancel reply.

Deployment automation for Java application running on Websphere, WebLogic and JBoss

Archives

  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009

Xebia Sites

  • Xebia Corporate
  • Xebia France
  • Xebia India

Categories

  • Java (282)
  • Agile (109)
  • General (50)
  • Testing (42)
  • Performance (42)
  • Hibernate (36)
  • Scrum (33)
  • Podcast (31)
  • Architecture (31)
  • Spring (28)
  • SOA (24)
  • Maven (22)
  • Project Management (22)
  • Middleware (23)
    • Deployment (14)
  • Flex (17)
  • JPA (17)
  • Eclipse (15)
  • Xebia Labs (15)
  • Quality Assurance (14)

Tag Cloud

    Testing Java Scrum XML JavaOne Spring Agile Awareness Workshop Closures Ajax Performance Hibernate Functional Programming Introduction to Agile product owner SOA Maven Lean fitnesse Architecture qcon Seam IntelliJ Agile Grails Semantic Web Xebia esb Poppendieck Groovy Scala