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

Why are all the responses using pointers #1429

Closed
marcofranssen opened this issue Feb 13, 2020 · 4 comments
Closed

Why are all the responses using pointers #1429

marcofranssen opened this issue Feb 13, 2020 · 4 comments

Comments

@marcofranssen
Copy link

I noticed most responses are using slices of pointers. IMHO it would be better to not use pointers in the slices.

Also I don't think we should use pointers for strings in the JSON.

It will reduce the amount of required null checks, simplify working with the strings. Furthermore a slice is already kind of using pointers.

Is there any reason for this?

Would a PR removing the pointers in those locations be accepted?

@gmlewis
Copy link
Collaborator

gmlewis commented Feb 13, 2020

Please see #79, #45, and #19.

Thank you for asking, but no, a PR removing the pointers would not be accepted.

@marcofranssen
Copy link
Author

marcofranssen commented Feb 13, 2020

I get the nil checking, what I don't understand is why to apply this on string and []*SomeStruct. In those cases I think they should be removed. For integers it is debatable as well I think as also there, why would I want to check for nil in those cases?

I Would do something like this. That simplifies codebase and consumption of it a lot.

type SomeType struct {
     MyOtherObject  *OtherType
     Amount         Int64
     Text           string
     Collection     []CollectionItem
}

An empty string can then be treated the same as nil and 0 can also be treated similar. No more nil checks there. And I think in the case of slices it doesn't add value as well. Either there is an element in the slice or it is not. Why would you want to have nil elements in the slice?

Only the case were another struct like MyOtherObject is used I could think of user a pointer.

Am I completely missing out on something?

@gmlewis
Copy link
Collaborator

gmlewis commented Feb 13, 2020

This has been discussed at length... (whups, I forgot to include others like #537), but here are short answers:

  • an empty string will not tell you if the field was completely missing in the response. In this case, a string pointer of nil would indicate that the field was missing.
  • for slices, it prevents copying data around... sometimes these structs are large and would involve a lot of memory copying, so we stuck with a pattern. Additionally, slices of pointers make for loop behavior much easier for everyone. Here is an issue filed by Russ Cox on the subject: replace []Issue with []*Issue #180.

@marcofranssen
Copy link
Author

Thanks for explaining.

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