Results 1 to 10 of 10
- 03-21-2012, 03:47 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 14
- Rep Power
- 0
How to avoid many if else conditions
Hello, i have a java class consists of more that 30 if loops to check the field is initialized or not and to get the value, store it in another entity class,
How can i avoid such huge no of if loops to check the initialization :)
any help is appreciated
Thank you
- 03-21-2012, 03:59 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: How to avoid many if else conditions
'if's aren't loops.
You haven't given us any code to look at here.
If this is 30 different attributes to check then there's little you can do about the number of if's.
Note, if you do post code please use [code] tags [/code].Please do not ask for code as refusal often offends.
- 03-21-2012, 04:07 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Re: How to avoid many if else conditions
You can use switch as well... it is a little bit less messy. How do you use it?
lets say:
var Peter;
switch(Peter) {
case 12:
System.out.println("If Peter=12 then this line will be printed");
break;
case "day":
System.out.println("if Peter = "day" then the system will print this line");
break;
default:
System.out.println("Whatever you write here is going to print only if none of the situations above are true");
}
- 03-21-2012, 04:25 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: How to avoid many if else conditions
Yes, you could, except the example you've given is most definitely not Java...:)
Please do not ask for code as refusal often offends.
- 03-21-2012, 05:02 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Re: How to avoid many if else conditions
javascript lol it is basically the same thing lol just change var for int or something lol
- 03-22-2012, 05:59 AM #6
Member
- Join Date
- Jan 2012
- Posts
- 14
- Rep Power
- 0
Re: How to avoid many if else conditions
all those are classes and isInitialized and getValue methods i am getting the value and storing it in object - and this object i am passing it to persist method in another class- now i want get rid of these if blocksJava Code:public static RA_Huawei_Sgsn ParserIntoCdrEntity( CallEventRecord callEventRecord, RA_Huawei_Sgsn sgsn) throws AsnFatalException { // RA_Huawei_Sgsn sgsn = new RA_Huawei_Sgsn(); try { if (callEventRecord.getCurrentChoice().equals( callEventRecord.sgsnPDPRecord)) { if (callEventRecord.sgsnPDPRecord.recordType.isInitialized) { sgsn.setRecordtype(callEventRecord.sgsnPDPRecord.recordType .getValue()); }// recordtype if (callEventRecord.sgsnPDPRecord.networkInitiation.isInitialized) { sgsn.setNetworkInitiation( callEventRecord.sgsnPDPRecord.networkInitiation.getValue()); }// networkInitiation if (callEventRecord.sgsnPDPRecord.servedIMSI.isInitialized) { sgsn.setServedIMSI(MapUtils .getNumberString(callEventRecord.sgsnPDPRecord.servedIMSI .getValue())); }// servedIMSI if (callEventRecord.sgsnPDPRecord.servedIMEI.isInitialized) { .getNumberString(callEventRecord.sgsnPDPRecord.servedIMEI .getValue())); }// servedIMEI
- 03-22-2012, 07:56 AM #7
- 03-22-2012, 10:11 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: How to avoid many if else conditions
There's not much you can do there to reduce the amount of code.
Those checks, since they're all for different attributes of both sgsnPDPRecord and sgsn, can't be merged in any way.
Now, depending on what else is going on in this method, you could extract this load of stuff out into another method. It seems to be translating from one class to another, so some form of Adapter? You wouldn't get rid of the code, but you would get it away (assuming it isn't already) from any non-translating logic.Please do not ask for code as refusal often offends.
- 03-22-2012, 11:41 AM #9
Member
- Join Date
- Jan 2012
- Posts
- 14
- Rep Power
- 0
Re: How to avoid many if else conditions
u mean to say so its nt possible to get rid of these if blocks :(
what are the alrenate ways to check the conditions - like creating interface returning boolean values intialized or not if returns true then calling getValue method to store it in object
- 03-22-2012, 12:39 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: How to avoid many if else conditions
Since you only appear to want to store something in sgsn if something else is set, then I don't see how you can avoid the if statement.
You can hide it away, but not remove it entirely.
Which is why I suggested some options above.Please do not ask for code as refusal often offends.
Similar Threads
-
Problem in while conditions
By davie89 in forum New To JavaReplies: 2Last Post: 03-20-2012, 12:37 AM -
while and proper conditions for if
By Saletra in forum New To JavaReplies: 11Last Post: 08-25-2010, 10:37 AM -
Asking for a username with conditions
By ScentOfAWookie in forum New To JavaReplies: 2Last Post: 03-20-2009, 05:53 AM -
[SOLVED] if else conditions not working. T_T
By uncanny in forum Advanced JavaReplies: 4Last Post: 10-17-2008, 05:44 AM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks