Solved

Streamlining the if condition

Posted on 2013-12-15
3
228 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

778 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