Code Monkey home page Code Monkey logo

Comments (2)

mschoch avatar mschoch commented on June 17, 2024 1

So, the design of Bluge's Config was influenced heavily by dgraph's BadgerDB Options structure: https://github.com/dgraph-io/badger/blob/master/options.go#L26

I personally found it awkward when I first used BadgerDB, but over time I came to appreciate some of the way it worked. Here are some of the aspects of the design:

  1. Struct members are mostly public
  2. User-facing options/config settings named X have a corresponding WithX() function to set them
  3. All these methods return a copy of the struct itself (this allows method chaining)
  4. A default config is created using a function
  5. Constructors taking the config also pass by copy

Things I like about this:

  • Constructor taking copy means we generally aren't worried about people changing the config after they passed it in because we own our copy (NOTE: still have to be careful with the contents of the config)
  • Also because of the copies, it's possible to reuse config definitions (again, must be careful that contents are not pointers to things with state) This is highly desirable for expressing configuration in one (or few) places and reusing many places safely.
  • The method chaining feels very convenient for expressing configuration:
cfg := DefaultConfig(path).
    WithX(x).
    WithY(y)

To me, this is very readable. Subjectively, it is more readable than if the methods were called SetX(x).

Things I don't like about this:

  • Config object can be large, and passed by copy means static analysis (and soon possibly go tooling itself) may warn about copying large objects. I've made peace with this as config objects are typically worked with infrequently.

from bluge.

mschoch avatar mschoch commented on June 17, 2024

So, with that as the background, @steveyen, I'd like to better understand your criticism. As I understood it you don't like a method named WithX(x) for an option named X. Here are some alternatives:

  • Method named SetX(x), option named X
  • Method named X(x) option named x

My thinking is that SetX(x) is more standard in Go, but perhaps less readable in this case, because it implies you've mutated the object, when in fact, we're returning a new one.

My thinking is that X(x) is simply confusing because it lacks a verb describing the action.

Your thoughts?

from bluge.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.