Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #17026
| Message-ID | <6563475.n3XdYolgyi@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2012-11-03 10:19 +0100 |
| Subject | Re: Good use of bitwise NOT "~" or unnecessary obfuscation? |
| Newsgroups | comp.lang.javascript |
| References | (2 earlier) <BMSdnRSFG8QIVA7NnZ2dnUVZ_vGdnZ2d@earthlink.com> <1805042.BzeuYpbCYS@PointedEars.de> <7MidnYXvqeq8ggnNnZ2dnUVZ_uGdnZ2d@earthlink.com> <1466051.Cd1scNln3u@PointedEars.de> <IcidnRJ5-sYx2gnNnZ2dnUVZ_hKdnZ2d@earthlink.com> |
| Followup-To | comp.lang.javascript |
Followups directed to: comp.lang.javascript
[I had written a longer reply with examples, but apparently it has not
reached Usenet. So I will keep this one shorter and expand on it upon
request.]
Patricia Shanahan wrote:
> On 11/2/2012 2:10 PM, Thomas 'PointedEars' Lahn wrote:
>> Patricia Shanahan wrote:
>>> On 11/2/2012 10:41 AM, Thomas 'PointedEars' Lahn wrote:
>>>> Patricia Shanahan wrote:
>>>>> On 11/2/2012 2:22 AM, Evertjan. wrote:
>>>>> ...
>>>>>> if ( /foo/.test(someString) ) {..}
>>>>>>
>>>>>> I trust not understanding Regex is not a valid counterargument.
>>>>> If I'm prepared to look at it long enough, I can generally work out
>>>>> what a Regex does, but Regex does not seem to me to be a very
>>>>> human-friendly, smoothly readable language.
>>>> That depends on the flavor and your experience with them.
>>> Well, I don't quite have 30 years of experience with regular expressions
>>> yet, but it's getting close.
>> Which flavors?
>
> Assorted UNIX shell tools (lex, grep, egrep, vi, ed, sed, awk etc.),
> Perl, PHP, and Java.
As I see it, implementations of regular expressions are lacking readability
because of two factors: insufficient expressiveness and stringly typing.
Insufficient expressiveness or excessive verbosity means you have to write
longer expressions for rather simply concepts. Consider, for example, POSIX
`[[:space:]]' vs. Perl/PCRE `\s'. Consider POSIX Basic Regular Expressions
(BRE) `\{0,1\}' vs. POSIX Extended Regular Expressions (ERE)'s and
Perl's/Perl-Compatible Regular Expressions (PCRE)'s `?'. Consider POSIX BRE
`\{1,\}' vs. ERE's/Perl's/PCRE's `+'.
Stringly typing, i. e. expressing data in string literals instead of in
regular expression literals, causes regular expressions to be less readable,
and in turn code that uses regular expressions to be less readable, due to
the fact that string literals have their own escaping mechanism.
I have not done much lex. But as you probably know, POSIX grep, vi(m), ed,
sed, awk & friends only support POSIX Basic Regular Expressions. BRE both
lack expressiveness and are stringly typed in direct use. So they cannot be
shining examples of readable regular expressions.
ERE as supported by POSIX egrep and GNU grep improve on that slightly by
reversing expression logic (so you have to escape what you do *not* want to
be special instead), by adding shortcuts for `{0,1}' and `{1,}' and
alternation. But they still have the excessive verbosity and stringly
typing problem of POSIX REs.
Perl RE and PCRE (the latter is supported by GNU grep) improve on
readability again by providing regular expression literals with and, among
other powerful features such as interpolation, flags to improve readability
specifically (such as `/x'). But you have to be aware of that.
While PHP eventually supports PCRE and allows a wider range of delimiters,
its RE implementation suffers from the fact that there are no regular
expression literals. It steps back to stringly typing. So does Java, but
Java essentially allows only one delimiter and no multi-line syntax; another
two steps back. Java's RE implementation, in addition to that, suffers from
Java's static typing and the excessive verbosity in implementation that
brings. So both of them do not provide good examples either.
You have not mentioned Python. Python's implementation is a step back from
PHP and Java in that it does not support PCRE, but its own PCRE-inspired
flavor. However, it is a step forward from that because of Python's
implicit string concatenation, raw-strings to alleviate the escaping
problem, and new RE features.
By comparison, ECMAScript and its implementations have regular expression
literals, but those have no variable delimiter, no interpolation, no multi-
line syntax, and they do not support PCRE but their own flavor yet again.
But on the plus side the languages are dynamic enough so that you can work
around those shortcomings (as I did in JSX:regexp.js).
(There are also other RE implementations, like that of Microsoft, which
primarily suffer from the fact that they are very different to the common
aforementioned ones, or have limited use. If you ever tried to use RE to
search with Visual Studio, you know what I mean.)
Finally, as a third factor to readability of code using regular expressions,
here comes in the person responsible for writing it. Many people use
regular expressions where they are not strictly necessary. And they write
needlessly complicated regular expressions because they do not know better
or do not care. Specifically for ECMAScript implementations (but you can
also find similar bloat code elsewhere), people escape `/' in *string*
literals passed to the RegExp constructor because `/' is the delimiter of
*RegExp* literals. They needlessly escape special characters in character
classes. They use alternation where character classes would have sufficed.
And so on.
Attempting to avoid regular expressions where they are not necessary, and
simplifying regular expressions where they are, thereby improving
readability, goes a long way towards understanding them, and vice-versa.
You can find examples of that in many of my follow-ups in comp.lang.ALL and
de.comp.lang.ALL.
PointedEars
--
When all you know is jQuery, every problem looks $(olvable).
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Good use of bitwise NOT "~" or unnecessary obfuscation? RobG <rgqld@iinet.net.au> - 2012-11-01 18:32 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Denis McMahon <denismfmcmahon@gmail.com> - 2012-11-02 06:14 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Patricia Shanahan <pats@acm.org> - 2012-11-02 02:28 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-11-02 10:22 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Tim Streater <timstreater@greenbee.net> - 2012-11-02 10:37 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Patricia Shanahan <pats@acm.org> - 2012-11-02 06:25 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-11-02 06:35 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-11-02 14:58 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Patricia Shanahan <pats@acm.org> - 2012-11-02 08:49 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-11-04 21:52 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Tim Streater <timstreater@greenbee.net> - 2012-11-04 22:49 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-11-05 13:18 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Scott Sauyet <scott.sauyet@gmail.com> - 2012-11-06 06:48 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Tim Streater <timstreater@greenbee.net> - 2012-11-06 15:04 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Gene Wirchenko <genew@ocis.net> - 2012-11-06 10:43 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Scott Sauyet <scott.sauyet@gmail.com> - 2012-11-06 11:22 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Patricia Shanahan <pats@acm.org> - 2012-11-06 12:16 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Scott Sauyet <scott.sauyet@gmail.com> - 2012-11-06 12:53 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-11-07 17:34 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-07 23:21 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-11-07 23:23 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Gene Wirchenko <genew@ocis.net> - 2012-11-07 17:00 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-11-08 07:16 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Dr J R Stockton <reply1245@merlyn.demon.co.uk.invalid> - 2012-11-07 17:50 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Dr J R Stockton <reply1245@merlyn.demon.co.uk.invalid> - 2012-11-05 19:26 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Stefan Weiss <krewecherl@gmail.com> - 2012-11-07 22:13 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Gene Wirchenko <genew@ocis.net> - 2012-11-02 09:51 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-02 18:41 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Patricia Shanahan <pats@acm.org> - 2012-11-02 12:30 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Tim Streater <timstreater@greenbee.net> - 2012-11-02 20:33 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-11-04 21:57 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Tim Streater <timstreater@greenbee.net> - 2012-11-04 22:44 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Andrew Poulos <ap_prog@hotmail.com> - 2012-11-05 15:41 +1100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Tim Streater <timstreater@greenbee.net> - 2012-11-05 09:07 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-02 22:10 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Patricia Shanahan <pats@acm.org> - 2012-11-02 15:22 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-03 10:19 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Patricia Shanahan <pats@acm.org> - 2012-11-03 13:05 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-03 21:39 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-03 12:26 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-11-02 12:28 +0200
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Gene Wirchenko <genew@ocis.net> - 2012-11-02 10:00 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-11-02 06:15 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Scott Sauyet <scott.sauyet@gmail.com> - 2012-11-06 06:32 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? RobG <rgqld@iinet.net.au> - 2012-11-06 18:23 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Scott Sauyet <scott.sauyet@gmail.com> - 2012-11-07 11:22 -0800
csiph-web