Solved

Streamlining the if condition

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java: The Public Class Main 4 45
collection output issue 9 66
jmeter usage 4 38
JDBC performance 8 29
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …

726 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