?
Solved

gauard clause in java

Posted on 2014-10-02
4
Medium Priority
?
163 Views
Last Modified: 2014-10-05
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;
  if (_isDead) result = deadAmount();
  else {
    if (_isSeparated) result = separatedAmount();
    else {
      if (_isRetired) result = retiredAmount();
      else result = normalPayAmount();
    };
  }
  return result;
};  

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

Open in new window


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

Please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 35

Assisted Solution

by:ste5an
ste5an earned 668 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 28

Assisted Solution

by:dpearson
dpearson earned 664 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

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

Open in new window


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.

Please advise
0
 
LVL 37

Accepted Solution

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

These are the three guard clauses:

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

Open in new window


If the variable _isDead is true, we immediately return deadAmount()
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question