• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nested Method Calls  RSS feed

 
Adam Jelinek
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never heard that called "nested". I'd have used the term "chained".
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 22844
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 ninkuma
    Marshal
    Posts: 66307
    152
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!