50

If I have something like this:

var blah = function() { };

and then later in code blah is being used, what is the JSLint hint that says remove the empty block?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375

7 Answers7

38

I don't know what jsLint thinks but if this is a problem and you need a solution then you can do something like the following:

var blah = function() { return undefined; }; // or just return;

Update : I think, Bergi's guess is right because, on the jslint site in the Required Blocks section :

JSLint expects that if, while, do and for statements will be made with blocks {that is, with statements enclosed in braces}.JavaScript allows an if to be written like this:if (condition) statement;That form is known to contribute to mistakes in projects where many programmers are working on the same code. That is why JSLint expects the use of a block:

if (condition) { statements; }

Experience shows that this form is more resilient.

So, It probably just checks for empty blocks { } and invalidate the blank function.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
30

Use the lambda expression:

const blah = () => void 0;

This will make it clear that blah is an empty function that returns undefined.

splintor
  • 9,924
  • 6
  • 74
  • 89
5

If you are asking what JsLint option turns this warning off it is: "debug:true"

Strangely, the docs make no reference to this behavior:

"Tolerate debugger statements" | debug | true if debugger statements should be allowed. Set this option to false before going into production.

But if you look at the code, you can see that it won't warn you with the debug option set to true:

function block(kind) {
    // A block is a sequence of statements wrapped in braces.

    ...

    if (kind !== 'catch' && array.length === 0 && !option.debug) {
        curly.warn('empty_block');
    }
    ...
}
Mat
  • 96
  • 1
  • 4
4

A lot of code checkers check for this sort of thing. It doesn't mean you should never have empty code blocks. Sometimes there are valid reasons for having them. But it often means that the programmer just forgot to write the implementation. :)

What I like to do is put a comment in the function body, explaining why it's empty. This should suppress the warning, but it may not depending on whether the code checker considers a code block with a comment "empty".

var blah = function() { /* empty because ... */ };
Michael
  • 34,873
  • 17
  • 75
  • 109
  • 11
    A good convention, though jshint will still complain, because Crocker. – dfreeman Jul 16 '13 at 20:02
  • 3
    This is so f** annoying. Crocker's own code for Object.create uses an empty block and will not pass JSLint. He needs to figure out who the audience is. Is it supposed to be used as a pre-production check, or as a sanity check during the development cycle? Because right now, he is even more confused than normal. – Michael Mikowski Feb 11 '14 at 22:41
3

If you intend to use the function as a constructor with the new operator:

// Returns the instance that was just created with the new operator.
var ClassLikeFunction = function(){
    return this; 
};

On the other hand, if is intentionally a blank function with no return value:

// Returns the same value as a function that returned nothing.
var blankFunction = function(){
    return undefined; 
};
Aaren Cordova
  • 2,068
  • 1
  • 14
  • 7
2

This

{
    ...
}

is considered a code block and the hint is letting you know that it is empty (there are no commands in it). You don't have to remove it though, as @Katana314 said, it could be intentional.

jacobianism
  • 226
  • 1
  • 3
  • 12
  • 1
    This won't return a `function`, it will only return a `new Object()` (it's a shortcut) and these two a completly different. The only way to return a empty function is to use `var myEmptyMethod = function(){}`. For more information see [Douglas Crockford explanation](http://javascript.crockford.com/code.html#bonus) – RPDeshaies Dec 17 '13 at 16:10
  • @RPDeshaies It depends. If evaluated as a statement (i.e. the first `{` is encountered when in statement evaluation mode), then `{ }` will be evaluated as an empty code block. Otherwise, if evaluated as an expression, it will evaluate to a new empty object. – Roy Tinker Jan 19 '19 at 01:50
0

what let me to this question is that I had an empty function in my namespace

and when I called that function, and

TypeError: MyNamespcae.myFunction is not a function

so don't create an empty function, at lease add one statement like void(0); or return null;

Basheer AL-MOMANI
  • 14,473
  • 9
  • 96
  • 92