Loading ...
Sorry, an error occurred while loading the content.

1621[jslint] Re: Filtered for...in

Expand Messages
  • Chris
    Nov 19, 2010

      This seems like a reasonable suggestion. I can see the appeal of the alternative style proposed here. I see nothing unclear or obscure about the "continue" keyword in this context. However, I remain unconvinced that JSLint would be better off permitting this.

      The "continue" and "break" keywords seem unclear if they occur at random points within a very large loop. A "continue" statement that is buried deep in the guts of its parent loop could indeed cause confusion. It could easily be overlooked by somebody looking over the code. Even if they find it, its meaning may not be clear: a reader might need to look back up the code to locate the loop's control statement. The situation can be further confounded by nested loops.

      Interestingly, the obscurity of purpose increases with the number of lines of code between the "continue" and the beginning of its parent loop. At the very top of the loop, the confusion caused by the "continue" statement is minimal.

      If I imagine a for..in loop that starts with a conditional block, and that conditional block contains "continue" as its only statement, then I would find that to be an acceptable level of complexity.

      Due to the nature of the problem, some increase in complexity is necessary. The current solution of wrapping the body of a for..in loop with a large conditional block also increases complexity.

      If I compare the increase in complexity caused by the current solution against the corresponding increase caused by the proposed solution, I don't see a significant difference. As others have pointed out, it seems to be a stylistic decision more than anything else.

      However, "continue" is a treacherous statement. It is maligned because of its similarity with "goto." Indeed, it is easy to imagine ways in which it could be abused. It has redeeming qualities when used correctly and consciously, but it could also lead to sloppy, confusing code if used poorly.

      Given the potential issues with "continue," I am not convinced that JSLint should suggest its use, particularly when the current solution functions perfectly well. I am happy to avoid use of "continue" unless it offers a clear improvement. I don't see such an improvement in this case.

      Furthermore, JSLint is in a unique position. There are many resources to help a beginning programmer learn JavaScript. There are, however, few resources to help a beginning programmer learn JavaScript WELL. Of those few, JSLint is the only tool of its kind that I am aware of. Many people look to JSLint and change their coding style because of it; in effect, improving themselves by drawing from Mr. Crockford's experience.

      Given the importance of JSLint, and the issues surrounding the "continue" statement, I opine that JSLint should not promote "continue" in any context.

      - Chris

      --- In jslint_com@yahoogroups.com, "Paul" <merlin@...> wrote:
      > I agree with the OP.
      > I use the continue statement in exactly the same manner and have trouble understanding the objection to responsible use of the continue statement.
      > I just don't see where the continue statement can confuse logic where other flow control methods do not. I've personally gone nearly cross-eyed trying to deal with another developer's deep layers of nested flow control.
      > I do see how the continue statement can be abused, but I'm sure we've all seen how if statements can also be abused. Which is why I think that outright discouraging its use altogether seems rather extreme.
      > While I respect the difference in opinion, I'd also be glad to see an optional exception added for the unfiltered for..in check so that it could tolerate a single conditional statement block containing only "continue;" at the top of the loop's expression as a filter, but not tolerate for..in loops that are actually unfiltered.
    • Show all 19 messages in this topic