uber-go / guide

The Uber Go Style Guide.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Guideline: Avoid init

abhinav opened this issue · comments

Creating this to track adding a general guideline around use or misuse
of init().

We can formulate the full guideline in this issue, but the first piece
of that can be based on the following general advice:

If it doesn't behave exactly the same every time it's called,
regardless of environment, arguments, directory, etc., then it most
definitely does not belong in init().

I think our guideline should distill that and at least the following
points:

  • Avoid init() if you can
  • Pre-computation is okay as long as it's deterministic
  • Don't do any form of IO from init()
  • Don't rely on mutable global state in init() (command line
    arguments, environment variables, current directory, etc.)

@abhinav One of the suggested tips to improve performance of cloud functions in GCP is to leverage init()

Should this be a caveat to your mentioned guidance?

Hey @gbedoya, thanks for calling that out.

GCP is a special execution environment where certain best practices don't
apply to the entry point of your application. For example, we generally agree
that global variables and global state are bad, but in GCP that's the only way
to persist state between invocations.

Yeah, as we add this to the guide, we could state that this advice may not
apply to certain environments like GCP and AWS Lambda.