-
Notifications
You must be signed in to change notification settings - Fork 184
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
Design Doc - Explicit/Implicit and Builder Patterns #112
Comments
Hi folks. Here's my two cents. I believe it is helpful to highlight the reasons and situations when one may prefer one construction approach over another. The tradeoffs have different weights depending on the programming language used. Without an attempt to argue any specific choice, here is a laundry list of some concerns that I noticed over the years.
tl;dr: we should probably check how much any of the above named concerns play a role in Rust. With my library user hat on, I prefer smaller API surfaces: if an object's interface can be separated cleanly into "mutable" and "immutable" parts, I'd go for that. |
Thanks @filmil! I think you're using slightly different vocabulary than I do when you describe your position. Can you help me coalesce on one? I named the two appraoches Can you help me name the two code snippets and if you think there's a third one, add it, so that we can use the consistent naming when expressing opinions? |
Well, I'm not a particularly qualified or well read arbiter of style. But you asked. I'll try but take it with a grain of salt.
Reflecting a bit on this, I guess the distinction I meant to make was that of what object is in control of the creation of
So, rewriting my remark quoted above with this in mind, it would become " HTH. |
Thank you! Updated the names to make it easier to continue the conv. |
I agree with everything said above. The way I've understood the role of builders has been more simplistic (without previous experience of the example of C++ memory leak in a constructor):
I also like the model of starting off thinking of data/struct of interest as immutable except in controlled regions when necessary vs. the freeze() mechanism. As far as naming, FWIW, the AutoValue, Protobuf, and Guava collections cases all use the "Builder" name. In the way I've thought about uses of builders, with the discussion of "Options" vs. "Builder", maybe Options corresponds to the first (associative data) and Builder corresponds to the second (building up immutable data structure of arbitrary shape). My opinion & guess is that from a user perspective, since the code using them looks similar, the benefit of having just one term instead of two would outweigh the precision of naming<->semantics of two terms. |
@echeran do I understand correctly that from the snippets in my comment you suggest swapping the names? The first is "Options" and the latter is "Builder"? |
I think the names you have in those 2 cases sound fine. I guess I'm just pointing out that I hadn't thought of builders/options in terms of where the work occurs (whether in the constructor or not), but rather builders as being both a thing to build up associative data and an immutable collection. I'm slightly leaning towards calling everything one name ("Builder" ?) to incur less mental overhead for the user, but that's a hunch, and so not a strong preference. If you feel more strongly about having "Builder" and "Options", then that's fine with me. |
In terms of the choice between I am unsure of how users are currently using UnicodeSet (and I assume not many are doing this), but a potential drawback of using tldr: What @zbraniecki has in the issue statement currently for |
Depending on how much intermediate information we need to store, we may be able to support it. We could have If the recalculation requires a lot of data we want to have on the |
I'm going to extend this with proposed details on Errors in ICU4X, and file a PR. |
I ended up not including #118 in my PR. Will keep it for a separate PR. |
* Style Guide revision 2 Fixes #112. I shied away from extending the `errors` bit because the changes are large enough as is, and I'd prefer to keep #118 for another revision later. * Update style-guide.md * Address Shane's feedback * Reviewers feedback * Update style-guide.md * Update style-guide.md
This issue is intended to verify that we have a consensus on the two areas I'd like to extend our design doc with.
Explicit vs Implicit memory management (clone/copy)
Rationale: If we know we'll want to store a
Clone
of a struct, we should construct the API to get a cloned version already. This allows the reader of the user of this API to understand the memory cost.Builder vs. Constructor Options
Builder
vs.
Constructor Options
I'm a proponent of the latter, and I'd recommend it for us, but I know that ICU uses the former and I've seen Rust APIs going both ways.
My reasons are that I like that
MyStructOptions
have their own logic, calculations and validations and the user can perform the options at any point and in stages, and instead ofbuild
step,it would just get passed to the constructor of the final struct.
Also, the final struct doesn't need fields for intermittent data that gets computed out in the build step, is leaner and has less logic.
Finally, its mutability can be easier controlled separately from mutability of the options struct.
The text was updated successfully, but these errors were encountered: