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


Groups > comp.lang.javascript > #17006 > unrolled thread

Good use of bitwise NOT "~" or unnecessary obfuscation?

Started byRobG <rgqld@iinet.net.au>
First post2012-11-01 18:32 -0700
Last post2012-11-07 11:22 -0800
Articles 6 on this page of 46 — 14 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

Page 3 of 3 — ← Prev page 1 2 [3]


#17011

From"Jukka K. Korpela" <jkorpela@cs.tut.fi>
Date2012-11-02 12:28 +0200
Message-ID<k7078i$9s4$1@dont-email.me>
In reply to#17006
2012-11-02 3:32, RobG wrote:

> I frequently see:
>
>      if (someString.indexOf('foo') != -1)
>
> but it looks ugly and isn't all that intuitive.

You can replace != -1 by >= 0, if you think that it's not intuitive that 
indexOf() returns specifically -1 in the case of no match.

If you want something even more intuitive, define your own method, say 
contains(), hiding the dirty details inside its definition.

There is nothing particularly intuitive in the idea that in order to 
test whether string A contains string B as substring, you call a method 
of A that returns the index of the first occurrence of B inside A, or -1 
if there is no occurrence. In most situations, it is irrelevant what the 
index is and whether it is the first occurrence. But the built-in tools 
of JavaScript don't give us a simple boolean function for purposes like 
this, so just use indexOf() as most JavaScript programmers are used to 
using, or set up your own function.

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

[toc] | [prev] | [next] | [standalone]


#17019

FromGene Wirchenko <genew@ocis.net>
Date2012-11-02 10:00 -0700
Message-ID<jku7981nh0mfeoj2o6jitka88dv4l7kopu@4ax.com>
In reply to#17011
On Fri, 02 Nov 2012 12:28:35 +0200, "Jukka K. Korpela"
<jkorpela@cs.tut.fi> wrote:

[snip]

>If you want something even more intuitive, define your own method, say 
>contains(), hiding the dirty details inside its definition.
>
>There is nothing particularly intuitive in the idea that in order to 
>test whether string A contains string B as substring, you call a method 
>of A that returns the index of the first occurrence of B inside A, or -1 
>if there is no occurrence. In most situations, it is irrelevant what the 

     Intuition is overrated here.  Simply learn the basic pattern of
the language.

     The methodology of using a return value for failure outside the
success range of return values when such values are available is
commonly used.  And quite often (usually?), the first such value is
used.  Since 0.. can be success values, -1 gets it.

     GW-BASIC has an instr() function for string searching. GW-BASIC's
strings start with an index of 1.  Not surprisingly, instr() returns 0
if the search string is not found.

>index is and whether it is the first occurrence. But the built-in tools 
>of JavaScript don't give us a simple boolean function for purposes like 
>this, so just use indexOf() as most JavaScript programmers are used to 
>using, or set up your own function.

<curmudgeon>
     Damn kids and all their Hallowe'en candy^W^Wfunction wrappers
strewn about.
</curmudgeon>

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#17013

FromAsen Bozhilov <asen.bozhilov@gmail.com>
Date2012-11-02 06:15 -0700
Message-ID<59d6b91b-f181-4e2b-936f-2720be4ad996@ez26g2000vbb.googlegroups.com>
In reply to#17006
RobG  wrote:
> I frequently see:
>
>     if (someString.indexOf('foo') != -1)
>
> but it looks ugly and isn't all that intuitive. Adding one to the result means if the string isn't found, the result is zero or +ve if it is found, so:
>
>     if (someString.indexOf('foo') + 1)
>
> but that doesn't look much better. But the bitwise NOT comes to the rescue:
>
>     if (~someString.indexOf('foo'))
>
> now if the string isn't found, the result is 0 and some positive integer if it is. And I didn't have to create a regular expression to get similar semantics.
>
> Can anyone see issues with the above? Other than perhaps some not seeing the bitwise NOT operator "~" so misunderstanding the test?

Usually I use `str.indexOf('foo') > -1`. In my opinion this the most
direct way and easy to understand for the readers how built-in indexOf
works. If my code is pretty obvious that any negative value returned
by `indexOf' means the string does not contain the passed substring.
Your checks seem indirectly for me. If the user is not well familiar
how an `indexOf' works, he would need to check the documentation to
understand your code.

ECMAScript 6 will include `String.prototype.contains' method, which
works exactly like your intentions. So you could detect if the
`contains' is not presented and implemented fallback version with
`str.indexOf(substr) > -1` or like your other solutions.

Trying to be too clever is not a good idea I think.

[toc] | [prev] | [next] | [standalone]


#17048

FromScott Sauyet <scott.sauyet@gmail.com>
Date2012-11-06 06:32 -0800
Message-ID<bedd9789-3b5d-456b-9931-9691eb3da47e@h15g2000yqe.googlegroups.com>
In reply to#17006
RobG wrote:
>     if (~someString.indexOf('foo'))
>
> Can anyone see issues with the above? Other than perhaps some not
> seeing the bitwise NOT operator "~" so misunderstanding the test?

No, that's the only problem, but it's a serious one.  This is
unnecessary obfuscation.  I'm with Asen here.  This is definitely
clearer:

    if (someString.indexOf('foo') > -1)

  -- Scott

[toc] | [prev] | [next] | [standalone]


#17061

FromRobG <rgqld@iinet.net.au>
Date2012-11-06 18:23 -0800
Message-ID<37442c0f-cd9a-4a7a-b4db-b2628765e796@googlegroups.com>
In reply to#17048
On Wednesday, November 7, 2012 12:32:24 AM UTC+10, Scott Sauyet wrote:
> RobG wrote:
> 
> >     if (~someString.indexOf('foo'))
> >
> > Can anyone see issues with the above? Other than perhaps some not
> > seeing the bitwise NOT operator "~" so misunderstanding the test?
> 
> No, that's the only problem, but it's a serious one.  This is
> unnecessary obfuscation.  I'm with Asen here.  This is definitely
> clearer:
> 
>     if (someString.indexOf('foo') > -1)

Yes, I think I'm in that camp too.

I often use regular expressions and *test* because it's more precise, but it also seems heavy handed to use a regular expression when *indexOf* will do.

The use of unary "+" to convert strings to numbers should probably be seen in the same light.


-- 
Rob

[toc] | [prev] | [next] | [standalone]


#17064

FromScott Sauyet <scott.sauyet@gmail.com>
Date2012-11-07 11:22 -0800
Message-ID<b308f825-ceb8-4368-84da-5179eb9a261e@j18g2000yqf.googlegroups.com>
In reply to#17061
RobG  wrote:
> Scott Sauyet wrote:
>> RobG wrote:
>>>  [... RE: the following ...]
>>>     if (~someString.indexOf('foo'))
>
>> This is definitely clearer:
>
>>     if (someString.indexOf('foo') > -1)
>
> Yes, I think I'm in that camp too.
>
> I often use regular expressions and *test* because it's more precise,
> but it also seems heavy handed to use a regular expression when
> *indexOf* will do.

If you're testing for a fixed simple string, such as "foo", then that
is to my eyes the cleanest approach of all:

    if (/foo/.test(someString))

But if the string is dynamic, even if you are certain that it will
contain no characters that need to be escaped, using the RegExp
constructor in the same manner is generally less clear.


> The use of unary "+" to convert strings to numbers should probably be
> seen in the same light.

Agreed, although I sometimes find myself guilty of using it in the
middle of a complicated arithmetic expression.  The `Number`
constructor will always be more explicit, and `parseFloat` or
`parseInt` have many legitimate uses.

  -- Scott

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

Back to top | Article view | comp.lang.javascript


csiph-web