jnichols-git/Gate

Consistency regarding if-initializers

Opened this issue · 0 comments

Go allows for the initialization of variables before if statements. For example, if we have a function foo() (string, error), you could:

if resultString, err := foo(); err != nil {...} else {...}

Both resultString and err belong to the scope of the if/else statement here. This is a handy way to manage functions that return errors (almost all of them) but isn't generally used routinely due to scope; often if err is nil, you want to do something with resultString, but doing it in the else block is awkward, so instead code will usually read

resultString, err := foo()
if err != nil {...}
...

The project as it is now doesn't handle this consistently, so I'm setting a guideline and leaving it as an issue to be fixed:

  • IF all values returned by the function are specifically related to the handling of an error returned by that function, then an initializer should be used.
  • OTHERWISE, regular syntax should be used.

I will be digging around for this for the time being, but it's a good place to start if you're unfamiliar with Go and want to familiarize yourself with syntax.