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

Get/set design guidelines #882

Closed
bvssvni opened this issue Apr 19, 2015 · 0 comments
Closed

Get/set design guidelines #882

bvssvni opened this issue Apr 19, 2015 · 0 comments

Comments

@bvssvni
Copy link
Member

bvssvni commented Apr 19, 2015

When writing methods for getting and setting options, you can have &mut self, self, &self etc. which can lead to inconsistent usage across libraries. The conventions here are used in the piston core and piston-graphics, and I suggest them as guidelines for all Piston libraries.

  • Whenever a method starts with set_ it should take &mut self
  • Whenever there is a set_ method and you need a get, it should start with get_
  • Methods without a prefix are either read only or take self and returns Self

This means that builder patterns doesn't need set_ prefixes, for example:

let events = events.max_fps(60).ups(120);

The method without prefix never gets in the way for the read only version, because when a property is read only you do not need a builder version. It also makes it possible to add either a read only or builder method later without breaking the API.

For mutable references to the interior of an object:

  • get_foo_mut when there is a set_ method
  • foo_mut when there is no set_ method

Rust API guidelines

The Rust API guidelines distinguishes between Builder patterns and ordinary objects, so the get_ prefix is removed for ordinary objects. See https://aturon.github.io/style/naming/README.html#getter/setter-methods-[rfc-344].

In several Piston libraries the distinction between builders and ordinary objects is blurry, so we use get_ to avoid breaking the API.

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

No branches or pull requests

1 participant