Undoubtedly. This example's purpose isn't to bring an exact implementation to the light, but to show that there may be small pieces of logic that depend on side effects. Perhaps you see a better example to this?
The getTimestamp modification you suggest also produces a different function. It converts any given date into a timestamp. Had I a requirement to use it to print only the current date into a timestamp, I'd have to pass it Date.now upon each invocation. Imho, that'd be a good sign that the date belongs in the function itself, given the aforementioned requirements.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
There would be no need for mocks for the
getTimestamp
function if it would allow to hand in the date.Impure functions are always harder to test.
Undoubtedly. This example's purpose isn't to bring an exact implementation to the light, but to show that there may be small pieces of logic that depend on side effects. Perhaps you see a better example to this?
The
getTimestamp
modification you suggest also produces a different function. It converts any given date into a timestamp. Had I a requirement to use it to print only the current date into a timestamp, I'd have to pass itDate.now
upon each invocation. Imho, that'd be a good sign that the date belongs in the function itself, given the aforementioned requirements.