What is the price offer for ESLint's “consistent return” rule?

What value always returns a value ("undefined") for functions, do you need to explicitly return something?

Why is this rule and what mistakes catch it?


You can read about the ESLint “consistent return” rule here (says “what”, not “why”).

You can read a speculative analysis of why javascript functions implicitly return undefined here when the stack overflows.

+6
source share
1 answer

Some languages ​​distinguish between functions and procedures. This does not apply to C-alikes, but it is still a good idea to develop routines in this way.

Linter does not want you to "always return something." It just tells you that if you create a function (as opposed to a procedure), it should return something meaningful anyway (ideally, all the return values ​​should be of the same type).

Example:

function is_visible(object) 

is a function, it must return a value (in this case, Boolean) and can be used in expressions. On the other hand

 function make_visible(object) 

- this is a procedure, it should not return anything and cannot be used in expressions - it is always a statement.

Such a design (and the warning about the linger associated with it) helps a lot to prevent such errors (taken from some random web page):

enter image description here

+4
source

All Articles