Solved

Streamlining the if condition

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

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 43
ejb on wildfly 5 30
jboss 7.1 start up error 1 39
mysql jsp example issue 32 49
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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…

829 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