0

I've written the following code:

d3.csv('data/co2_uitstoot.csv', function(error, data) {
    if (error) throw error;
        // Rest of code

When I try to validate this code using JavascriptLint, I get the following warning:

lint warning: block statement without curly braces

Is this truly illegal according to the official Javascript guidelines? I'm asking this because I see this a lot in Javascript code, also in professional plugins and libraries.

TheLeonKing
  • 3,501
  • 7
  • 32
  • 44
  • 3
    It's not illegal. If it was it would be an error. It is just a warning. Warnings means that the authors of JavascriptLint thinks it is a bad idea. – slebetman Jan 22 '16 at 12:15
  • Lint doesn't catch *illegal* constructs (those usually result in `SyntaxError`s), it catches *dangerous and error-prone* constructs. – DCoder Jan 22 '16 at 12:15
  • 1
    It is not illegal, just very bad style and potentially dangerous, as Apple demonstated. – Mithrandir Jan 22 '16 at 12:16
  • It is good practice (not only in Javascript) to code blocks with the block delimiters for stability and security reasons. Just adding a line of code would not change the code structure when blocks are explicitely delimited. Otherwise it is unclear what the changed code does. – hherger Jan 22 '16 at 12:19

2 Answers2

3

It is a warning, not an error. So no, it is not illegal per se. The purpose of any lint program is to warn about violations of best practices. In many cases, violations of best practices can lead to subtle, hard-to-find errors. For example, this code may not do what you might expect:

if (conditionA)
    if (conditionB)
        Console.Write("Condition A & B");
else
    Console.Write("Not condition A");
Jack A.
  • 4,245
  • 1
  • 20
  • 34
  • 1
    this is basically how [ssl goto fail](http://www.dwheeler.com/essays/apple-goto-fail.html) came to be. – dreamlab Jan 22 '16 at 12:25
0

It's not illegal. The linter doesn't claim that it is. It is basically warning you that it's a bad idea.

You're not the first who asks this, but I can imagine the warning put you on a wrong search path. A similar question: Do 'if' statements in JavaScript require curly braces? with meaningful answers.

Community
  • 1
  • 1
Dynom
  • 1,000
  • 12
  • 19