stuphlabs / pullcord

A reverse proxy that allows scaling down a cloud service to zero servers without sacrificing (eventual) availability.
GNU Affero General Public License v3.0
4 stars 2 forks source link

No `else` needed if all `if` blocks end in `return` #153

Closed proidiot closed 5 years ago

proidiot commented 5 years ago

If every if and else if block ends with a return statement, then there is no need for an else statement (even if it too has a return clause) as the only way any code following the if... else if... clauses would be executed is if these previous if and else if blocks had not been executed. For example:

if err != nil {
    return err
} else {
    // do some things
}
// do more things

should be turned into:

if err != nil {
    return err
}
// do some things
// do more things

Somewhat nit-picky, but golint feels strongly on this point.

Unfortunately, that sometimes means taking a variable declaration that had previously been scoped to just the if...else blocks would need to be defined outside of those blocks if the variable is needed in the old else section, but that should be a simple enough fix. For example:

if val, present := table[key]; !present {
    return errors.New("key was not present")
} else {
    // do things with this val
}
// do things without that val, maybe even with something completely different being called val

would need to be turned into:

val, present := table[key]
if !present {
    return errors.New("key was not present")
}
// do things with val
// do other things, but now you have to be careful about calling other things val