Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shall I use guard clause, and try to avoid else clause?

Tags:

I've read (e.g. from Martin Fowler) that we should use guard clause instead of single return in a (short) method in OOP. I've also read (from somewhere I don't remember) that else clause should be avoided when possible.

But my colleagues (I work in a small team with only 3 guys) force me not to use multiple returns in a method, and to use else clause as much as possible, even if there is only one comment line in the else block.

This makes it difficult for me to follow their coding style, because for example, I cannot view all code of a method in one screen. And when I code, I have to write guard clause first, and then try to convert it into the form with out multiple returns.

Am I wrong or what should I do with it?

like image 386
chance Avatar asked Feb 03 '11 14:02

chance


People also ask

Should I use guard clauses?

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.

Where should you put a guard clause in a method?

A Guard Clause, also known as Early Return or Bouncer Pattern, is a common practice in programming, consisting in an early exit of a function based on preconditions check.

How do you write a guard clause?

The syntax of the guard statement is: guard expression else { // statements // control statement: return, break, continue or throw. } Note: We must use return , break , continue or throw to exit from the guard scope.

What is guard clause in Java?

A guard clause is simply a check that immediately exits the function, either with a return statement or an exception.


2 Answers

This is arguable and pure aesthetic question.

Early return has been historically avoided in C and similar languages since it was possible to miss resource cleanup which is usually placed at the end of the function in case of early return.

Given that Java have exceptions and try, catch, finally, there's no need to fear early returns.

Personaly, I agree with you, since I do early return often - that usually means less code and simpler code flow with less if/else nesting.

like image 177
Marko Avatar answered Sep 29 '22 20:09

Marko


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.

There is one stronger case of guard clauses - statements that validate input and throw exceptions when some argument is unacceptable, e.g. null. In that case you don't want to proceed with execution but wish to throw at the very beginning of the method. That is where guard clauses are the best solution because you don't want to mix exception throwing logic with the core of the method you're implementing.

When talking about guard clauses that throw exceptions, here is one article about how you can simplify them in C# using extension methods: How to Reduce Cyclomatic Complexity: Guard Clause. Though that method is not available in Java, it is useful in C#.

like image 29
Zoran Horvat Avatar answered Sep 29 '22 21:09

Zoran Horvat