Solved

Too many if statements...

Posted on 2014-02-28
6
265 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
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 26

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
create a gui in perl 3 72
maven project error 5 48
egit plugin on eclipse 8 40
servlet example 17 5
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now