4

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?

2
  • 6
    You may find this a relevant read: en.wikipedia.org/wiki/Law_of_Demeter. Commented Jul 11, 2012 at 11:59
  • Your first case is less efficient, since you call getPerson and getData twice rather than once. Furthermore, what if the object changes, and the two lines get different people? Commented Jul 11, 2012 at 12:03

8 Answers 8

7

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.

Sign up to request clarification or add additional context in comments.

5 Comments

You can be true, but it doesn't answer the question. You give a third solution based on an informations you haven't. Who told you getPerson can return null? Your post answer more a question like : which is the safer way?
@alain.janinm Well ok, I think mine is both safer and clearer. I also clearly state below the code that it depends on whether the values are able to be null or not, I have not assumed anything, however, I often find that many people miss this sort of thing and so it is always worth pointing out.
You're absolutely right about null check. What is missing in your answer is the case where nothing can return null. Then the creation of data var has no justification in your answer.
It depends if performance is much of an issue, if it isn't I'd still prefer this option including the Data data = person.getData();. Just personal preference I guess in the case where none of them can return null.
Ok, now your answer is more complete ;)
3

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
}

Comments

2

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.

Comments

2

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.

Comments

1

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();

Comments

1

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.

Comments

1

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.

Comments

1
 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.