Top Stories
|
Bloody code!4 Dec 2007 00:32 Multiple exit pointsIt's amazing how some good practices limp on for decades beyond their expiration date. I still encounter people who insist that a method should have only one point of return - as if we're all still littering our code with GOTOs, and the concept of a "black-box" function was never invented. The way these same people go on about multiple exit points, you'd think they were headlining in one of the grislier episodes of ER trying to patch up the latest guest-star casualty while predicting dire consequences for the patient as they lie, bleeding to death. People who prefer the single exit point tend to feel very strongly about it. But the reasons they give have never struck me as especially convincing. The following stream of generic reasons reminds me of the cookie-cutter platitudes that they roll out at the end of the more nauseating US TV series that seek to emulate ER, over the soul-grinding background warble of James Blunt:
at the top of a method is much clearer, than:
Sometimes, trying to weave your code into a single return point results in setting of pointless flags and excessive nesting of "if..else" conditions. It's like wrapping a paper napkin around a seven-dimensional helix and trying to read the agile documentation off it. Figuring out whether each block of code is relevant to the current program state becomes a game of lining up the curly braces to figure out where each clause finishes. It's easier to simply say: "Hey, I'm halfway through a method but I'm done. I'm outa here!" Artificially stretching the program flow to the end of the method just results in misleading code: implying that a block of code is relevant to a given state when the runtime really has no business still noodling around in there. If it's time to exit a method, exit the method already. Adhering to an outdated maxim like "single exit point" results in a "one size fits all" approach to programming, which is hardly a good thing. But religiously hacking in multiple return points would of course be bad as well. If in doubt, go for the simpler, more expressive option which best communicates what the code means. It's more an article of faith than anything else. It's important to be able to take a step back and make a rational judgement call: this alone helps sort out the thinkers from the believers. ® Agile Iconoclast Matt Stephens has co-authored Use Case Driven Object Modeling with UML: Theory and Practice, which explores ways to drive functional tests and unit tests from use cases. 67 comments posted — Comment period finished Sorry - just can't agree with you on this one.Posted: 00:49 4th December 2007 Thank you!Posted: 01:15 4th December 2007 Vey bloody codePosted: 01:44 4th December 2007 More nuancedPosted: 01:47 4th December 2007 Clean, clear and well-documented = The AnswerPosted: 02:27 4th December 2007
Track this type of story as a custom Atom/RSS feed or by email. Related storiesThe secret to evolutionary code (24 March 2008)
|
Breaking Hardware News
The heat rising from San Jose isn't merely an effect of a convention center full of GTX 280 cards being flipped on at once. Nvidia's decision to host its inaugural computing conference, Nvision 08, this week in the company's home town has fanned the flames of passion inside city officials.
Newsletter |