gaia
gaia copied to clipboard
[Refactor] Avoid mixing value and pointer semantics all over the place
In all the source code there are a lot of places where pointers are not needed or are declared all over the place.
When we first create a struct, we don't know it's memory address so using pointer semantic is pointless ( pun intended :D ).
Something like this:
mW := &mockWhatever{}
These things are confusing and are hard to read. This means you will have a cognitive load whenever you see this variable passed around because you have to remember that it will be an address. Instead the &
adds readability. Which means it should be used at places where it denotes sharing. Like on a return:
mW := Whatever{}
...
...
return &mW
This is now easy to read, you can see that it will return an address. Where as this:
mW := &Whatever{}
...
// Long method
return mW
Here you have to remember that you returned an address after reading a 100-200 line long method which was doing stuff all over the place.
Use &
when it's needed. And make sure a pointer exists because it need to be a pointer and not for the convenient way of checking for nil
upon a db lookup. We should use the ok
pattern for that.
Good idea! I like that 😄
I'm going to break this down into package sized issues to avoid a pr that has a ton of modifications.