"function doesn't always return a value" strict warning issue

RESOLVED WONTFIX

Status

()

Core
JavaScript Engine
--
minor
RESOLVED WONTFIX
11 years ago
2 years ago

People

(Reporter: sdwilsh, Unassigned)

Tracking

(Blocks: 1 bug)

Trunk
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: [wontfix?], URL)

(Reporter)

Description

11 years ago
Spinoff from Firebug Issue.  We produce strict warnings for functions that don't return a value always.  Joe sums it up well:

"I hate to say this, but I have always loved the fact that functions without a return statement return "undefined", and I take advantage of this fact for syntactic cleanliness."

and later:
"I realize that JavaScript strict mode spits out warnings about this, but to me, that is an indictment of strict mode, and one of the reasons why I don't ever run with it on.  JavaScript is not Java, and so we shouldn't be forced to be so rigid."

Comment 1

11 years ago
Can you give an example of code that appears "cleaner" when some paths return a value and others don't return/throw at all?  That just sounds confusing and dangerous to me.

Comment 2

11 years ago
Or is the issue that the js engine complains about "return;" and wants you to write out "return undefined;"?  Using just "return" still seems more confusing than clean to me, but I still want to see an example.
(Reporter)

Comment 3

11 years ago
Something like this:

function findPropByName(props, name)
{
    for (var i = 0; i < props.length; ++i)
    {
        if (props[i].name == name)
            return i;
    }
}
Summary: JavaScript strict warnings issue → "function doesn't always return a value" strict warning issue

Comment 4

11 years ago
what we need is a option select/deselect warning message to be shown on JS Error console.

Comment 5

11 years ago
(In reply to comment #3)
> Something like this:
> 
> function findPropByName(props, name)
> {
>     for (var i = 0; i < props.length; ++i)
>     {
>         if (props[i].name == name)
>             return i;
>     }
> }

So how's that cleaner than return null;?

It should also be mentioned that the warning doesn't just bitch about coding style but potentially helps to find buggy code. This bug should be wontfix'd.

Comment 6

11 years ago
(In reply to comment #5)
> (In reply to comment #3)
> > Something like this:
> > 
> > function findPropByName(props, name)
> > {
> >     for (var i = 0; i < props.length; ++i)
> >     {
> >         if (props[i].name == name)
> >             return i;
> >     }
> > }
> 
> So how's that cleaner than return null;?

-1 even, since you're returning an index otherwise. Excellent example for how "sometimes" returning a value can cause confusion.

Comment 7

11 years ago
(In reply to comment #0)
> "functions without a return statement return "undefined""

Isn't it rather that the function doesn't return anything at all, making the returned value undefined? That's opposite to Python which doesn't know "undefined" and thus returns None (null) for value-less returns - which is where Joe is coming from.

> "JavaScript is not Java, and so we shouldn't be forced to be so rigid."

Strict warnings have ever been optional and benign, as JSLint they allow to enforce several coding standards which help to avoid several common sets of errors (you wouldn't usually expect "undefined" to be returned as a result, rather null, 0 or false).

So either the issue is about being able to enable/disable strict warnings by type (valid, but not this bug as filed), it's about filtering them out in the Error Console (not this bug, either) or it's about dropping this specific warning (WONTFIX, as the only argument seems to be that they clash with Joe's Python-influenced coding habits).
Whiteboard: [wontfix?]

Comment 8

11 years ago
I would consider this a WONTFIX.  If you really want to return undefined and have it pass strict, then use 'return undefined;'

Especially since JavaScript doesn't have a prototype to explain what the function should/shouldn't return.  Either it returns the default value (undefined) everywhere, or it returns values explicitly.  This is a strict mode after all.

Ciao!

Updated

10 years ago
Blocks: 296661
(Assignee)

Updated

4 years ago
Assignee: general → nobody
Strict warning "does not always return a value" was removed in http://hg.mozilla.org/mozilla-central/rev/a470d0cbe3fa (bug 1046964)
Status: NEW → RESOLVED
Last Resolved: 2 years ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.