开发者

Is block style really this important? [duplicate]

开发者 https://www.devze.com 2023-01-02 15:49 出处:网络
This question already has answers here: Why do results vary based on curly brace placement? (6 answers)
This question already has answers here: Why do results vary based on curly brace placement? (6 answers) Closed 6 years ago.

I just watched a video of Douglas Crockford's presentation about his 2009 book JavaScript: The Good Parts.

In the video, he explains that the following block is dangerous because it produces silent errors:

return
{
    ok: false
};

And that it should actually be written like this (emphasising that although seemingly identical the behavioural difference is crucial):

return {
    ok: false
};

You can see his comments around 32 minutes into the video here: http://w开发者_运维知识库ww.youtube.com/watch?v=hQVTIJBZook&feature=player_embedded#!&start=1920

I have not heard this before, and was wondering if this rule still applies or if this requirement in syntax has been overcome by JavaScript developments since this statement was made.

I found this very interesting as I have NOT been writing my code this way, and wanted to check that this information was not out of date.


The silent error is that undefined is returned!

Semicolons are optional in JavaScript, and therefore

return
{
    ok: false
};

is parsed as if it were

return;  // Leaves function straight away
{
    ok: false   
};

JSLint will recognize such patterns and warn about them:

lint warning: unexpected end of line; it is ambiguous whether these lines are part of the same statement

lint warning: missing semicolon

lint warning: unreachable code

lint warning: meaningless block; curly braces have no impact

This has been discussed on SO in the "Strangest language feature" question.


The rule still applies.

Because the language automatically inserts "missing" semi-colons, the first snippet is interpreted as:

return;

{
    ok: false
};

I.e, undefined is returned. If the code was somehow permitted to run past the return statement, an object would then be created, but would not be assigned to anything useful (a variable).


Javascript will insert a semicolon after the return, because it "seems to be missing".

What follows is a block of {ok:false} that has no effect.

So it's a bug in the javascript specification..

My recommendation is run jslint whenever you can, and configure it to allow for your style when it's different from Crockford's.


This is a very real problem. Unintentionally returning a null can definitely do bad things in your code!


The rule applies today as well, and is one of the 'bad parts'. The first code snippet will make the function return undefined.

See my other answer about this topic: Semicolon in C++?


Wondering why people decide to make stuff optional will it cost you almost nothing to add a semicolon. But debugging this situation can be very time consuming.

More generally, why language designer kept thinking that the code should self healing from user mistake. If people make mistake the ought to be warn. Otherwise you will get in trouble later and later.

And we know that the later you figure out mistake the more it will be expensive.


Javascript is "friendly" enough to assume semicolons at line breaks in some cases. I actually wrote a blog entry about that a while ago:

Javascript – almost not line based

The part about the return statement goes:

However, there are a few things that you can’t do. If you for example put a line break between the return keyword and it’s argument, it suddenly has a different meaning. If you format the code like this:

function getAnswer() {
  var answer = 42;
  return
    answer;
} 

Then it is interpreted like this:

function getAnswer() {
  var answer = 42;
  return;
  answer;
} 

The return statement takes it’s parameterless form, and the argument becomes a statement of it’s own.

0

精彩评论

暂无评论...
验证码 换一张
取 消