Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Avoid mixing value and pointer semantics all over the place #203

Open
Skarlso opened this issue Aug 19, 2019 · 2 comments
Open

Comments

@Skarlso
Copy link
Member

Skarlso commented Aug 19, 2019

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.

@michelvocks
Copy link
Member

Good idea! I like that 😄

@Skarlso
Copy link
Member Author

Skarlso commented Aug 21, 2019

I'm going to break this down into package sized issues to avoid a pr that has a ton of modifications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants