Forum Discussion

Mis3's avatar
Mis3
Frequent Contributor
3 years ago

Please help me to improve my awkward code

Below is my code to validate an input string.  It is working but it is awkward.  Any suggestions to improve the code?   

1. First I check if the input string.  If it does not start with 431, 1431, 204 or 1204, exit the loop.

2. Then I check if the input string starts with "1".  If not, add "1" to the string.

Thanks.

 

rowdata = rowsData[i]
String[] data = rowdata.split(",")

if ( !rowdata.startsWith('431') && !rowdata.startsWith('1431') && !rowdata.startsWith('204') && !rowdata.startsWith('1204') )
{
log.info " Seq="+i+ " Invalid line " + " ["+ data[0] + "]"
report << '\n'+sdf.format(log_date)+" Seq="+i+ " Invalid line " + "[" + data[0] + "]"
continue
}
if ( rowdata.startsWith('1') )
{ msisdn_1 = data[0]  }
else
{ msisdn_1 = "1" + data[0]  }

3 Replies

  • Mis3's avatar
    Mis3
    Frequent Contributor

    Anyone?

    Is there a better way to write this code?

    if ( !rowdata.startsWith('431') && !rowdata.startsWith('1431') && !rowdata.startsWith('204') && !rowdata.startsWith('1204') )

     

    • nmrao's avatar
      nmrao
      Champion Level 3
      Not sure why are you feeling that it is awkward.
      • Mis3's avatar
        Mis3
        Frequent Contributor

        I do not know Java programming at all.  I used to code maybe 30 years ago in other programming languages  which emphasis on readability.  Little that I know about Java coding,  I find it not easy to read at all. 

        I myself find these lines of code awkward in the sense that it is not easy to read.   Given the same requirements, how would you code these 2 if statements?

        if ( !rowdata.startsWith('431') && !rowdata.startsWith('1431') && !rowdata.startsWith('204') && !rowdata.startsWith('1204') )
        {


        continue
        }
        if ( rowdata.startsWith('1') )
        { msisdn_1 = data[0]  }
        else
        { msisdn_1 = "1" + data[0]  }