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

Remove pointer arguments where they're not needed #197

Merged
merged 16 commits into from
Oct 16, 2024
Merged

Conversation

moskyb
Copy link
Contributor

@moskyb moskyb commented Oct 15, 2024

This PR: Is a bit of a doozy!

When initially creating this library, we got super trigger happy with arguments that are pointers, to the point of making most arguments pointers to structs, and most of the members of those structs pointers to scalar types.

in 99% of cases, most of these things don't need to be pointers, so i've done a refactor with the philosophy of:

  • Unless something must be a pointer - that is, there's a meaningful semantic difference between nil and its empty value - don't use pointers
  • If structs represent data only - they don't have any methods defined on them - return them as values, not pointers
  • If structs as arguments don't need to be modified, and there's no no meaningful semantic difference between nil and zero values, don't use pointers to structs as arguments
    • Notably, there's one strain of argument that we keep as pointers, and that's a variety of List options, eg here. In this case i do think there's a meaningful semantic difference between zero and nil values, but i'm happy to argue about this
  • In general, don't expose any unmarshalling semantics to users, again unless necessary. There was a pattern in this repo previously where you would have something like:
fooUpdate := buildkite.FooUpdate{/* set some fields to update /*}
resp, err := bkclient.FooService.Update(ctx, &fooUpdate)

where fooUpdate would be the fields to update, but then the result of the update call would also be unmarshalled into fooUpdate, rather than returned. this is an issue because encoding/json behaves surprisingly (#27172, #31924, #26946, #21092) when unmarshalling into non-empty structs, and is also just kind of a weird user interface IMO. this behaviour is replaced with something like

fooUpdate := buildkite.FooUpdate{/* set some fields to update /*}
updated, resp, err := bkclient.FooService.Update(ctx, fooUpdate)

where updated is a full buildkite.Foo struct. See b116725 for more info.

@moskyb moskyb changed the title [SUPER WIPNo more pointers [SUPER WIP] No more pointers Oct 15, 2024
@moskyb moskyb force-pushed the no-more-pointers branch 3 times, most recently from a7e65d3 to 8ef63c5 Compare October 16, 2024 01:58
@moskyb moskyb changed the title [SUPER WIP] No more pointers Remove pointer arguments where they're not needed Oct 16, 2024
@moskyb moskyb force-pushed the no-more-pointers branch 2 times, most recently from fcbc4b0 to 0668ba9 Compare October 16, 2024 03:33
@moskyb moskyb marked this pull request as ready for review October 16, 2024 05:20
Copy link
Contributor

@mcncl mcncl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🫗

@mcncl mcncl merged commit e6fff1d into main Oct 16, 2024
1 check passed
@mcncl mcncl deleted the no-more-pointers branch October 16, 2024 23:46
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

Successfully merging this pull request may close these issues.

2 participants