SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: jslint

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2000
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jslint

    I have a language structure question - or maybe it's just a question on how jslint works:

    http://www.jslint.com

    This construct:

    Code:
     
    var foo = 'hello';
    var bar = 'world';
    if ( foo == 'hello' )
     bar = 'world2';
    gives

    Problem at line 4 character 21: Line breaking error: ')'.
    if ( foo == 'hello' )
    Problem at line 5 character 2: Missing '{' before 'bar'.
    bar = 'world2';


    Now if removing the line break (either by checking the box lax line breaking ) or by code:

    Code:
     
    var foo = 'hello';
    var bar = 'world';
    if ( foo == 'hello' ) bar = 'world2';
    I get:

    Problem at line 4 character 23: Missing '{' before 'bar'.
    if ( foo == 'hello' ) bar = 'world2';

    This:
    Code:
     
    var foo = 'hello';
    var bar = 'world';
    if ( foo == 'hello' )
    {
     bar = 'world2';
    }
    is however ok...i.e you must use {} even if only one line is the result so to speak.

    I never use {} in C/C++,C# when just one line statement is the result. However I have never cared for linting neither my javascript nor my C/C++/C# code (just happy if it works...) - but just wanted to check with you all guys - isn't this a little too hard from jslint to not "allow" these type of statements?

    Anyone now about the motivation for such a verifying rule in jslint?

    Stefan
    http://www.aspcode.net - articles on ASP/ASP.NET
    http://www.findfreefonts.net - jquery/ajax experiment site

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's no doubt that "always use braces" is a proven best practice. But it would be nice if non-standard rules were optional.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2000
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks mike. I am not all that confident in javascript - so I have no idea about best practices. Just found out about jslint yesterday - and now I also found the http://javascript.crockford.com/code.html document (links on his site are not underlined so I didn't see it...) specifying that
    "Braces are used around all statements, even single statements, when they are part of a control structure, such as an if or for statement. This makes it easier to add statements without accidentally introducing bugs"

    Now it does make sense (both why jslint warns and the motivation behind it)
    Stefan
    http://www.aspcode.net - articles on ASP/ASP.NET
    http://www.findfreefonts.net - jquery/ajax experiment site

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JavaScript has a 'feature' called semicolon insertion. Unlike other languages with a similar-looking syntax (e.g., C, C++, Java), you can omit semicolons as statement terminators/separators in many instances with JavaScript.

    This can cause very surprising results for the unwary, which seems to be why jslint makes an issue of warning you about it, according to this page.
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reasoning for this is that not using braces everywhere can confuse packers and minifiers, or even some javascript interpreters.

  6. #6
    Google Engineer polvero's Avatar
    Join Date
    Oct 2003
    Location
    Mountain View
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I never use {}
    Yeah, as mentioned. You 'should'. These minifiers and obfuscators are really going to mess with your code if you're not. Really, you should read both style guides part I and part II


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •