[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Too many if statements...

Posted on 2014-02-28
6
Medium Priority
?
308 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 400 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 28

Assisted Solution

by:dpearson
dpearson earned 400 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 1200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 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:
Suggested Courses

649 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