Solved

# gauard clause in java

Posted on 2014-10-02
152 Views
Hi,

I am going through below link

http://refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html

I see below code can be modified better as given further below
double getPayAmount() {
double result;
else {
if (_isSeparated) result = separatedAmount();
else {
if (_isRetired) result = retiredAmount();
else result = normalPayAmount();
};
}
return result;
};

double getPayAmount() {
if (_isSeparated) return separatedAmount();
if (_isRetired) return retiredAmount();
return normalPayAmount();
};

I wonder what is the meaning of 'guard clause'  what is its significance.

0
Question by:gudii9

LVL 32

Assisted Solution

ste5an earned 167 total points
ID: 40358314
"Guard Clause" means that you exit the code by an early return, avoiding the "evaluation" of the further else parts.
0

LVL 26

Assisted Solution

dpearson earned 166 total points
ID: 40358340
You might find this example:
http://blog.mafr.de/2009/06/12/a-case-for-guard-clauses/

easier to understand.  It points out more clearly exactly what the "guard clause" is in the code.  It's "guarding" the later code from being executed, because some condition has not been met.  So you can edit the function immediately.

Hope that helps,

Doug
0

LVL 7

Author Comment

ID: 40358618
double getPayAmount() {
if (_isSeparated) return separatedAmount();
if (_isRetired) return retiredAmount();
return normalPayAmount();
};

In above code where and how they are doing

"Guard Clause" means that you exit the code by an early return, avoiding the "evaluation" of the further else parts.

0

LVL 37

Accepted Solution

zzynx earned 167 total points
ID: 40358821
What is your remaining question exactly?

These are the three guard clauses:

if (_isSeparated) return separatedAmount();
if (_isRetired) return retiredAmount();

If the variable _isSeparated is true, we immediately return separatedAmount()
If the variable _isRetired is true, we immediately return retiredAmount()

That's much better readable and understandable than the first code you posted
0

## Featured Post

### Suggested Solutions

countX 22 70
pairstar challenge 2 41
topping2 challenge 13 59
Starting to learn JAVA, 7 47
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.