--

For example, I wouldn't pass a function in unless it's really critical for the dependency to be a function. If the function is just there to retrieve a value, I would simply pass in the value.

For example, you could have your second parameter be simply itemData, with a default of getItemData(id). That's a different place you could draw the line. However, it violates "principal of least surprise" in that the API hides that there's a second parameter and that it's tied to some internal state (so it won't be determinate--you could call it 5 different times and get 5 different results).

I've been very heavily influenced by the writing of Misko Hevery when he was at Google, such as this article that explains why that hidden state could result in weird bugs http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/

--

--

Amy Blankenship
Amy Blankenship

Written by Amy Blankenship

Full Stack developer at fintech company. I mainly write about React, Javascript, Typescript, and testing.

Responses (1)