Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming style: should you return early if a guard condition is not satisfied?

One thing I've sometimes wondered is which is the better style out of the two shown below (if any)? Is it better to return immediately if a guard condition hasn't been satisfied, or should you only do the other stuff if the guard condition is satisfied?

For the sake of argument, please assume that the guard condition is a simple test that returns a boolean, such as checking to see if an element is in a collection, rather than something that might affect the control flow by throwing an exception. Also assume that methods/functions are short enough not to require editor scrolling.

// Style 1 public SomeType aMethod() {   SomeType result = null;    if (!guardCondition()) {     return result;   }    doStuffToResult(result);   doMoreStuffToResult(result);    return result; }  // Style 2 public SomeType aMethod() {   SomeType result = null;    if (guardCondition()) {     doStuffToResult(result);     doMoreStuffToResult(result);   }    return result; } 
like image 787
John Topley Avatar asked May 28 '10 11:05

John Topley


People also ask

Should you use early returns?

One of the reasons that you could consider using early returns is that it keeps your code visually flatter. There is no need for extra indentation that you would have had if you went for the alternative route that uses a wrapping if-statement. This makes code more readable.

When can you use early returns?

R eturn early is the way of writing functions or methods so that the expected positive result is returned at the end of the function and the rest of the code terminates the execution (by returning or throwing an exception) when conditions are not met.

What is an early return?

Early return to work program is designed to get employees who have been out of work due to injury or illness to come back to the work sooner by giving them easier jobs until they regain their full capacity.

Are guard clauses good?

Guard clause is a good idea because it clearly indicates that current method is not interested in certain cases. When you clear up at the very beginning of the method that it doesn't deal with some cases (e.g. when some value is less than zero), then the rest of the method is pure implementation of its responsibility.


2 Answers

I prefer the first style, except that I wouldn't create a variable when there is no need for it. I'd do this:

// Style 3 public SomeType aMethod() {    if (!guardCondition()) {     return null;   }    SomeType result = new SomeType();   doStuffToResult(result);   doMoreStuffToResult(result);    return result; } 
like image 111
unbeli Avatar answered Sep 25 '22 17:09

unbeli


Having been trained in Jackson Structured Programming in the late '80s, my ingrained philosophy was always "a function should have a single entry-point and a single exit-point"; this meant I wrote code according to Style 2.

In the last few years I have come to realise that code written in this style is often overcomplex and hard to read/maintain, and I have switched to Style 1.

Who says old dogs can't learn new tricks? ;)

like image 43
Eight-Bit Guru Avatar answered Sep 23 '22 17:09

Eight-Bit Guru