messense / jieba-rs

The Jieba Chinese Word Segmentation Implemented in Rust
MIT License
717 stars 43 forks source link

Prefer builder pattern to setters/getters in KeywordExtractConfig #101

Closed awong-dev closed 3 months ago

awong-dev commented 3 months ago

Fromt here https://github.com/messense/jieba-rs/pull/100/files#r1560432915

@messense said

I'd remove getters (not really useful) and use builder pattern for KeywordExtractConfig, like

let config = KeywordExtractConfig::builder()
    .add_stop_word("word")
    .use_hmm(true)
    // and other options
    .build();

or without a separate builder type:

let config = KeywordExtractConfig::default()
    .add_stop_word("word")
    .use_hmm(true)
    // and other options
    ;

The builder pattern is indeed nicer here. The getters may still be wanted to allow other languages to query the rust struct w/o replicating the state...but builder does seem like a definite win.

messense commented 3 months ago

It's fine to keep getters, in that case a separate builder type is better so we don't need to name getters get_XXX(), just use XXX() like let use_hmm = config.use_hmm();

messense commented 3 months ago

For example:

awong-dev commented 3 months ago

Gotcha. Will look into it. Need to sleep for now. :)