|
Re: How Do You Structure Flow in the Presence of Potential Errors?
|
Posted: Aug 1, 2007 9:59 AM
|
|
Hi Robert,
> Early on in the history of java, it was understood that > exceptions should be thrown when a condition occurred > which the code could not negotiate. That exceptions > should not be used for flow control. > Well technically that's what was going on in this example. The getSignedInUser method doesn't know how to handle the error condition locally, so it throws an exception. But the trouble here is that the intent of factoring functionality out into that getSignedInUser method in the first place was so the programmer could use exceptions for that error particular condition in the handler method. In Java that doesn't feel like a good use of exceptions to me.
> Early on in the history of languages, it was understood > that functions should have one exit point/return. This > was not for so much for machine efficiency, but logical > consistency. > I'm not sure this was ever the consensus. I heard people have strong opinions in either direction. The people who liked having many explicit returns tended to do so because they felt it made the code more readable. One reason I heard against it was that it was easier to put a breakpoint right at the return of a method, if there's just one return point. I usually have tried to have just one return, but now and then would stray if it was awkward to do so. Now that I'm starting to use Scala, it has made me want to make each method an expression, whose result is automatically returned. So there's no return statement anywhere in the method. I suspect this will push me into factoring larger methods into multiple smaller ones more than I did in the past, and I suspect that will make my code better.
|
|