-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add _.noop #1497
add _.noop #1497
Conversation
👍 I dig it, I've had it in my implementation for a bit. |
The advantage to |
👍 |
Quite so. But if you're already checking if the function is a noop or not — then why not (in that particular case) simply pass Any other good arguments for this? |
It's subjective of course, but I like the consistency of having a thing be what it is intended to be, of having it abide by a single interface. When a function variable is guaranteed to hold a function and not just sometimes a function, I can rely on dealing with it just one way -- invoking it. This follows suit with the premise behind the null object pattern. How much of programming should really be about side-stepping nulls? I'd rather use practices that allow me to reduce null checking. If we allow a function variable to sometimes be null, then every site where it might be invoked needs a null guard. In practice I find that guaranteeing an interface (null object, |
Your first two paragraphs are quite right — but are also completely add odds with what you said earlier:
Yes, it's nice to consistently always have a passed-in function if you're going to blindly call it. But if you're going to explicitly check if it's a null function before (not) calling it, then you've just reinvented the |
I retracted the benefit I formerly mentioned in favor of the consistent interface. I am not suggesting "to explicitly check if it's a null function before (not) calling it." I suggested that blindly calling the function is better than checking what it is. Nix my former statement if you like; I think you're paying too much attention to it. |
And for that, |
|
I'd suggest that |
I agree. @jashkenas is quite correct that _.noop is not necessary. In many cases Underscore provides a way to express ideas more clearly; ideas which could have been expressed without help from a library. I'll provide an example. function h() {
g.apply(null, arguments);
} Note that we were able to achieve this without help from a library. With Underscore, though, we can express the same thought more clearly: var h = _.compose(function() {}, g); var h = _.compose(_.constant(void 0), g); var h = _.compose(_.constant(), g); We could take this a step further with _.noop: var h = _.compose(_.noop, g); One advantage of _.noop is that it provides the canonical empty function. I can imagine projects using a mix of |
PR's already merged but I thought I'd also add another reason for having this 'canonical empty function' around. In my project, it's This is handy when a base class provides noop as the default implementation of a method which is then bound. eg: Foo.prototype.onSomeEvent = _.noop;
Foo.prototype.setup = function () {
this.on('someEvent', this.onSomeEvent); // effectively does nothing
} |
This is an unassuming function, but I've found myself defining it more often than one might imagine.
An example of a situation in which this function is useful: