简体   繁体   中英

How can I optimize this code (return statements)?

So while I was typing this question, I found a workaround for a "missing return statement" error. But I still don't think this is the proper way of doing it. As you can see I have some nested if statements. I want both conditions to be met before returning anything, but I had to place the return statements outside of the nested if statements. If the last condition isn't met doubt this should cause much problem since an empty string will be returned, but I just feel as if this isn't the best way of going about doing things.

1st Edit: with my current update, i'm still missing a return statement. I could do the same fix i applied but I feel as if it is innapropriate.

public String findProtein(String dna) {
int start = dna.indexOf("atg");

int stop1 = dna.indexOf("tag", start + 3);
int stop2 = dna.indexOf("tga", start + 3);
int stop3 = dna.indexOf("taa", start + 3);

String subStr1 = dna.substring(start, stop1);
String subStr2 = dna.substring(start, stop2);
String subStr3 = dna.substring(start, stop3);

boolean geneFound = false;

if (subStr1.length() % 3 == 0) {
    geneFound = true;
    return subStr1;
}

if (geneFound == false) {
    if (subStr2.length() % 3 == 0) {
            geneFound = true;
        }
        return subStr2;
    }

    if (geneFound == false) {
        if (subStr3.length() % 3 == 0) {
            geneFound = true;
        }
        return subStr3;
    }

    if (geneFound == false) {
        return "";
    }
}

2nd Edit: additional code

private void stopCodon(String gene){
    //This prints out the last 3 characters of the gene

    String stopCodon = gene.substring(gene.length() - 3);
    System.out.println(stopCodon);
}

public void testing() {
    String a = "ataaactatgttttaaatgt";

    String result = findProtein(a);

    stopCodon(result);         
}

If it were me, I would edit the following logic

if( subStr1.length() % 3 ==0 ){
    geneFound = true;
    return subStr1;
}

if(geneFound == false){
    if(subStr2.length( )% 3 ==0 ){
        geneFound = true;
    }return subStr2;
}

if(geneFound == false){
    if(subStr3.length( )% 3 ==0 ){
        geneFound = true;  
    }
    return subStr3;
}
if (geneFound == false){
    return "";
}

To the following using else if statements:

if( subStr1.length() % 3 ==0 ){
    return subStr1;
} else if (substr2.length()%3==0){
    return substr2;
} else if (substr3.length()%3 == 0) {
    return substr3;
} else {
    return null;
}

I'm also not sure if String subStr1 = dna.substring(start,stop1); is something you want since an Exception will be thrown if the stop codon doesn't exist, but it would be hard to judge without you giving us additional information.

Added

Kind of saw this coming, but if you look at the description for indexOf

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int)

the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

If you want to check if the substring exists, you check if the index is -1

I'm only gonna go through an example for the first substring

int stop1 = dna.indexOf("tag", start + 3);
if(stop != -1) {
    return dna.substring(start, stop1);
}

You should start by checking if the start codon exists at all and return null if it doesn't exist immediately, since locations of stop codons are useless without start codons.

Hopefully this helps

  if( subStr1.length() % 3 ==0 ){
       geneFound = true;
        result = subStr1;

  }else if(geneFound == false){
        if(subStr2.length( )% 3 ==0 ){
            geneFound = true;
        }
        result = subStr2;
  }else if(geneFound == false)
        if(subStr3.length( )% 3 ==0 ){
            geneFound = true;  
        }
       result = subStr3;
  }

    if (geneFound == false){
       result = "";
    }
return result;

result is of type String.

However any one of three if statements will return the value. If not fourth if statement will return the value.

You can assign the result to a variable and return it at the end

Since you return a value and the boolean is a local variable, it doesn't really matter if you change the boolean value or not in this code. I really don't see a use for it at the time. I simplified the code following your logic!

public String findProtein(String dna) {

    int start = dna.indexOf("atg");
    int stop1 = dna.indexOf("tag", start+3);
    int stop2 = dna.indexOf("tga",start+3);
    int stop3 = dna.indexOf("taa",start+3);


    String subStr1 = dna.substring(start,stop1);
    String subStr2 = dna.substring(start,stop2);
    String subStr3 = dna.substring(start,stop3);

    if(subStr1.length() % 3 == 0 ) {
        return subStr1;
    }

    if(subStr2.length() % 3 == 0 ){
        return subStr2;
    }

    if(subStr3.length( )% 3 ==0 ){
        return subStr3;
    }

    return "";
}

Why don't you return something like this ?

 public String findProtein(String dna) {

     String valueToBeReturned = "";
     if(condition 1){
          valueToBeReturned = "value1"
       }

     if(condition 2){
          valueToBeReturned = "value2"
       }

   //Rest of the conditions 

   return valueToBeReturned; //Finally return the specific value
   }

How about remove unnecessary block of code?

if (geneFound == false) { return ""; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM