Dean Wampler, a friend and coworker of mine, recently wrote a neat blog about else statements. He was sitting with someone and they noticed that good code just doesn't seem to have as many elses as code that isn't quite so good. I grepped a couple of code bases I have on hand, and it does seem like there's a correlation.
Does this mean that elses are bad? I don't think so. There's a difference between seeing some code quality as an indicator and saying that the construct itself is bad. And, I doubt that anyone could end up with better code by just mechanically removing elses. That seems like it would be recipe for disaster.
Unfortunately, this argument didn't fly on reddit a while ago. I'm not sure why.
Regarding the Reddit post, I'm just amazed how much bile (and personal insults) a post about programming can evoke in people.
The internet is a strange place.
Posted by: JakeB | June 18, 2007 at 01:14 PM
So, maybe guard clauses are less icky than long, nested, if-else trees? Having fewer "if" mean having fewer "else" too, and you don't suppose that reducing the number of decisions makes code clearer?
I guess it was not that hard of an observation to justify. Odd that it ticked people off. You'd think he recommended not writing comments (http://butunclebob.com/ArticleS.TimOttinger.ApologizeIncode and http://worsethanfailure.com/Comments/So_Bad_It_Needs_An_Apology.aspx?pg=4 )or something.
Ah, well, you of all people know that you can't say anything you think is important or useful without someone getting offended. I guess that's the way of it.
Posted by: Tim | June 21, 2007 at 11:03 AM