Results 1 to 10 of 10
  1. #1
    Addymails is offline Member
    Join Date
    Jan 2012
    Posts
    14
    Rep Power
    0

    Default 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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default 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.

    ** This space for rent **

  3. #3
    deyvi1993 is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default 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");
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default 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.

    ** This space for rent **

  5. #5
    deyvi1993 is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default 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

  6. #6
    Addymails is offline Member
    Join Date
    Jan 2012
    Posts
    14
    Rep Power
    0

    Default Re: How to avoid many if else conditions

    Java 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
    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 blocks

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: How to avoid many if else conditions

    Quote Originally Posted by deyvi1993 View Post
    javascript lol it is basically the same thing lol just change var for int or something lol
    How to avoid many if else conditions-hamhamster.jpg

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: How to avoid many if else conditions

    Quote Originally Posted by Addymails View Post
    Java 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
    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 blocks
    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.

    ** This space for rent **

  9. #9
    Addymails is offline Member
    Join Date
    Jan 2012
    Posts
    14
    Rep Power
    0

    Default 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

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default 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.

    ** This space for rent **

Similar Threads

  1. Problem in while conditions
    By davie89 in forum New To Java
    Replies: 2
    Last Post: 03-20-2012, 01:37 AM
  2. while and proper conditions for if
    By Saletra in forum New To Java
    Replies: 11
    Last Post: 08-25-2010, 11:37 AM
  3. Asking for a username with conditions
    By ScentOfAWookie in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 06:53 AM
  4. [SOLVED] if else conditions not working. T_T
    By uncanny in forum Advanced Java
    Replies: 4
    Last Post: 10-17-2008, 06:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •