adi
adi

Reputation: 1741

Java calling methods on instance variable

Got two ways:

 String salary = company.getPerson(id).getData().getSalary();      
 String postcode = company.getPerson(id).getData().getPostCode();

or

Data data = company.getPerson(id).getData();    
String salary = data.getSalary();     
String postcode = data.getPostCode();

Which is the preferred way and why? Any benefits apart from readability?

Upvotes: 4

Views: 295

Answers (8)

Jon Taylor
Jon Taylor

Reputation: 7905

Id actually prefer a third option in case the person does not exist

Person person = company.getPerson(id);
if(person != null) {
    Data data = person.getData();
    if(data != null) {
        String salary = data.getSalary();
        String postcode = data.getPostCode();
    }
}

It depends whether there may be null values or not. If you can guarantee there will be no null values then you can eliminate some/all of the null checks.

As another user pointed out in the comment below there may be a case where none of the method calls can return null in which case unless performance is an issue it would really be down to personal preference in my opinion.

I would probably still prefer to seperate them out into seperate calls as I have, even without the null checks.

Upvotes: 7

alain.janinm
alain.janinm

Reputation: 20065

There is not really a true good style, it depends on the context and on your needs.

Use :

 String salary = company.getPerson(id).getData().getSalary();      
 String postcode = company.getPerson(id).getData().getPostCode();

If you need to improve your memory usage.

Else use :

Data data = company.getPerson(id).getData();    
String salary = data.getSalary();     
String postcode = data.getPostCode();

If you want to improve performance.

For readability it's too subjectives for me. There are both as much readable honestly.

The 2nd example has the advantages of refractoring company.getPerson(id) and the variable also allow to perform some verification without having to call company.getPerson(id) again. I often prefer this kind of style, but, again, depending on the needs the first solution can be better if getPersonne(id) and getData() can't return null.

Upvotes: 2

amicngh
amicngh

Reputation: 7899

 String salary = company.getPerson(id).getData().getSalary();      
 String postcode = company.getPerson(id).getData().getPostCode();

This is less readable and scope of data return by company.getPerson(id).getData() is at run time only so it will garbage collected at that time.

or

Data data = company.getPerson(id).getData();    
String salary = data.getSalary();     
String postcode = data.getPostCode();

Above is more readable but You are creating one more reference variable of type Data so for garbage collection collector will check for reference of 'data' in heap and then if finds it eligible then it will collect.

Upvotes: 1

user1512950
user1512950

Reputation:

One thing to consider is the case where getPerson(...) or getData() might return null. If any of these ever return null you'll be rewarded with a NullPointerException so the answer might depend on other hidden factors than just readability.

Upvotes: 1

user784540
user784540

Reputation:

It depends on complexity of your getter methods. If, for instance, getPerson(id) method is complex, or/and getData() method is complex, then it may lead to performance problems.

A smart compiler may overcome this issue excluding repeating code parts. But in general, from my point of view, the second way is better.

Upvotes: 2

Bohemian
Bohemian

Reputation: 425398

You have to consider performance. While "nice looking code" is great, if there is something expensive to get, keep a reference to it and reuse it.

It is likely that company.getPerson(id) involves a database query to retrieve the data, so while the first option looks "neater", the second option is probably better.

But the answer is "it depends" - if each call is cheap, you can use the first option.

Upvotes: 1

Tobias
Tobias

Reputation: 7380

For refactoring / readability and performance reasons I personally find this one the best solution:

Data data = company.getPerson(id).getData();    
String salary = data.getSalary();     
String postcode = data.getPostCode();

Upvotes: 1

codebox
codebox

Reputation: 20264

If there is any chance that the intermediate variable may be null, then you should use the second option, together with a null check

Data data = company.getPerson(id).getData();    
if (data != null){
    String salary = data.getSalary();     
    String postcode = data.getPostCode();
    // other code here
}

Upvotes: 3

Related Questions