IntelliJ Java IDE
The moose likes Beginning Java and the fly likes Nested Method Calls Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Nested Method Calls" Watch "Nested Method Calls" New topic
Author

Nested Method Calls

Adam Jelinek
Greenhorn

Joined: Feb 07, 2012
Posts: 4
When and when not to use nested method calls. I am just starting out in Java and I have heard different things on nesting method calls.

Here is an example of what I am talking about.
Code This:

Or something like this

or some other combination

Thanks
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50693

Never heard that called "nested". I'd have used the term "chained".


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50693

The criteria I'd use is to balance the two following considerations:
  • Are the intermediate variables needed?
  • Which approach provides the best clarity?
  • Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 3144
    Note that there will be no difference in performance or behavior. It is simply a matter of what's easiest to write and read, and, as Bear points out, whether you need the results of one call for more than just the immediate use as an argument to another call.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 3144
    Bear Bibeault wrote:Never heard that called "nested". I'd have used the term "chained".


    To me, chained is where nested would be
    Adam Jelinek
    Greenhorn

    Joined: Feb 07, 2012
    Posts: 4
    Jeff Verdegan wrote:Note that there will be no difference in performance or behavior. It is simply a matter of what's easiest to write and read, and, as Bear points out, whether you need the results of one call for more than just the immediate use as an argument to another call.


    In this case the intermediate results are not needed. That is actually my question, readability. I prefer the condensed version, where my mentor suggests to always break it out. With that I wanted to ask the community what they fell is easier to read. I could code this either way. Right now I am just trying to avoid a potentially bad habit.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 3144
    Adam Jelinek wrote:
    That is actually my question, readability. I prefer the condensed version, where my mentor suggests to always break it out.


    I'll sometimes nest it one level deep, if the names are short enough, like but for the specific example in your original post, I would absolutely use varaibles.
    Adam Jelinek
    Greenhorn

    Joined: Feb 07, 2012
    Posts: 4
    Jeff Verdegan wrote:
    Adam Jelinek wrote:
    That is actually my question, readability. I prefer the condensed version, where my mentor suggests to always break it out.


    I'll sometimes nest it one level deep, if the names are short enough, like but for the specific example in your original post, I would absolutely use varaibles.



    Thanks for the help!
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 13842

    Adam Jelinek wrote:That is actually my question, readability. I prefer the condensed version, where my mentor suggests to always break it out. With that I wanted to ask the community what they fell is easier to read.


    In my opinion your second example is much more readable. However your first example could be made readable with a bit of white space:


    And I certainly wouldn't go along with the advice "always break it out". Consider this example:

    I wouldn't change it to this to improve clarity:
    Bear Bibeault
    Author and opinionated walrus
    Marshal

    Joined: Jan 10, 2002
    Posts: 50693

    There's never one approach that always "the best". That's where judgement comes in.

    And sometimes, it's just a matter of formatting. Consider the difference between:

    and

    To me, the overriding criteria is clarity.

    One thing you always want to avoid is "clever" (unless being clever achieves greater clarity).
    Adam Jelinek
    Greenhorn

    Joined: Feb 07, 2012
    Posts: 4
    Bear Bibeault wrote:There's never one approach that always "the best". That's where judgement comes in.

    To me, the overriding criteria is clarity.

    One thing you always want to avoid is "clever" (unless being clever achieves greater clarity).


    Very well put. Thank you to everyone for your help.
     
     
    subject: Nested Method Calls
     
    Threads others viewed
    JMS implementation using JNDI in spring application
    equals and ==
    trouble setting a Boolean parameter
    Subclassing a generic class
    life/death?
    MyEclipse, The Clear Choice