Giter Club home page Giter Club logo

Comments (6)

connec avatar connec commented on May 2, 2024 1

@rkichenama that example does not work because config has already mutated when the 'overrides' are applied:

let config = { key: 'override' }
Object.assign(config, { key: 'default' }, config)
console.log(config.key) // default

Hence the need for an additional Object.assign.

@kwelch that option is equivalent to the non-mutating version I specified - the defaults are already in a new object, so merging them into another new object is redundant.


Anyway, looks like this was fixed in d00ca4c with @kwelch's version.

from clean-code-javascript.

halfzebra avatar halfzebra commented on May 2, 2024

Sorry if I'm overthinking it, but I find it rather confusing to provide the example of object mutation in a procedural style, while advising to favor functional programming over imperative programming.

from clean-code-javascript.

rkichenama avatar rkichenama commented on May 2, 2024

If the purpose is to mutate config to add defaults but keep changes, then the example should be

function createMenu(config) {
  Object.assign(
    config, // object to merge changes into
    {
      title: 'Foo',
      body: 'Bar',
      buttonText: 'Baz',
      cancellable: true
    }, // defaults
    config // overrides
);
}

createMenu({ title: 'Not Foo' });

IMHO, I think this is the case since there is no return and we are dealing with objects, passed by reference.

from clean-code-javascript.

kwelch avatar kwelch commented on May 2, 2024

I would say the best option would be to maintain the passed in parameter and create a new object all together that use defaults then user param.

function createMenu(options) {
  let config = Object.assign({}, {
      title: 'Foo',
      body: 'Bar',
      buttonText: 'Baz',
      cancellable: true
    }, // defaults
    options // overrides
  );
}

from clean-code-javascript.

rkichenama avatar rkichenama commented on May 2, 2024

from clean-code-javascript.

kwelch avatar kwelch commented on May 2, 2024

@connec I agree ours are the same, I just prefer not editing parameters if you are attempting to be functional.

from clean-code-javascript.

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.