-
Notifications
You must be signed in to change notification settings - Fork 4
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
Function Getters and Setters, what is our consensus? #3
Comments
This double call can be avoided if the code is written in a different way. It's hard to answer your questions because I would say it depends if you speak about the Rust API or the Python API. On the Rust side, I would not use getter/setter systematically and would prefer to have struct with public fields. But not for all cases. |
Rust has proper abstract data types, so you can make fields public if necessary. Of course, if you want to encapsulate state/behavior in a struct, then you should keep those fields private and only allow access via methods, etc. One of the nice features of Rust's type system (and other ML-inspired static type systems) is that you can "make illegal states unrepresentable" [1,2] and enforce program invariants (using the type system to create correct programs & prevent logic bugs). I would recommend that public/API facing types shouldn't be allowed to be directly modified by the user (within reason), so that other parts of the library can assume that the data is valid. If you are familiar with Rust's standard library (the reference for idiomatic Rust), you will know that very few - if any - structs have public fields - most behavior is encapsulated via methods. I would also note that exposing data/behavior only via methods has the additional benefit of making it easier to maintain semver compatibility by abstracting away implementation details. [1] https://blog.janestreet.com/effective-ml-revisited/ |
In #2 i saw the following:
entry.get_header().get_header()
, which uses two getters to retrieve the attributes in a class.What is our consensus in getters and setters? From my experience, this makes projects less readable and somewhat slow to tangle through. Especially it is tedious to write getters and setters for all attributes or if we want to access attributes in classes within classes (within classes, ...), we chain those getters (as above). Basically, i am wondering what we should decide on:
Is there a "good practice" or guideline in Rust?
So what are you thoughts!?
(linking @david-bouyssie and @di-hardt , since you two are also mentioned in the PR)
The text was updated successfully, but these errors were encountered: