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


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

Programming style question

Started byPatricia Shanahan <pats@acm.org>
First post2012-10-18 08:32 +0100
Last post2012-10-21 17:57 -0700
Articles 20 on this page of 113 — 20 participants

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


Contents

  Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-18 08:32 +0100
    Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-18 10:23 +0200
      Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-18 13:42 +0100
        Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-18 17:59 +0200
          Re: Programming style question Gene Wirchenko <genew@ocis.net> - 2012-10-18 09:56 -0700
            Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-19 00:49 +0200
              Re: Programming style question Gene Wirchenko <genew@ocis.net> - 2012-10-19 11:04 -0700
                Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-20 11:52 +0200
              Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-19 12:06 -0700
                Re: Programming style question Tim Streater <timstreater@greenbee.net> - 2012-10-19 23:12 +0100
                  Re: Programming style question "Mel Smith" <med_cutout_syntel@aol.com> - 2012-10-19 21:56 -0600
                Re: Programming style question Bart Van der Donck <bart@nijlen.com> - 2012-10-20 02:21 -0700
                  Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-20 12:03 +0200
                  Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-20 06:31 -0700
                    Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-20 18:30 +0200
                      Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-20 10:32 -0700
                        Re: Programming style question Tim Streater <timstreater@greenbee.net> - 2012-10-20 22:34 +0100
                          Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-21 10:12 +0200
                            Re: Programming style question Tim Streater <timstreater@greenbee.net> - 2012-10-21 09:34 +0100
                          Re: Programming style question Dr J R Stockton <reply1242@merlyn.demon.co.uk.invalid> - 2012-10-21 17:59 +0100
                            Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-22 09:55 +0200
                            Re: Programming style question Tim Streater <timstreater@greenbee.net> - 2012-10-22 09:50 +0100
                              Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-22 04:49 -0700
                                Re: Programming style question Tim Streater <timstreater@greenbee.net> - 2012-10-22 14:04 +0100
                                Re: Programming style question Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-10-22 16:07 +0200
                                Re: Programming style question Dr J R Stockton <reply1243@merlyn.demon.co.uk.invalid> - 2012-10-23 18:22 +0100
                              Re: Programming style question Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-10-22 16:01 +0200
                                Re: Programming style question Jim T. <x@y.z> - 2012-10-22 12:16 -0400
                                  Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-22 11:40 -0700
                                    Re: Programming style question Gene Wirchenko <genew@ocis.net> - 2012-10-22 14:01 -0700
                                      Re: Programming style question Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-10-23 10:07 +0200
                                        Re: Programming style question Gene Wirchenko <genew@ocis.net> - 2012-10-23 09:43 -0700
                                      Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-23 11:49 +0200
                                        Re: Programming style question Jim T. <x@y.z> - 2012-10-23 15:52 -0400
                                          Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-23 13:07 -0700
                                          Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-23 22:45 +0200
                                            Re: Programming style question Gene Wirchenko <genew@ocis.net> - 2012-10-23 14:14 -0700
                                              Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-24 09:35 +0200
                                                Re: Programming style question Gene Wirchenko <genew@ocis.net> - 2012-10-24 10:17 -0700
                                                  Re: Programming style question Dr J R Stockton <reply1243@merlyn.demon.co.uk.invalid> - 2012-10-25 18:41 +0100
                                                    Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-25 17:03 -0700
                                                      Re: Programming style question Gene Wirchenko <genew@ocis.net> - 2012-10-25 20:14 -0700
                                                        Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-25 20:46 -0700
                                                          Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-26 09:55 +0200
                                                            Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-26 06:18 -0700
                                                              Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-26 17:43 +0200
                                                          Re: Programming style question Gene Wirchenko <genew@ocis.net> - 2012-10-26 09:57 -0700
                                                            Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-26 10:24 -0700
                                                              Re: Programming style question Gene Wirchenko <genew@ocis.net> - 2012-10-26 14:18 -0700
                                                          Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-27 10:45 +0200
                                                            Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-27 12:58 +0200
                                                              Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-27 16:18 +0200
                                                                Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-27 18:08 +0200
                                                                  Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-27 18:26 +0200
                                                                    Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-27 19:43 +0200
                                                                      Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-27 21:09 +0200
                                                                        Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-27 22:53 +0200
                                                                Re: Programming style question John G Harris <john@nospam.demon.co.uk> - 2012-10-28 11:37 +0000
                                                                  Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-28 12:50 +0100
                                                                    Re: Programming style question Martin Leese <please@see.Web.for.e-mail.INVALID> - 2012-10-28 13:43 -0600
                                                                      Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-28 23:58 +0100
                                                                    Re: Programming style question John G Harris <john@nospam.demon.co.uk> - 2012-10-29 11:18 +0000
                                                        Re: Programming style question Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2012-10-27 23:58 +0200
                                                          Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-28 12:43 +0100
                                                        Re: Programming style question Dr J R Stockton <reply1243@merlyn.demon.co.uk.invalid> - 2012-10-27 19:44 +0100
                                                Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-24 12:06 -0700
                                                  Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-24 21:27 +0200
                                                  Re: Programming style question Gene Wirchenko <genew@ocis.net> - 2012-10-24 15:12 -0700
                                                  Re: Programming style question Tim Streater <timstreater@greenbee.net> - 2012-10-25 01:29 +0100
                                                Re: Programming style question Stefan Weiss <krewecherl@gmail.com> - 2012-10-24 21:25 +0200
                                                  Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-24 21:57 +0200
                                                    Re: Programming style question Stefan Weiss <krewecherl@gmail.com> - 2012-10-25 01:11 +0200
                                                      Re: Programming style question Christoph Becker <cmbecker69@gmx.de> - 2012-10-25 02:00 +0200
                                                        Re: Programming style question Scott Sauyet <scott.sauyet@gmail.com> - 2012-10-24 20:27 -0700
                                                      Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-25 08:44 +0200
                                                        Re: Programming style question Adam Silver <adambsilver@gmail.com> - 2012-10-25 04:28 -0700
                                                      Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-25 23:18 +0200
                                                        Re: Programming style question Scott Sauyet <scott.sauyet@gmail.com> - 2012-10-26 08:24 -0700
                                                        Re: Programming style question Stefan Weiss <krewecherl@gmail.com> - 2012-11-07 22:13 +0100
                                                          Re: Programming style question Tim Streater <timstreater@greenbee.net> - 2012-11-07 21:51 +0000
                                                          Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-07 23:47 +0100
                                                            Re: Programming style question Stefan Weiss <krewecherl@gmail.com> - 2012-11-08 01:14 +0100
                                                          Re: Programming style question Gene Wirchenko <genew@ocis.net> - 2012-11-07 16:51 -0800
                                                            Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-11-08 16:45 +0100
                                                              Re: Programming style question Scott Sauyet <scott.sauyet@gmail.com> - 2012-11-08 08:02 -0800
                                                                Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-11-08 17:39 +0100
                                                              Re: Programming style question Gene Wirchenko <genew@ocis.net> - 2012-11-08 11:25 -0800
                                                                Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-11-08 22:25 +0100
                                                                  Re: Programming style question John G Harris <john@nospam.demon.co.uk> - 2012-11-09 10:19 +0000
                                                                    Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-11-09 17:41 +0100
                                                                    Re: Programming style question Dr J R Stockton <reply1245@merlyn.demon.co.uk.invalid> - 2012-11-10 22:38 +0000
                                  Re: Programming style question Scott Sauyet <scott.sauyet@gmail.com> - 2012-10-22 12:32 -0700
                                  Re: Programming style question Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-10-23 10:05 +0200
                              Re: Programming style question Christoph Becker <cmbecker69@gmx.de> - 2012-10-22 22:11 +0200
                                Re: Programming style question Stefan Weiss <krewecherl@gmail.com> - 2012-10-23 02:42 +0200
                                  Re: Programming style question Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-10-23 10:08 +0200
                Re: Programming style question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-20 18:34 +0200
    Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-18 11:57 +0200
      Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-18 13:06 +0100
        Re: Programming style question "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-10-18 15:32 +0300
          Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-18 13:37 +0100
            Re: Programming style question "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-10-18 15:55 +0300
            Re: Programming style question John G Harris <john@nospam.demon.co.uk> - 2012-10-19 10:12 +0100
              Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-19 07:24 -0700
                Re: Programming style question John G Harris <john@nospam.demon.co.uk> - 2012-10-20 17:47 +0100
            Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-24 01:15 +0100
              Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-24 01:31 +0100
              Re: Programming style question "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-10-24 07:34 +0300
                Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-24 19:57 +0200
                  Re: Programming style question "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-10-24 21:02 +0300
          Re: Programming style question Patricia Shanahan <pats@acm.org> - 2012-10-18 20:15 -0700
        Re: Programming style question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-24 01:21 +0100
    Re: Programming style question Scott Sauyet <scott.sauyet@gmail.com> - 2012-10-21 17:57 -0700

Page 2 of 6 — ← Prev page 1 [2] 3 4 5 6  Next page →


#16787

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2012-10-22 09:55 +0200
Message-ID<XnsA0F464FB65E3eejj99@194.109.133.133>
In reply to#16767
Dr J R Stockton wrote on 21 okt 2012 in comp.lang.javascript:

> In comp.lang.javascript message <timstreater-13F0F1.22343820102012@news.
> individual.net>, Sat, 20 Oct 2012 22:34:38, Tim Streater
> <timstreater@greenbee.net> posted:
> 
>>
>>I might do something like:
>>
>> if  (n==0)  n = "No";
>> console.info (n + " results);
>>
>>but that's a trivial example.
> 
>  console.info( (n||"No") + " results" ) ; // ??

console.info( (n||"No") + " resul" + (n!=1) ?"ts" :"t" ); 


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

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


#16788

FromTim Streater <timstreater@greenbee.net>
Date2012-10-22 09:50 +0100
Message-ID<timstreater-89CF41.09503722102012@news.individual.net>
In reply to#16767
In article <cP4vbFDDoChQFwQo@invalid.uk.co.demon.merlyn.invalid>,
 Dr J R Stockton <reply1242@merlyn.demon.co.uk.invalid> wrote:

> In comp.lang.javascript message <timstreater-13F0F1.22343820102012@news.
> individual.net>, Sat, 20 Oct 2012 22:34:38, Tim Streater
> <timstreater@greenbee.net> posted:
> 
> >
> >I might do something like:
> >
> > if  (n==0)  n = "No";
> > console.info (n + " results);
> >
> >but that's a trivial example.
> 
>  console.info( (n||"No") + " results" ) ; // ??

I find constructs such as this to be unreadable. I never use them since 
I don't want to spend 20 mins figuring it out the first time, and 
another 20 mins convincing myself it's correct each time I come across 
it.

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

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


#16789

FromPatricia Shanahan <pats@acm.org>
Date2012-10-22 04:49 -0700
Message-ID<K62dnZWtQpVNrxjNnZ2dnUVZ_uWdnZ2d@earthlink.com>
In reply to#16788
On 10/22/2012 1:50 AM, Tim Streater wrote:
> In article <cP4vbFDDoChQFwQo@invalid.uk.co.demon.merlyn.invalid>,
> Dr J R Stockton <reply1242@merlyn.demon.co.uk.invalid> wrote:
>
>> In comp.lang.javascript message <timstreater-13F0F1.22343820102012@news.
>> individual.net>, Sat, 20 Oct 2012 22:34:38, Tim Streater
>> <timstreater@greenbee.net> posted:
>>
>> >
>> >I might do something like:
>> >
>> > if  (n==0)  n = "No";
>> > console.info (n + " results);
>> >
>> >but that's a trivial example.
>>
>>  console.info( (n||"No") + " results" ) ; // ??
>
> I find constructs such as this to be unreadable. I never use them since
> I don't want to spend 20 mins figuring it out the first time, and
> another 20 mins convincing myself it's correct each time I come across it.
>

I think 20 minutes is a slight exaggeration. It only took me a moment to
remember the key fact about "||".

On the other hand, it seems to me to be at least a little bit less
direct and readable than the if-else alternative. I would prefer if-else
unless the alternative had a measurable benefit in something I really
needed to optimize for.

Patricia

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


#16790

FromTim Streater <timstreater@greenbee.net>
Date2012-10-22 14:04 +0100
Message-ID<timstreater-127726.14045622102012@news.individual.net>
In reply to#16789
In article <K62dnZWtQpVNrxjNnZ2dnUVZ_uWdnZ2d@earthlink.com>,
 Patricia Shanahan <pats@acm.org> wrote:

> On 10/22/2012 1:50 AM, Tim Streater wrote:
> > In article <cP4vbFDDoChQFwQo@invalid.uk.co.demon.merlyn.invalid>,
> > Dr J R Stockton <reply1242@merlyn.demon.co.uk.invalid> wrote:
> >
> >> In comp.lang.javascript message <timstreater-13F0F1.22343820102012@news.
> >> individual.net>, Sat, 20 Oct 2012 22:34:38, Tim Streater
> >> <timstreater@greenbee.net> posted:
> >>
> >> >
> >> >I might do something like:
> >> >
> >> > if  (n==0)  n = "No";
> >> > console.info (n + " results);
> >> >
> >> >but that's a trivial example.
> >>
> >>  console.info( (n||"No") + " results" ) ; // ??
> >
> > I find constructs such as this to be unreadable. I never use them since
> > I don't want to spend 20 mins figuring it out the first time, and
> > another 20 mins convincing myself it's correct each time I come across it.
> >
> 
> I think 20 minutes is a slight exaggeration. It only took me a moment to
> remember the key fact about "||".

That execution stops as soon as one expression has value "true"? Sure. 
But personally I don't regard the non-zeroness of n as being anything to 
do with boolean value "true", and even less that a non-empty string has 
boolean value "true", either. Which is why I wrote it the way I did.

I note that some people do, but IMO they should go off and write in 
perl. perl programmers, IME, like to be clever and obscure; this shows 
up in their code, documentation, and in books on perl.

> On the other hand, it seems to me to be at least a little bit less
> direct and readable than the if-else alternative. I would prefer if-else
> unless the alternative had a measurable benefit in something I really
> needed to optimize for.

Well exactly.

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

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


#16795

FromHans-Georg Michna <hans-georgNoEmailPlease@michna.com>
Date2012-10-22 16:07 +0200
Message-ID<7hka88dag6i1edj12nms09gq3m8bp850eq@4ax.com>
In reply to#16789
On Mon, 22 Oct 2012 04:49:59 -0700, Patricia Shanahan wrote:

>On the other hand, it seems to me to be at least a little bit less
>direct and readable than the if-else alternative. I would prefer if-else
>unless the alternative had a measurable benefit in something I really
>needed to optimize for.

My guess is that it seems less direct only because you are not
used to it as much as to if-then-else constructs, because you
are coming from other languages.

I have done some JavaScript programming recently. To me, the
if-then-else construct looks less direct and verbose. For
example, the n variable is touched a full three times, rather
than just once in the more direct (from my point of view)
version.

Hans-Georg

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


#16820

FromDr J R Stockton <reply1243@merlyn.demon.co.uk.invalid>
Date2012-10-23 18:22 +0100
Message-ID<9gqXwFDLJthQFwGj@invalid.uk.co.demon.merlyn.invalid>
In reply to#16789
In comp.lang.javascript message <K62dnZWtQpVNrxjNnZ2dnUVZ_uWdnZ2d@earthl
ink.com>, Mon, 22 Oct 2012 04:49:59, Patricia Shanahan <pats@acm.org>
posted:

>On 10/22/2012 1:50 AM, Tim Streater wrote:
>> In article <cP4vbFDDoChQFwQo@invalid.uk.co.demon.merlyn.invalid>,
>> Dr J R Stockton <reply1242@merlyn.demon.co.uk.invalid> wrote:
>>
>>> In comp.lang.javascript message <timstreater-13F0F1.22343820102012@news.
>>> individual.net>, Sat, 20 Oct 2012 22:34:38, Tim Streater
>>> <timstreater@greenbee.net> posted:
>>>
>>> >
>>> >I might do something like:
>>> >
>>> > if  (n==0)  n = "No";
>>> > console.info (n + " results);
>>> >
>>> >but that's a trivial example.
>>>
>>>  console.info( (n||"No") + " results" ) ; // ??
>>
>> I find constructs such as this to be unreadable. I never use them since
>> I don't want to spend 20 mins figuring it out the first time, and
>> another 20 mins convincing myself it's correct each time I come across it.

With a scratchpad to hand, one should be able to test it in a minute or
two.

>I think 20 minutes is a slight exaggeration. It only took me a moment to
>remember the key fact about "||".
>
>On the other hand, it seems to me to be at least a little bit less
>direct and readable than the if-else alternative. I would prefer if-else
>unless the alternative had a measurable benefit in something I really
>needed to optimize for.

Mine has the advantage of not altering the variable n.  It is safer if
display or diagnostic code has no side-effects.

-- 
 (c) John Stockton, nr London, UK.  Mail via homepage.  Turnpike v6.05  MIME.
  Web  <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms and links;
  Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
 No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

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


#16794

FromHans-Georg Michna <hans-georgNoEmailPlease@michna.com>
Date2012-10-22 16:01 +0200
Message-ID<iuja88hvfo1fs4ai8giqe79d0p7g1b1hq9@4ax.com>
In reply to#16788
On Mon, 22 Oct 2012 09:50:37 +0100, Tim Streater wrote:

>>  console.info( (n||"No") + " results" ) ; // ??

>I find constructs such as this to be unreadable. I never use them since 
>I don't want to spend 20 mins figuring it out the first time, and 
>another 20 mins convincing myself it's correct each time I come across 
>it.

I can only report that I got used to these typical JavaScript
expressions or phrases quite easily.

This particular case is useful also because it teaches you what
the || operator actually does. It looks like an OR operator and
often acts like one, but it really does something else.

My personal take is that the language is only the base on which
you build. You have to master the base, because if you cannot do
that, how can you even hope to build something more complex on
top of it?

And if you master only a part of the language, your programs
will always be inefficient.

The only gripe I have with the expression above is the missing
spaces that are commonly used to separate operators and operands
from each other, not to mention the excess space, thus better:

console.info((n || "No") + " results");

or

console.info((n || "No") + " result" + (n === 1 ? "" : "s"));

Hans-Georg

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


#16800

FromJim T. <x@y.z>
Date2012-10-22 12:16 -0400
Message-ID<s4sa889g1978stjqgqhg5esjpnk7oqeqgj@4ax.com>
In reply to#16794
On Mon, 22 Oct 2012 16:01:24 +0200, Hans-Georg Michna
<hans-georgNoEmailPlease@michna.com> wrote:

>On Mon, 22 Oct 2012 09:50:37 +0100, Tim Streater wrote:
>
>>>  console.info( (n||"No") + " results" ) ; // ??
>
>>I find constructs such as this to be unreadable. I never use them since 
>>I don't want to spend 20 mins figuring it out the first time, and 
>>another 20 mins convincing myself it's correct each time I come across 
>>it.
>
>I can only report that I got used to these typical JavaScript
>expressions or phrases quite easily.
>
>This particular case is useful also because it teaches you what
>the || operator actually does. It looks like an OR operator and
>often acts like one, but it really does something else.

I have to admit, I've always just assumed || expressions always
evaluated to a  boolean. Maybe it's time I actually read the specs...

>My personal take is that the language is only the base on which
>you build. You have to master the base, because if you cannot do
>that, how can you even hope to build something more complex on
>top of it?
>
>And if you master only a part of the language, your programs
>will always be inefficient.
>
>The only gripe I have with the expression above is the missing
>spaces that are commonly used to separate operators and operands
>from each other, not to mention the excess space, thus better:
>
>console.info((n || "No") + " results");
>
>or
>
>console.info((n || "No") + " result" + (n === 1 ? "" : "s"));
>
>Hans-Georg

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


#16801

FromPatricia Shanahan <pats@acm.org>
Date2012-10-22 11:40 -0700
Message-ID<pPCdncn5wICZDhjNnZ2dnUVZ_uednZ2d@earthlink.com>
In reply to#16800
On 10/22/2012 9:16 AM, Jim T. wrote:
> On Mon, 22 Oct 2012 16:01:24 +0200, Hans-Georg Michna
> <hans-georgNoEmailPlease@michna.com> wrote:
...
>> This particular case is useful also because it teaches you what
>> the || operator actually does. It looks like an OR operator and
>> often acts like one, but it really does something else.
>
> I have to admit, I've always just assumed || expressions always
> evaluated to a  boolean. Maybe it's time I actually read the specs...
...

This sort of issue is why I like to read a book from start to finish
when learning another language. The "||" construct looks similar in C,
Java, and JavaScript, and has some flavor of an inclusive-or with short
circuit in all three, but is not exactly the same in any pair of them.

Patricia

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


#16806

FromGene Wirchenko <genew@ocis.net>
Date2012-10-22 14:01 -0700
Message-ID<tucb88te23lg15laq5a0meriminejqeko1@4ax.com>
In reply to#16801
On Mon, 22 Oct 2012 11:40:27 -0700, Patricia Shanahan <pats@acm.org>
wrote:

>On 10/22/2012 9:16 AM, Jim T. wrote:
>> On Mon, 22 Oct 2012 16:01:24 +0200, Hans-Georg Michna
>> <hans-georgNoEmailPlease@michna.com> wrote:
>...
>>> This particular case is useful also because it teaches you what
>>> the || operator actually does. It looks like an OR operator and
>>> often acts like one, but it really does something else.
>>
>> I have to admit, I've always just assumed || expressions always
>> evaluated to a  boolean. Maybe it's time I actually read the specs...
>...
>
>This sort of issue is why I like to read a book from start to finish
>when learning another language. The "||" construct looks similar in C,
>Java, and JavaScript, and has some flavor of an inclusive-or with short
>circuit in all three, but is not exactly the same in any pair of them.

     I tend to avoid such tricks because of this.  Slightly different
syntax and semantics between similar languages: ugh!

Sincerely,

Gene Wirchenko

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


#16812

FromHans-Georg Michna <hans-georgNoEmailPlease@michna.com>
Date2012-10-23 10:07 +0200
Message-ID<fvjc88586mh1sbkgklf2j9irq4fhsebulh@4ax.com>
In reply to#16806
On Mon, 22 Oct 2012 14:01:32 -0700, Gene Wirchenko wrote:

>     I tend to avoid such tricks because of this.  Slightly different
>syntax and semantics between similar languages: ugh!

What tricks? There are no tricks. Languages are just different,
and JavaScript is very often highly efficient and expressive.

Either you master the language or you don't.

Hans-Georg

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


#16815

FromGene Wirchenko <genew@ocis.net>
Date2012-10-23 09:43 -0700
Message-ID<16id8817n6l14i2l172bn12sp07j00r2l5@4ax.com>
In reply to#16812
On Tue, 23 Oct 2012 10:07:04 +0200, Hans-Georg Michna
<hans-georgNoEmailPlease@michna.com> wrote:

>On Mon, 22 Oct 2012 14:01:32 -0700, Gene Wirchenko wrote:
>
>>     I tend to avoid such tricks because of this.  Slightly different
>>syntax and semantics between similar languages: ugh!
>
>What tricks? There are no tricks. Languages are just different,
>and JavaScript is very often highly efficient and expressive.

     Sure there are.  And when two or more languages are very similar
in syntax and semantics, it is easy to err.  Switch between similar
languages and it is easy use one language's semantics by mistake.

>Either you master the language or you don't.

     I use languages to get work done.  Mastering a language is only a
means to an end.

Sincerely,

Gene Wirchenko

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


#16814

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2012-10-23 11:49 +0200
Message-ID<XnsA0F578505BA8Eeejj99@194.109.133.133>
In reply to#16806
Gene Wirchenko wrote on 22 okt 2012 in comp.lang.javascript:

>>This sort of issue is why I like to read a book from start to finish
>>when learning another language. The "||" construct looks similar in C,
>>Java, and JavaScript, and has some flavor of an inclusive-or with short
>>circuit in all three, but is not exactly the same in any pair of them.
> 
>      I tend to avoid such tricks because of this. 

Using || and && are NOT yust tricks.
[unlees you define programming as pure trickery]

compare:

var a = f();
var b = g();
var c = a || b;

with

var c = f() || g();

The two scripts are not equal in Javascript!

The positioning of the two funcions is relevant,
not as expected in pure calculus.

The difference is that if the first function is 
false [in the case of || ]
or
true [in the case of && ]
the second function is not executed.

This has two efficient advantages [1,2] 
and one avoidable disadvantage [3]:

1 Needless processor-time is spared.

2 Errors in the second function, 
  detected by the first function,
  ase prevented do collateral damage.

3 Inexperienced programmers planning to use collateral results
  of the second function, could be bewildered.

> Slightly different syntax and semantics between similar languages: ugh!

Oh grow up, and do not dwell on your professed extensive experience with 
other languages, but learn to use the lingo at hand.

If languages should be similar, there would be no logical need for using 
different languages, and we could follow up in Dutch, Greek or Malay.

Different languages are needed, because among other reasons:

1 speaker / programmer and listener / engine should understand each other.

2 monoglotism is less fun, gives less insight in languages, lessens 
developmental change and does not give you this marvelous incentive to use 
your biological brain.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

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


#16816

FromJim T. <x@y.z>
Date2012-10-23 15:52 -0400
Message-ID<f5td88dckgks06mf02cssn9e0116qum6c8@4ax.com>
In reply to#16814
On Tue, 23 Oct 2012 11:49:38 +0200, "Evertjan."
<exxjxw.hannivoort@inter.nl.net> wrote:

>Gene Wirchenko wrote on 22 okt 2012 in comp.lang.javascript:
>
>>>This sort of issue is why I like to read a book from start to finish
>>>when learning another language. The "||" construct looks similar in C,
>>>Java, and JavaScript, and has some flavor of an inclusive-or with short
>>>circuit in all three, but is not exactly the same in any pair of them.
>> 
>>      I tend to avoid such tricks because of this. 
>
>Using || and && are NOT yust tricks.
>[unlees you define programming as pure trickery]
>
>compare:
>
>var a = f();
>var b = g();
>var c = a || b;
>
>with
>
>var c = f() || g();
>
>The two scripts are not equal in Javascript!
>
>The positioning of the two funcions is relevant,
>not as expected in pure calculus.
>
>The difference is that if the first function is 
>false [in the case of || ]
>or
>true [in the case of && ]
>the second function is not executed.

I had also assumed this behaviour in JS, and thankfully was correct in
this case. Is there any language with syntax derived rom C that
doesn't behave this way?

>This has two efficient advantages [1,2] 
>and one avoidable disadvantage [3]:
>
>1 Needless processor-time is spared.
>
>2 Errors in the second function, 
>  detected by the first function,
>  ase prevented do collateral damage.
>
>3 Inexperienced programmers planning to use collateral results
>  of the second function, could be bewildered.
>
>> Slightly different syntax and semantics between similar languages: ugh!
>
>Oh grow up, and do not dwell on your professed extensive experience with 
>other languages, but learn to use the lingo at hand.
>
>If languages should be similar, there would be no logical need for using 
>different languages, and we could follow up in Dutch, Greek or Malay.
>
>Different languages are needed, because among other reasons:
>
>1 speaker / programmer and listener / engine should understand each other.
>
>2 monoglotism is less fun, gives less insight in languages, lessens 
>developmental change and does not give you this marvelous incentive to use 
>your biological brain.

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


#16817

FromPatricia Shanahan <pats@acm.org>
Date2012-10-23 13:07 -0700
Message-ID<hrqdnZzYONSIZBvNnZ2dnUVZ_hOdnZ2d@earthlink.com>
In reply to#16816
On 10/23/2012 12:52 PM, Jim T. wrote:
> On Tue, 23 Oct 2012 11:49:38 +0200, "Evertjan."
> <exxjxw.hannivoort@inter.nl.net> wrote:
>
>> Gene Wirchenko wrote on 22 okt 2012 in comp.lang.javascript:
>>
>>>> This sort of issue is why I like to read a book from start to finish
>>>> when learning another language. The "||" construct looks similar in C,
>>>> Java, and JavaScript, and has some flavor of an inclusive-or with short
>>>> circuit in all three, but is not exactly the same in any pair of them.
>>>
>>>       I tend to avoid such tricks because of this.
>>
>> Using || and && are NOT yust tricks.
>> [unlees you define programming as pure trickery]
>>
>> compare:
>>
>> var a = f();
>> var b = g();
>> var c = a || b;
>>
>> with
>>
>> var c = f() || g();
>>
>> The two scripts are not equal in Javascript!
>>
>> The positioning of the two funcions is relevant,
>> not as expected in pure calculus.
>>
>> The difference is that if the first function is
>> false [in the case of || ]
>> or
>> true [in the case of && ]
>> the second function is not executed.
>
> I had also assumed this behaviour in JS, and thankfully was correct in
> this case. Is there any language with syntax derived rom C that
> doesn't behave this way?

I knew it has this behavior because of reading about JS, but I was not
in the least surprised. It is a short circuit operator in C, C#, Java,
Matlab, and in C++ provided || has not been overridden.

The only language I've used that has || as its logical inclusive-or
operator without guaranteeing short-circuit evaluation is Verilog. That
is something of an odd language, because a core subset can be compiled
to a gate list and used to define hardware.

Patricia

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


#16818

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2012-10-23 22:45 +0200
Message-ID<XnsA0F5E78026978eejj99@194.109.133.133>
In reply to#16816
Jim T. wrote on 23 okt 2012 in comp.lang.javascript:

>>The difference is that if the first function is 
>>false [in the case of || ]
>>or
>>true [in the case of && ]
>>the second function is not executed.
> 
> I had also assumed this behaviour in JS, and thankfully was correct in
> this case. Is there any language with syntax derived rom C that
> doesn't behave this way?

Asking what OTHER languages have the same syntax as Javascript?

This surely is off topic in this NG,
as it does not even help understanding Javascript.

==============

btw:

The power of || and && actually is enhanced tremendously 
by the weak typing.


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

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


#16819

FromGene Wirchenko <genew@ocis.net>
Date2012-10-23 14:14 -0700
Message-ID<722e889us0ep0mra233907giq2nnolqhge@4ax.com>
In reply to#16818
On Tue, 23 Oct 2012 22:45:26 +0200, "Evertjan."
<exxjxw.hannivoort@inter.nl.net> wrote:

>Jim T. wrote on 23 okt 2012 in comp.lang.javascript:
>
>>>The difference is that if the first function is 
>>>false [in the case of || ]
>>>or
>>>true [in the case of && ]
>>>the second function is not executed.
>> 
>> I had also assumed this behaviour in JS, and thankfully was correct in
>> this case. Is there any language with syntax derived rom C that
>> doesn't behave this way?
>
>Asking what OTHER languages have the same syntax as Javascript?
>
>This surely is off topic in this NG,
>as it does not even help understanding Javascript.

     Sure, it does.  If one knows how it works in all such languages,
one knows how it works in JavaScript.

>btw:
>
>The power of || and && actually is enhanced tremendously 
>by the weak typing.

     It looks to be something that could be occasionally useful, but
is likely to be a roadblock for someone learning the language.

Sincerely,

Gene Wirchenko

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


#16831

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2012-10-24 09:35 +0200
Message-ID<XnsA0F6619B1C3Ceejj99@194.109.133.133>
In reply to#16819
Gene Wirchenko wrote on 23 okt 2012 in comp.lang.javascript:

>>Asking what OTHER languages have the same syntax as Javascript?
>>
>>This surely is off topic in this NG,
>>as it does not even help understanding Javascript.
> 
>      Sure, it does.  If one knows how it works in all such languages,
> one knows how it works in JavaScript.

No, if one knows how it works in THIS language, 
it does not matter how it works in OFF TOPIC languages.

Where do you infer that the workings in different languages 
would be the same????

Unless we want to start an historical thread about such binary operators, 
that is.

>>btw:
>>
>>The power of || and && actually is enhanced tremendously 
>>by the weak typing.
> 
>      It looks to be something that could be occasionally useful, 

Well, you are looking the wrong way, it seems,
these oprators, especially || are regulary used 
in Javascript to prevent errors when a function is not available
on a platform, so at runtime, when you as a programmer are not available.

var r = aDOMfunction || aDOMfunction("23px");

Probably more so than in strict mathematical statements, like

var c = a==3 || b==3 || (c+2)!=d;

btw, try:

alert( 'a!=3 is ' + !!( a==3 || alert('a is not 3') ) );

Oh sorry, perhaps you don't like this 
BECAUSE you cannot do this in some other language,
where you would desperately need multiline if-else-then constructs.

> but is likely to be a roadblock for someone learning the language.

Not at all. This is only because you want to learn Javascript 
by wrongfully assuming that it MUST work the same as in languages
you profess to be fluent in.

Learn an take advantage of these differences by reading scripts of others 
and see how nice solutions of the cross-engine and cross-browser problems 
become, and perhals also read our[!!] FAQ.


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

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


#16832

FromGene Wirchenko <genew@ocis.net>
Date2012-10-24 10:17 -0700
Message-ID<bh7g885ekk9ce55mmqec0bo2k12605ak6l@4ax.com>
In reply to#16831
On Wed, 24 Oct 2012 09:35:42 +0200, "Evertjan."
<exxjxw.hannivoort@inter.nl.net> wrote:

>Gene Wirchenko wrote on 23 okt 2012 in comp.lang.javascript:
>
>>>Asking what OTHER languages have the same syntax as Javascript?
>>>
>>>This surely is off topic in this NG,
>>>as it does not even help understanding Javascript.
>> 
>>      Sure, it does.  If one knows how it works in all such languages,
>> one knows how it works in JavaScript.
>
>No, if one knows how it works in THIS language, 
>it does not matter how it works in OFF TOPIC languages.

     Unless one also uses those other languages.  If the two work
similarly, one may be able to use techniques used in the other
language.

>Where do you infer that the workings in different languages 
>would be the same????

     It happens quite frequently as there are common concepts
implemented in many languages: loops, arrays, arithmetic, and on.

>Unless we want to start an historical thread about such binary operators, 
>that is.
>
>>>btw:
>>>
>>>The power of || and && actually is enhanced tremendously 
>>>by the weak typing.
>> 
>>      It looks to be something that could be occasionally useful, 
>
>Well, you are looking the wrong way, it seems,
>these oprators, especially || are regulary used 
>in Javascript to prevent errors when a function is not available
>on a platform, so at runtime, when you as a programmer are not available.
>
>var r = aDOMfunction || aDOMfunction("23px");

     Which means what exactly?  More importantly, what does someone
who has never seen this structure before do in order to look it up?

>Probably more so than in strict mathematical statements, like
>
>var c = a==3 || b==3 || (c+2)!=d;
>
>btw, try:
>
>alert( 'a!=3 is ' + !!( a==3 || alert('a is not 3') ) );
>
>Oh sorry, perhaps you don't like this 
>BECAUSE you cannot do this in some other language,
>where you would desperately need multiline if-else-then constructs.

     It is obfuscated.  Many statements, I can glance at and know what
they do.  I tried your code.  I put
          var a=3;
before it.  The alert stated
          a!=3 is true

     OOPS!  It appears that the obfuscation got *you*.

     I see this obfuscation in other areas.  A common question in SQL
newsgroups is how to do such-and-such a query with one one SELECT.
Often, it is possible, but sometimes, the resulting query is difficult
to parse.

     Yes, shorter forms can be useful, but somewhere along the line,
one gets to where the short form is confusing.

     Overly-short forms, IME, tend to be less maintainable.  As I do a
lot of maintenance programming, I want the code to be clear.  With SQL
queries, I sometimes build my data in several steps.  If I ever have
to make changes, it is easier to spot the point where the change
should go.  In one statement, it is a Big Ball of Mud, and I
effectively have to rewrite the whole statement.  I prefer not to
write write-only code.

>> but is likely to be a roadblock for someone learning the language.
>
>Not at all. This is only because you want to learn Javascript 
>by wrongfully assuming that it MUST work the same as in languages
>you profess to be fluent in.

     No.  It is easier to learn something in another language if it is
the same.  If it is almost the same, it is easy to make mistakes
between the two.

>Learn an take advantage of these differences by reading scripts of others 
>and see how nice solutions of the cross-engine and cross-browser problems 
>become, and perhals also read our[!!] FAQ.

     I have read the FAQ and most of the Standard.  There is a lot
more needed than what is in them in order to totally know the
language.

     Some of these idioms are showstoppers, because how do you look
something up if you do not know its name and it is mainly an
arrangement of symbols?

Sincerely,

Gene Wirchenko

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


#16876

FromDr J R Stockton <reply1243@merlyn.demon.co.uk.invalid>
Date2012-10-25 18:41 +0100
Message-ID<lLthKcEanXiQFw$R@invalid.uk.co.demon.merlyn.invalid>
In reply to#16832
In comp.lang.javascript message <bh7g885ekk9ce55mmqec0bo2k12605ak6l@4ax.
com>, Wed, 24 Oct 2012 10:17:13, Gene Wirchenko <genew@ocis.net> posted:

>
>     Some of these idioms are showstoppers, because how do you look
>something up if you do not know its name and it is mainly an
>arrangement of symbols?

You look it up in the index of a book, where the symbols should come
before the words.  Four of my five JavaScript books are like that, one
having numbers before symbols.  The fifth, and most used, has no index.

-- 
 (c) John Stockton, nr London, UK.  Mail via homepage.  Turnpike v6.05  MIME.
  Web  <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms and links;
  Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
 No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

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


Page 2 of 6 — ← Prev page 1 [2] 3 4 5 6  Next page →

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


csiph-web