Early return

While doing my morning blog walk I discovered, much to my surprise, that the “early return” is considered “bad practice”.

Rather than reiterate all of the dialog surrounding the “single function exit point” debate, I direct you to the following links:

If my primary language was C/C++ (or any language without automatic garbage collection) then I would agree that an early return is bad practice; it is simply too difficult in most cases to prevent memory leaks with early returns. Since I have shed the albatross of manually managing memory, I have fewer constraints on when I may safely exit a method.

One of my favorite features of Eclipse (and, as usual, I don’t care if IDE XYZ supports it) is that I can click on the return type of a method and it will highlight all points at which the method returns (either due to throwing an exception, an exception bubbled up, or a return). See Highlight method exit points for more information. This feature certainly simplifies examining code with early returns.

My personal viewpoint on coding is: reducing the number of factors that one has to be concerned about, in general, reduces the complexity. Reduced complexity tends to lead to code that is less obfuscated, easier to maintain and less likely to contain defects. In order to reduce the number of factors that one has to be concerned about, I liberally employ the Guard Clause which (in most forms) is contrary to abolishing early returns.

But then again, what do I know? I also put comments in my code. *grin*


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s