Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #31226

Re: JSlint finds for loops intolerable

From Scott Sauyet <scott@sauyet.com>
Newsgroups comp.lang.javascript
Subject Re: JSlint finds for loops intolerable
Date 2016-09-03 23:03 +0000
Organization A noiseless patient Spider
Message-ID <nqfkra$s0m$1@dont-email.me> (permalink)
References <npug18$166s$1@gioia.aioe.org> <npuldh$q9l$1@solani.org> <3919372.LvFx2qVVIh@PointedEars.de>

Show all headers | View raw


Thomas 'PointedEars' Lahn wrote:
> Christoph M. Becker wrote:

>> Once you get the hang of using Array.prototype.forEach(), you'll
>> probably find many places where a more specific array method is more
>> appropriate (for instance, .filter() and map()), and these can
>> simplify the code (even more).
> 
> Yes, but: [ ... ] 
> it will be difficult to impossible to convert a “for” or
> “while” loop that uses the ”break”, “continue” or “return” statements
> to *equivalent* callback code: “break” and “continue” are not allowed
> outside of loops.  “return” in a callback is equivalent to a “continue”
> without label, but there is no elegant equivalent for ”break” (keeping
> to call the callback and querying a non-local flag so that the callback
> returns early is _not_ elegant IMO), and “return” returns *from the
> callback*, _not_ from the function that called the Array method, so
> there is no elegant equivalent for that either. [ ... ]

As mentioned elsewhere on this thread, Crockford and JSLint are quite
opinionated.  I would expect that his response to this would be similar
to my, admittedly opinionated, one: Code written with `break` or
`continue` is most likely poorly written and should be rewritten to avoid
them; it's simply too difficult to reason about for reasons related to
those that caused the programming community to five up `GOTO` statements.

So the point isn't that all existing `for-` loops should be converted to
`filter`, `map`, `some`, `includes`, `reduce`, _et. al._  Instead, one
should write code so that those functions are natural.  *Then* one can
convert it, if there are still actual `for-` loops remaining.

  -- Scott

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

JSlint finds for loops intolerable emf <emfril@gmail.com> - 2016-08-28 06:56 -0400
  Re: JSlint finds for loops intolerable "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-08-28 14:28 +0200
    Re: JSlint finds for loops intolerable emf <emfril@gmail.com> - 2016-08-29 22:35 -0400
    Re: JSlint finds for loops intolerable Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-08-30 12:11 +0200
      Re: JSlint finds for loops intolerable "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-09-03 20:54 +0200
      Re: JSlint finds for loops intolerable Scott Sauyet <scott@sauyet.com> - 2016-09-03 23:03 +0000
    Re: JSlint finds for loops intolerable Scott Sauyet <scott@sauyet.com> - 2016-09-03 22:47 +0000
  Re: JSlint finds for loops intolerable Lewis Perin <perin@panix.com> - 2016-08-28 16:50 -0400
    Re: JSlint finds for loops intolerable Lewis Perin <perin@panix.com> - 2016-08-28 18:49 -0400
      Re: JSlint finds for loops intolerable Lewis Perin <perin@panix.com> - 2016-08-28 19:13 -0400
  Re: JSlint finds for loops intolerable $Bill <news@todbe.com> - 2016-08-28 14:59 -0700

csiph-web