stampit-org / stamp-specification

The Stamp Specification: Composables
434 stars 16 forks source link

Should options default to empty object? #93

Closed danielkcz closed 8 years ago

danielkcz commented 8 years ago

Without default it means that every initializer that wants to access options object has to take care of it on its own. Imagine using stamps in some bigger application with many initializers. Should really each one of them do it again and again?

const MyStamp = compose({
    initializers: [({ mydata } = {}) => {
        ...
    }]
});

MyStamp();

Frankly it already looks less readable with so much brackets. Initializer has to validate mydata anyway, so why not lift this small burden and give there a little insurance that object will be there if nothing was passed in?

Also if using anonymous fat arrow functions, the stack trace would be actually somewhat unhelpful locating which initializer is actually failing. With named functions it's bit easier, but that heavily depends on how good name has been used.

TypeError: Cannot read property 'mydata' of undefined
    at Object.stampit.default.compose.initializers (pen.js:5062:29)
    at http://s.codepen.io/boomerang/f4a63016cac103fc2fbac879211b8a111464779436316/index.html:4795:43
    at Array.reduce (native)
    at Stamp (http://s.codepen.io/boomerang/f4a63016cac103fc2fbac879211b8a111464779436316/index.html:4794:69)
    at pen.js:5067:5

See the Pen Playground for stampit by Daniel K. (@FredyC) on CodePen.

In case there is a really need to use something else than a plain object, it's still possible to pass it to factory anyway given that all initializers in the mix can handle that properly. I think it's good to promote the pattern of using options object with this.

unstoppablecarl commented 8 years ago

I think ensuring there is an empty object is a good idea.

koresar commented 8 years ago

I like it. @ericelliott how about you?

ericelliott commented 8 years ago

Sounds good to me. PR.