Solved

Streamlining the if condition

Posted on 2013-12-15
3
223 Views
Last Modified: 2013-12-16
Hi,

I have a Piece of code with lot of if condition complexity and some more. Here is the sample one

if(isEnable("xxxxx")){
				
	if(isEnable("yyyyy")){
	
		if("zzzz".equalsIgnoreCase(tttt)){
	
			enableFlag("QuestionUserRole");
			xxxxxUserRole=new SAXQuestionUserRole();
			        	}
						
					}
                     else if("useryyyyy".equalsIgnoreCase(tttt)) {
						
                        enableFlag("yyyyy");
			useryyyyy = new LinkedHashSet<SAXQuestionUserRole>();
					}

Open in new window


My Approach would be like this below, can some body pls verify is this correct way to do that instead the above one

if(isEnable("xxxxx")) && (isEnable("yyyyy")) && (("zzzz".equalsIgnoreCase(tttt))
	              {
			enableFlag("QuestionUserRole");
			xxxxxUserRole=new SAXQuestionUserRole();
			        	}
		 
 else if (("useryyyyy".equalsIgnoreCase(tttt))  
                        {

                        enableFlag("yyyyy"); 
			useryyyyy = new LinkedHashSet<SAXQuestionUserRole>();
			}

Open in new window

0
Comment
Question by:roy_sanu
3 Comments
 
LVL 26

Assisted Solution

by:dpearson
dpearson earned 167 total points
ID: 39719682
No I think it should be:

if(isEnable("xxxxx")) && (isEnable("yyyyy")) && (("zzzz".equalsIgnoreCase(tttt)) {
}
else  if (isEnable("xxxxx")) && (!isEnable("yyyyy")) && (("useryyyyy".equalsIgnoreCase(tttt)) {
}

Better to write this as:

boolean x = isEnable("xxxxx") ;
boolean y = isEnable("yyyyy") ;
boolean z = ("zzzz".equalsIgnoreCase(tttt) ;
boolean userY = ("useryyyyy".equalsIgnoreCase(tttt) ;

// So then the ifs become easy to read:
if (x && y && z) {
   ...
}
else if (x && !y && userY) {
   ...
}

Doug
0
 
LVL 16

Assisted Solution

by:Peter Kwan
Peter Kwan earned 166 total points
ID: 39719685
For the original code, it should be re-format like this:

if(isEnable("xxxxx")) {
   if (isEnable("yyyyy") && "zzzz".equalsIgnoreCase(tttt)) {
      enableFlag("QuestionUserRole");
      xxxxxUserRole=new SAXQuestionUserRole();
   }
  else if ("useryyyyy".equalsIgnoreCase(tttt)) {
      enableFlag("yyyyy");
      useryyyyy = new LinkedHashSet<SAXQuestionUserRole>();
  }
}

Open in new window


P.S. The closing brace is missing in the original code.
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 167 total points
ID: 39720106
You can also think about what the ternary operator might offer, such as :

boolean b = isEnable("xxxxx") ? (isEnable("yyyyy")&&("zzzz".equalsIgnoreCase("zzzz"))?true:false):false;

if(!b&&"useryyyyy".equalsIgnoreCase("useryyyyy")){
     //;	
}

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now