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
If every
if
andelse if
block ends with areturn
statement, then there is no need for anelse
statement (even if it too has areturn
clause) as the only way any code following theif... else if...
clauses would be executed is if these previousif
andelse if
blocks had not been executed. For example:should be turned into:
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 oldelse
section, but that should be a simple enough fix. For example:would need to be turned into: