Deep nesting in code hid a problem from me

 1 year ago
source link: http://rachelbythebay.com/w/2013/02/05/nesting/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Deep nesting in code hid a problem from me

Deeply nested code is a pain to read. Here's some more proof. The other day, I wrote about replacing some broken code. In it, I provided an example of what a huge block of nested if-then-elses might look like. It looked like this:

Block of code from old post

Note that I said looked, as in past tense. This morning I was working on fixing a different part of that post based on some reader feedback, and then noticed it: I mismatched my blocks! Even though I tried to be very careful about this for my post, I still made a mistake.

The code as shown above doesn't say "machine is offline" when it's offline. It says it when the base install isn't OK. It doesn't say anything when the machine is actually offline. Worse yet, it never does anything to solve the actual problem: a bad base install!

I've fixed the old post so it doesn't trip anyone up. I've preserved the broken version of it here as a screenshot to demonstrate just how subtle the problems of "crowded code" can be.

Let your code roam free and breathe! White space is good!

About Joyk

Aggregate valuable and interesting links.
Joyk means Joy of geeK