10

Who Builds the Builder?

 3 years ago
source link: https://matklad.github.io/2020/08/12/who-builds-the-builder.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Who Builds the Builder?

Aug 12, 2020

This is a short note on the builder pattern, or, rather, on the builder method pattern.

TL;DR: if you have Foo and FooBuilder, consider adding a builder method to Foo:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
struct Foo { ... }

#[derive(Default)]
struct FooBuilder { ... }

impl Foo {
    fn builder() -> FooBuilder {
        FooBuilder::default()
    }
}

impl FooBuilder {
    fn build(self) -> Foo { ... }
}

A more minimal solution is to rely just on FooBuilder::default or FooBuilder::new. There are two problems with that:

First, it is hard to discover. Nothing in the docs/signature of Foo mentions FooBuilder, you need to look elsewhere to learn how to create a Foo. I remember being puzzled at how to create a GlobSet for exactly this reason. In contrast, the builder method is right there on Foo, probably the first one.

Second, it is more annoying to use, as you need to import both Foo and FooBuilder. With Foo::builder method often only one import suffices, as you don’t need to name the builder type.

Case studies:

Discussion on /r/rust.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK