gocast icon indicating copy to clipboard operation
gocast copied to clipboard

Remove side effects from code caused by `init()`

Open joschahenningsen opened this issue 2 years ago • 0 comments

Our code utilizes the init() function go automatically runs at package initialization. Most people recommend avoiding this https://github.com/uber-go/guide/blob/master/style.md#avoid-init as it makes the code unclear and less testable. For instance: The worker could try communicate with TUM-Live when tests are run because it's initialized using init().

Avoid depending on the ordering or side-effects of other init() functions. While init() ordering is well-known, code can change, and thus relationships between init() functions can make code brittle and error-prone.

This is an issue as well in our dao packages. When the daos are initialized the DB variable might not be set yet, causing calls to somedao.db to fail.

joschahenningsen avatar Sep 28 '22 07:09 joschahenningsen