Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a name for an empty function? [closed]

Tags:

javascript

If you have a function like:

function FunctionT(){
//do something
}

Would you refer to this as being empty or undefined or either works? Is there an official name for this?

like image 303
Brandon Avatar asked Dec 25 '22 12:12

Brandon


2 Answers

A "noop" or "null function":

var noop = function(){}

In computer science, a NOP or NOOP (short for No Operation) [...] a command that effectively does nothing at all.

In computer science, a null function (or null operator) is subroutine that returns no data values and leaves the program state unchanged

Sources, http://en.wikipedia.org/wiki/NOP, https://en.wikipedia.org/wiki/Null_function

like image 82
elclanrs Avatar answered Jan 05 '23 16:01

elclanrs


When I create such functions, I consider them "sentinel functions" - similar to identity functions in which the return value is either discarded or is a default a value (but not the identity); there should be no side-effects.


The terminology "sentinel function" is not common, but I find that the role they play is similar to that of sentinel values. (Although, one could argue that a function in JavaScript is a value and thus a "sentinel function" is a sentinel value..)

A related practice, used in slightly different circumstances, is to place some specific value at the end of the data, in order to avoid the need for an explicit test for termination in some processing loop, because the value will trigger termination by the tests already present for other reasons.

Rewriting this for a "sentinel function"

A related practice, used in slightly different circumstances, is use an empty function, in order to avoid the need for an explicit test for a function-object when invoking a function expression, because the sentinel function-object can be called.

For instance:

function doStuff(data, callback) {
   callback = callback || function(){};
   callback(compute(data));
}
like image 28
user2864740 Avatar answered Jan 05 '23 16:01

user2864740