Solved

Too many if statements...

Posted on 2014-02-28
6
289 Views
Last Modified: 2014-03-02
Hi,

Is there a way of writing the code if there is too many "ifs"?

if(ssomething != null)
do this
if(anotherthing != null)
do that
if(thirdthing != null)
do ...

thanks ...
0
Comment
Question by:zizi21
[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
6 Comments
 
LVL 19

Assisted Solution

by:regmigrant
regmigrant earned 100 total points
ID: 39896456
You could use a switch block which allows you to execute a given code block based on a particular case being true.

Here's a primer:
http://www.tech-recipes.com/rx/668/java-switchcase-statement-syntax/
0
 
LVL 27

Assisted Solution

by:dpearson
dpearson earned 100 total points
ID: 39896585
If you can't use a switch for some reason, you can also use the fact that AND evaluates left to right to remove the ifs:

boolean ok = true ;

ok = (a != null) && doSomething1(a) ;
ok = (b != null) && doSomething2(b) ;
ok = (c != null) && doSomething3(c) ;

For this pattern you generally make the doSomethings() return boolean instead of void.

You can even chain the ok values if you want to be a bit fancier:

ok = (a != null) && doSomething1(a) && ok ;
ok = (b != null) && doSomething2(b) && ok ;
ok = (c != null) && doSomething3(c) && ok ;

ok only true at the end if all 3 succeeded.

or:

ok = ok && (a != null) && doSomething1(a) ;
ok = ok && (b != null) && doSomething2(b)  ;
ok = ok && (c != null) && doSomething3(c)  ;

Only does each step if the previous one succeeds.

Doug
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39896942
The way your pseudo-code above is presented, the ifs are all discrete - one has no bearing on the subsequent ones. So you already have the most efficient model for that. Would be different if the conditions were the same in all three cases.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:zizi21
ID: 39896954
sorry for not explaining in more detail earlier..

if((str = somefunction != null)
      a = somevalue;            
else
  return null;

you can also return a boolean value. the problem is that you need to do it for many variables and at times, that lengthens the code .. .. thought maybe, there is a simpler way ...
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 300 total points
ID: 39896961
Ah then that's different.

You could use the ternary operator in the right context : var = condition ==red?green:blue;
0
 

Author Closing Comment

by:zizi21
ID: 39898304
Thanks
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free 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
jdbc error in jsp application 20 88
Android development question 2 108
jmss example java 2 48
dao vs facade design patterns 2 69
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…
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…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

734 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