1

I'm running JSLint on a project and I'm hitting this error:

Expected exactly one space between } and else

On this block of code:

// Check for the existance of the file created by firstrun.js
if (runOnce.exists) {
    window.location = 'app:/core/firstrun.html';
}

// Check for version info
else if (!versionInfo.exists) {
    window.location = 'app:/core/createVersion.html';
}

The // Check for version info line is obviously causing the problem; but where would Crockford have me put this comment?

I could obviously change the else if to an if since the first if contains a redirect; but I have other commented if/else if/else's not containing redirects.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
  • possible duplicate of [JSLint, else and Expected exactly one space between '}' and 'else' error](http://stackoverflow.com/questions/8130134/jslint-else-and-expected-exactly-one-space-between-and-else-error) – Eugene Evdokimov Sep 25 '13 at 19:14
  • 1
    Honestly the comments you provided in that example don't even say anything more than the code already does, so why even bother? They could even be inside those conditions. – guessimtoolate Sep 25 '13 at 19:15
  • @guessimtoolate I've edited the code for brevity. The actual block of code is more complex. – Danny Beckett Sep 25 '13 at 19:18
  • @EugeneEvdokimov That's **not** a duplicate. I know *why* the error is being shown. What I want to know is *where* to place the comment. That question's answer just confirms what I already know. – Danny Beckett Sep 25 '13 at 19:20
  • 1
    I suppose Crockford would have you put both comments at the top: "If this happens we're going to this, but otherwise we'll too that". This allows later readers who are skimming to get the gist of the whole control block without having to scroll. The "paragraph" is not split up. – AlexMA Sep 25 '13 at 19:34
  • "Check for version info" is synonymous with "if (!versionInfo.exists)". The comment is redundant and is not necessary. Comments should *not* explain what you are doing -- the code should be enough to determine that. Comments should explain *why you are doing it.* (If the code doesn't obviously show what it's doing then it's bad code.) – cdhowie Sep 25 '13 at 20:34
  • @cdhowie You clearly didn't read my first comment. guessimtoolate already said that, and I answered. – Danny Beckett Sep 25 '13 at 20:35
  • @DannyBeckett And you didn't read mine. If the code needs a comment to explain what it is testing then the code is not readable. If the condition is that complex then a comment is probably not the answer; a refactor is. For example, encapsulate the condition in a function with a descriptive name. – cdhowie Sep 25 '13 at 20:42
  • @cdhowie The block of code relates to licensing. The variable name the conditional checks is *purposely* not named descriptively. Comments are stripped as part of the build process. – Danny Beckett Sep 25 '13 at 20:45
  • @DannyBeckett Hmm, would it be possible to use a descriptive name but run the code through an obfuscator instead? It sounds like you're effectively manually obfuscating. – cdhowie Sep 25 '13 at 20:56
  • @cdhowie Essentially, you've got it in one. Implementing an obfuscator is on our to-do list, but as with most projects, other things are taking priority atm. – Danny Beckett Sep 25 '13 at 21:03

3 Answers3

2

I know this sounds odd, but you may try and put it inline with the else Like this:

else if (!versionInfo.exists) { // Check for version info    
        window.location = 'app:/core/createVersion.html';    
}

JSHint from JsFiddle said that this piece of js is syntactically valid :/

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Sebastian
  • 99
  • 2
  • It'd have to be `} else if (!versionInfo.exists) { // Check for version info`, with the preceding `}`. Not a terrible idea, but it doesn't look great. I've given you an upvote though! Also, JSHint !== JSLint; Hint is a community fork for people who don't like the strictness of Lint. – Danny Beckett Sep 25 '13 at 19:17
  • 1
    No it doesn't, the } is on a separate line up above. – Collin Grady Sep 25 '13 at 19:40
  • @CollinGrady That isn't valid in JSLint. It generates the exact same error (expected exactly one space between `}` and `else`) – Danny Beckett Sep 25 '13 at 20:36
2

It looks a little weird, but I use this style.

if (runOnce.exists) {
    // Check for the existance of the file created by firstrun.js
    window.location = 'app:/core/firstrun.html';
} else if (!versionInfo.exists) {
    // Check for version info
    window.location = 'app:/core/createVersion.html';
}

Honestly, though, just forget about JSLint, for this case.
Those are just suggestions, not rules. I think readability is more important here.

Joe Simmons
  • 1,828
  • 2
  • 12
  • 9
1

Set the JSLint option to allow messy whitespace, otherwise it tries to enforce its own whitespace style rules, which are silly (IMO):

/*jslint white: true */
Collin Grady
  • 2,226
  • 1
  • 14
  • 14
  • The idea of using Lint is to enforce a strict coding style, which is why I'm/we're using it. This defeats the point. – Danny Beckett Sep 25 '13 at 20:37
  • Lol. Then you have to use @Joe's style. That is, I think you've answered your own question -- if you want Crockford's whitespacing style (one of the least useful parts of JSLint imo), you have to change yours. If you want yours, you set `white:true`. Crockford does give you these settings for a reason. Either Joe or Collin has the "right" answer. – ruffin Oct 05 '13 at 14:47