Joe
Joe

Reputation: 386

What do you call this?

Is there a name for this techinique (method calls returning objects upon which another method call is made on the same line)?

String pAID = commonAssets.getApplicationServerSettings().getSetting("plivoAuthID");

instead of

ApplicationServerSettings applicationServerSettings = commonAssets.getApplicationServerSettings();
String pAID = applicationServerSettings.getSetting("plivoAuthID");

Also, when I do the first, Eclipse doesn't prompt me to import the class ApplicationServerSettings, but it does if I use the second code style.

Also, are these two styles merely preferences?

Upvotes: 1

Views: 114

Answers (2)

Cacho Santa
Cacho Santa

Reputation: 6914

The technique is called method chaining.

String pAID = commonAssets.getApplicationServerSettings().getSetting("plivoAuthID");

Definition from the wiki:

Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.[1] Local variable declarations are syntactic sugar because of the difficulty humans have with deeply nested method calls.[2][3] A method chain is also known as a train wreck due to the increase in the number of methods that come one after another in the same line that occurs as more methods are chained together[4] even though line breaks are often added between methods.

Your second question:

Also, when I do the first, Eclipse doesn't prompt me to import the class ApplicationServerSettings, but it does if I use the second code style.

  • From the definition again "Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results." That is why it does not prompt you to import the class ApplicationServerSettings.

Another example (besides the want you introduce) that looks simpler:

Take a look at the wiki example:

class Person {
    private String name;
    private int age;

    // In addition to having the side-effect of setting the attributes in question,
    // the setters return "this" (the current Person object) to allow for further chained method calls.

    public Person setName(String name) {
        this.name = name;
        return this;
    }

    public Person setAge(int age) {
        this.age = age;
        return this;
    }

    public void introduce() {
        System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
    }

    // Usage:
    public static void main(String[] args) {
        Person person = new Person();
        // Output: Hello, my name is Peter and I am 21 years old.
        person.setName("Peter").setAge(21).introduce();
    }
}

Upvotes: 7

Harald Wellmann
Harald Wellmann

Reputation: 12855

It's often called fluent syntax.

IMHO it's a matter of style, there's no right or wrong.

Fluent syntax is more concise, which is sometimes a good thing.

The other variant is more handy for source-level debugging. You can step through the statements and inspect the intermediate results.

Upvotes: 1

Related Questions