?
Solved

Too many if statements...

Posted on 2014-02-28
6
Medium Priority
?
299 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
Independent Software Vendors: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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
Course of the Month14 days, 11 hours left to enroll

771 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