• Post Reply Bookmark Topic Watch Topic
  • New Topic

What's considered appropriate nesting?  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've read one shouldn't have to many nesting statements due to it getting hard to follow or moving code further and further to the right.
My question is, if you're designing a program involving a bunch of steps what is the accepted way of nesting? For example, if I
have a code where someone picks their favorite basketball team, a specific year for stats, a favorite player, and down to that individual players
stats, how would I code that without doing something like the following:

if(team)
if(year)
if(player)
if(player stats)
{ Bobby had 72 home runs}
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a bunch of conditions like what you describe above, you can combine these conditions in a single if-statement. For example, instead of something like this:

you could write:

It's ofcourse a matter of style, but I would normally not nest if-statements more than 2 or 3 levels deep. If your method gets too complex, consider splitting up the method into multiple smaller, easier to understand methods.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as your nested if blocks are readable. Assume you have 2 levels of nested if blocks with each level having 100 lines of code. This to me is already unmaintainable. Of course you could spin that 100 lines into a method. But the point here is that writing nested if else statements results in code which is very hard to read.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote:I've read one shouldn't have to many nesting statements due to it getting hard to follow or moving code further and further to the right. My question is, if you're designing a program involving a bunch of steps what is the accepted way of nesting?

It's actually rare for conditions alone to cause nesting (as Jesper showed), but it does quite often happen that you need to nest loops - and in that case, my general rule of thumb is "no more than two deep". If I need more, then I'll generally stick the innermost one(s) in a method that describes what they're doing. For one thing, it usually makes the code more readable.

But there's no "right" or "wrong" about any of this; just a feel for what "looks good" - and that'll come with practise.

My advice is to keep two things in mind when you're writing code - and it doesn't just apply to nesting:
1. Do you find your code easy to follow?
2. Do you think you'll find it equally easy to follow in 6 month's time, when you've forgotten what you were thinking when you wrote it?

That last question is usually a good guide as to whether other people will find it easy to follow - and that's generally the best way to make sure your programs live a long life.

HIH

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!