Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #8327
| Message-ID | <11202628.XJjokVgzJq@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2011-11-14 18:19 +0100 |
| Subject | Re: Difference between findPos("divThis") and findPos(divThis) |
| Newsgroups | comp.lang.javascript |
| References | (8 earlier) <j9phhk$s3i$1@speranza.aioe.org> <4ec07ef1$0$28711$a8266bb1@newsreader.readnews.com> <j9q83e$b9m$1@speranza.aioe.org> <4775188.ypaU67uLZW@PointedEars.de> <j9rhta$etf$1@speranza.aioe.org> |
| Followup-To | comp.lang.javascript |
Followups directed to: comp.lang.javascript
J.R. wrote:
> On 14/11/2011 12:04, Thomas 'PointedEars' Lahn wrote:
>> J.R. wrote:
>>> On 14/11/2011 00:37, Denis McMahon wrote:
>>>> On Sun, 13 Nov 2011 20:49:25 -0200, J.R. wrote:
>>>>> it is one of the JavaScript best practices to declare all of the
>>>>> variables used in a function at the top of the function body. You may
>>>>> have many variables separated by commas, e.g
>>>>
>>>>> var el = ...,
>>>>> h = ...,
>>>>> w = ...,
>>>>> i, j, len, ..., z;
>>>>
>>>>> You must not use multiple var statements such as:
>>>>> var el;
>>>>> var h = ...;
>>>>> var z;
>>>>
>>>> Rubbish.
>>>>
>>>> You can use as many var statements as you like. Nothing in the
>>>> standards or the implementation prevents this, so "must not" is
>>>> incorrect.
>>>
>>> "must not" was exaggerated on purpose, because using many var statements
>>> is an antipattern in JavaScript.
>>
>> In your humble opinion (which may not even be your opinion but that of
>> the author of some botched book you read instead, and now you think you
>> know the language and its "best practices").
>
> I still have a long and winding road to go until I get a thorough
> comprehension about JavaScript. But what about you?
>
>>>> Some people think all variables should be declared in a single
>>>> statement. Some people use a different statement for each type of
>>>> variable, where type is the sort of data that it will be used to hold
>>>> (array, string, integer, float, object etc).
>>>> Some people just use one variable per var.
>>>
>>> The advantages of using the Single var Pattern at the top of your
>>> functions are:
>>
>> You are confusing two concepts here.
>
> No, I am not.
Yes, you are. One, declaring variables either on top of a function body or
anywhere. Two, using either consecutive `var' statements or one `var'
statement with a list.
>>> - there's a single place to look for all the local variables needed by
>>> the function;
>>
>> The comma operator does not help with this.
>>
>>> - prevention of logical errors when a variable is used before it’s
>>> defined;
>>
>> The comma operator does not help with this.
>
> Read again and notice that I was writing about adopting the Single var
> Pattern, instead of writing many var statements. It's not about using
> commas.
Apparently you don't even know what you write.
>>> - Helps you remember to declare variables and therefore minimize
>>> globals;
>>
>> The comma …
>>
>>> - only one var statement means less code to type and a reduction
>>> in the file size.
>>
>> Sometimes. Because for readable code, you will have to indent the
>> second, third aso. line. In particular, with the style you used above,
>> you do not save or add *any* byte. If this is not obvious to you, look
>> more carefully:
>>
>> var.el.=....,.........
>> ....h =....,..........
>> ....w.=....,..........
>> ....i,.j,.len,....,.z;
>>
>> vs.
>>
>> var.el.=....;.........
>> var.h.=....;..........
>> var.w.=....;..........
>> var.i,.j,.len,....,.z;
>>
>
> Yes, it is obvious to anyone.
Not to you, obviously, as you were claiming that your approach would save
space. It doesn't.
> But what if we minify our code>
> <http://en.wikipedia.org/wiki/Minification_(programming)>, leaving out
> unnecessary spaces?
A good minifier should indeed be capable to make one variable statement out
of consecutive ones. Minification becomes relevant when serving resources,
not when storing them.
> Code Minification is one of the best practices in Javascript.
Says who? The same person who says you must not use consecutive variable
statements? I see.
>> At any rate, the potential savings are so very small compared to what
>> trimming comments, minimization and gzipping can achieve that this is not
>> a convincing argument. Taking my current local object.js as an example:
>>
>> [snip]
>>
>> which saves *nothing* with \n =<LF> but *adds* one byte with \n
>> =<CR><LF> as it means removing a space [−1] and adding a newline [+1|+2]
>> on the first line, adding two spaces [+2] on the second line, and
>> removing "var" (3 bytes [−3]) and adding a space [+1] on the third line).
>>
>> [snip]
>>
>> which means removing one space [−1] and adding one newline [+1|+2] on the
>> first line, adding one<HT> [+1] on the second line and saving 3 bytes
>> [−3]
>> on the second line (a total of only 1 or two 2 saved bytes). (However,
>> tab indentation may require additional adjustments at the reader's, and
>> is therefore also not recommended for posting to Usenet.)
>
> Obviously, you are not considering a file with hundreds, perhaps
> thousands of lines.
Obviously you can't read.
Perhaps I should take David Mark's approach instead, and simply send you to
bed from time to time. Currently at least, your higher brain functions do
not appear to be very active.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Difference between findPos("divThis") and findPos(divThis) " Cal Who" <CalWhoNOSPAM@roadrunner.com> - 2011-11-13 09:51 -0500
Re: Difference between findPos("divThis") and findPos(divThis) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-13 18:25 +0100
Re: Difference between findPos("divThis") and findPos(divThis) " Cal Who" <CalWhoNOSPAM@roadrunner.com> - 2011-11-13 13:58 -0500
Re: Difference between findPos("divThis") and findPos(divThis) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-13 20:26 +0100
Re: Difference between findPos("divThis") and findPos(divThis) " Cal Who" <CalWhoNOSPAM@roadrunner.com> - 2011-11-13 15:04 -0500
Re: Difference between findPos("divThis") and findPos(divThis) "J.R." <groups_jr-1@yahoo.com.br> - 2011-11-13 18:55 -0200
Re: Difference between findPos("divThis") and findPos(divThis) "J.R." <groups_jr-1@yahoo.com.br> - 2011-11-13 18:59 -0200
Re: Difference between findPos("divThis") and findPos(divThis) " Cal Who" <CalWhoNOSPAM@roadrunner.com> - 2011-11-13 17:10 -0500
Re: Difference between findPos("divThis") and findPos(divThis) "J.R." <groups_jr-1@yahoo.com.br> - 2011-11-13 20:49 -0200
Re: Difference between findPos("divThis") and findPos(divThis) " Cal Who" <CalWhoNOSPAM@roadrunner.com> - 2011-11-13 20:00 -0500
Re: Difference between findPos("divThis") and findPos(divThis) Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-14 02:37 +0000
Re: Difference between findPos("divThis") and findPos(divThis) "J.R." <groups_jr-1@yahoo.com.br> - 2011-11-14 03:14 -0200
Re: Difference between findPos("divThis") and findPos(divThis) Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-14 14:03 +0000
Re: Difference between findPos("divThis") and findPos(divThis) "J.R." <groups_jr-1@yahoo.com.br> - 2011-11-14 14:14 -0200
Re: Difference between findPos("divThis") and findPos(divThis) Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-14 18:31 +0000
Re: Difference between findPos("divThis") and findPos(divThis) "Evertjan." <exjxw.hannivoort@interxnl.net> - 2011-11-14 18:36 +0000
Re: Difference between findPos("divThis") and findPos(divThis) Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-15 00:18 +0000
Re: Difference between findPos("divThis") and findPos(divThis) Scott Sauyet <scott.sauyet@gmail.com> - 2011-11-14 10:54 -0800
Re: Difference between findPos("divThis") and findPos(divThis) "Richard Cornford" <Richard@litotes.demon.co.uk> - 2011-11-15 01:23 +0000
Re: Difference between findPos("divThis") and findPos(divThis) Scott Sauyet <scott.sauyet@gmail.com> - 2011-11-16 06:04 -0800
Re: Difference between findPos("divThis") and findPos(divThis) Frobernik <nospam@nospam.com> - 2011-11-17 22:06 +0000
Re: Difference between findPos("divThis") and findPos(divThis) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-17 23:54 +0100
Re: Difference between findPos("divThis") and findPos(divThis) Frobernik <nospam@nospam.com> - 2011-11-18 09:33 +0000
Function arguments vs. local variables (was: Difference between findPos("divThis") and findPos(divThis)) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-19 16:05 +0100
Re: Function arguments vs. local variables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-19 18:17 +0100
Re: Function arguments vs. local variables Frobernik <nospam@nospam.com> - 2011-11-21 20:01 +0000
Re: Function arguments vs. local variables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-22 12:50 +0100
Re: Difference between findPos("divThis") and findPos(divThis) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-14 15:04 +0100
Re: Difference between findPos("divThis") and findPos(divThis) John G Harris <john@nospam.demon.co.uk> - 2011-11-14 15:13 +0000
Re: Difference between findPos("divThis") and findPos(divThis) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-14 18:27 +0100
Re: Difference between findPos("divThis") and findPos(divThis) Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2011-11-14 17:52 +0100
Re: Difference between findPos("divThis") and findPos(divThis) "J.R." <groups_jr-1@yahoo.com.br> - 2011-11-14 15:07 -0200
Re: Difference between findPos("divThis") and findPos(divThis) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-14 18:19 +0100
Re: Difference between findPos("divThis") and findPos(divThis) "J.R." <groups_jr-1@yahoo.com.br> - 2011-11-14 15:44 -0200
Re: Difference between findPos("divThis") and findPos(divThis) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-14 19:01 +0100
Re: Difference between findPos("divThis") and findPos(divThis) "J.R." <groups_jr-1@yahoo.com.br> - 2011-11-14 16:42 -0200
Re: Difference between findPos("divThis") and findPos(divThis) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-14 19:53 +0100
Re: Difference between findPos("divThis") and findPos(divThis) Richard Cornford <Richard@litotes.demon.co.uk> - 2011-11-14 09:18 -0800
Re: Difference between findPos("divThis") and findPos(divThis) "J.R." <groups_jr-1@yahoo.com.br> - 2011-11-14 16:22 -0200
Re: Difference between findPos("divThis") and findPos(divThis) Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-14 18:38 +0000
Re: Difference between findPos("divThis") and findPos(divThis) Gene Wirchenko <genew@ocis.net> - 2011-11-14 12:12 -0800
Re: Difference between findPos("divThis") and findPos(divThis) Gene Wirchenko <genew@ocis.net> - 2011-11-14 12:00 -0800
Re: Difference between findPos("divThis") and findPos(divThis) " Cal Who" <CalWhoNOSPAM@roadrunner.com> - 2011-11-14 08:48 -0500
Re: Difference between findPos("divThis") and findPos(divThis) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-14 13:15 +0100
Re: Difference between findPos("divThis") and findPos(divThis) "J.R." <groups_jr-1@yahoo.com.br> - 2011-11-14 13:03 -0200
Re: Difference between findPos("divThis") and findPos(divThis) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-14 17:24 +0100
Re: Difference between findPos("divThis") and findPos(divThis) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-14 13:00 +0100
Re: Difference between findPos("divThis") and findPos(divThis) John G Harris <john@nospam.demon.co.uk> - 2011-11-14 15:05 +0000
csiph-web