166

I've seen people do more than 4 as well though

Comments
  • 6
    Multi dimensional arrays / lists encourage this some days 🙁
  • 0
    Itry to adhere to one nest for anything, keeps it easy to read
  • 0
    And I'm sitting here with some old code that literally had 6 seperate for loops nested in another for loop .-.
  • 0
    if you can nest it, you can extract it into a method
  • 2
    @kleopi good luck creating an early exit condition.

    Sure in some languages you can throw an exception and have that handles in a very convoluted way but for good sake stop obsessing over this. Clarity trumps the nesting argument!

    If it seems one coherent task it's fine to have a nested loop and a few conditions especially if the deeper loop needs a lot of data.
    Splitting things up with hard to define method names and a lot of argument passing is not helpful.

    Please understand that I hates long methods/functions/subroutines and overly complex flow control in them. These days I come across more and more of the stuff in previous paragraph because of nesting fear.
  • 1
    More than 1 nested item it's a code smell indicator.
  • 2
    @hjk101 I totally agree that nesting isn't strictly bad. But if your loop has 200 lines 3 loops deep, it might be worth to think about simplifying, or to use streams, or well to extract it in a method.
    If it's nested but easily understandable or caused by Dataobject nesting, extracting can indeed make it harder to work with. All depends on the situation.
  • 0
    @kleopi if you're loop has 200 lines it's safe to assume it is not doing one coherent task and is not clear at all.
    That root is the problem; not the nesting. Nesting is just a side effect.

    Nesting itself becomes a problem when you have to scroll east or having to read it 5 times just to follow the flow.
  • 0
    @kleopi... and all that does in many cases is:
    1. prevent you from doing early exit in many cases

    2. complicates how you actually modify the data (byref one dimension of the array to the procedure? have the array as global "temporary" variable? have the modified dimension as a return value from the function, meaning you need to duplicate it needlessly?)

    3. (and this is worst, imo) : HIDES AWAY A LAYER OF TIME COMPLEXITY OF THE CODE, inviting careless use, which will inevitably come back at some point and bite you in the ass by tanking your performance.

    any questions?
Add Comment