3 ms·
Nowadays I use builders with validation and defaults. It makes life so easier when creating instances or testing them. This is for c and Java. Not sure how the
by mothsonasloth 7y ago
Nowadays I use builders with validation and defaults. It makes life so easier when creating instances or testing them.
This is for c and Java. Not sure how the builder pattern would work for Rust.
- kibwen 7y agoMany Rust libraries actually employ the builder pattern extensively (sometimes as a reaction to the lack of default/keyword arguments); it's so widely-accepted that the necessity of error-handling while constructing items via method-chaining (as featured by the builder pattern) was a key argument for the postfix `?` error-propagation operator.
- hobofan 7y agoThe builder pattern works just as well in Rust, and is in fact used pretty commonly in the ecosystem whenever you want to create a struct that has quite a few configuration options, but you don't want to specify all of them (Rust doesn't have default argument values).
- munchbunny 7y agoIn my experience with C/C++/C#/Java I pretty much always use factory methods and hidden constructors for complex object construction in order to hide the constructor semantics and failure modes behind a standard function abstraction. It has the added bonus of being more testable.