mardi 3 juillet 2018

if vs. if/else statement?

I have this kind of code in a class :

if (reload == 0) {
    String mailSuccess = "send success mail";
} else {
    String mailOther = "send other mail";
} 

if (fileError1Exists == true) {
    String mailError1 = "send mail for error 1";
}

if (fileError2Exists == true ) {
    String mailError2 = "send mail for error 2";
}

if (fileError3Exists == true ) {
    String mailError3 = "send mail for error 3";
}

And maybe because I am still green with Java, i was wondering if this kind of syntax was clean ? Every time, i saw a "if/ese" expression, it was more like this :

if (condition == 0) {
    String mailSuccess = "send success mail";
} else if (fileError1Exists == true) {
    String mailError1 = "send mail for error 1";
} else if (fileError2Exists == true) {
    String mailError2 = "send mail for error 1";
} else if (fileError3Exists == true) {
    String mailError3 = "send mail for error 1";
} else {
    String mailOther = "send other mail";
} 

Is keeping those if statements one after another like this clean ? Obviously the first sample of code works because it is already in production. But do the 1st sample and the 2nd sample code do the same thing ? Normally, it does, right ? Or am I missing something because of my lack of experience ?

Thanks in advance for your feedback. I couldn't find my awnser on previous posts.

Aucun commentaire:

Enregistrer un commentaire