Letting RegExp method return something iterable?

Brendan Eich brendan at mozilla.com
Tue Aug 27 09:48:32 PDT 2013


On Aug 27, 2013, at 9:42 AM, Andrea Giammarchi <andrea.giammarchi at gmail.com> wrote:

> sure you know everything as soon as you read `of` ... right ?

Wrong. The nested assignment is idiomatic in C but not good for everyone (see gcc's warning when not parenthesized in such contexts) due to == and = being so close as to make typo and n00b hazards.

Furthermore, the exogenous binding / hoisting problem is objectively greater cognitive load and bug habitat.


> How objectives are your points ? If you know JS that while looks very simple, IMO

Please learn when to fold a losing argument :-|.

/be

> 
> 
> On Tue, Aug 27, 2013 at 5:24 AM, Claude Pache <claude.pache at gmail.com> wrote:
>> 
>> Le 27 août 2013 à 01:23, Brendan Eich <brendan at mozilla.com> a écrit :
>> 
>> > Andrea Giammarchi wrote:
>> >> Is it very useful because you wrote for instead of while ?
>> >>
>> >> ```javascript
>> >> while (m = re.exec(str))
>> >>  console.log(m[0])
>> >> ;
>> >> ```
>> >
>> > It is, for two reasons:
>> >
>> > 1. in JS only for can have a let or var binding in the head.
>> >
>> > 2. the utility extends to all for-of variations: array comprehensions, generator expresisons.
>> >
>> > /be
>> 
>> There is a third reason. The syntax:
>> 
>> ```javascript
>> for (let m of re.execAll(str) {
>>         // ...
>> }
>> ```
>> 
>> has the clear advantage to express the intention of the programmer, and *nothing more*. It does not require good knowledge of the details of the language to understand what happens.
>> 
>> Indeed, when I read `while(m = re.exec(str))`, I really have to analyse the following *additional* points:
>> * `=` is not a typo for `==` (here, some annotation would be useful);
>> * `RegExp#exec` returns a falsy value if *and only if* there is no more match;
>> * `re` has its global flag set, and its `.lastIndex` property has not been disturbed.
>> 
>> All these tricks are unrelated to the intention of the programmer, and are just distracting points, especially for any reader that use only occasionally `RegExp#exec` with the global flag set.
>> 
>> In summary, citing [1]: "Don’t be clever, don’t make me think."
>> 
>> —Claude
>> 
>> [1] http://www.2ality.com/2013/07/meta-style-guide.html
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130827/da0a822b/attachment.html>


More information about the es-discuss mailing list