Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #8314
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail |
|---|---|
| Content-Type | text/plain; charset="UTF-8" |
| Message-ID | <4775188.ypaU67uLZW@PointedEars.de> (permalink) |
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| Organization | PointedEars Software (PES) |
| Date | Mon, 14 Nov 2011 15:04:54 +0100 |
| User-Agent | KNode/4.4.11 |
| Content-Transfer-Encoding | 8Bit |
| Subject | Re: Difference between findPos("divThis") and findPos(divThis) |
| Newsgroups | comp.lang.javascript |
| References | <j9olie$dsd$1@dont-email.me> <4775241.ypaU67uLZW@PointedEars.de> <j9p403$e80$1@dont-email.me> <2394720.ArG6xLiZAS@PointedEars.de> <j9p7t8$98q$1@dont-email.me> <j9pas5$ah2$1@speranza.aioe.org> <j9pb43$ah2$2@speranza.aioe.org> <j9pf87$rka$1@dont-email.me> <j9phhk$s3i$1@speranza.aioe.org> <4ec07ef1$0$28711$a8266bb1@newsreader.readnews.com> <j9q83e$b9m$1@speranza.aioe.org> |
| Followup-To | comp.lang.javascript |
| MIME-Version | 1.0 |
| Lines | 186 |
| NNTP-Posting-Date | 14 Nov 2011 15:04:54 CET |
| NNTP-Posting-Host | c4b3025a.newsspool3.arcor-online.net |
| X-Trace | DXC=_ea3eaEfV4Qf8j24CD<3lPMcF=Q^Z^V3X4Fo<]lROoRQ8kF<OcfhCO[dd@C8U4Bnn^DZm8W4\YJN\;?f@h5gMfb\3koVXd8EHBTKoP=Q51USfX |
| X-Complaints-To | usenet-abuse@arcor.de |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.javascript:8314 |
Followups directed to: comp.lang.javascript
Show key headers only | View raw
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").
>> 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.
> - 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.
> - 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;
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:
awk '
BEGIN {
total = 0;
var_lines = 0;
print;
}
/^[[:space:]]*var/ {
total += length();
var_lines++;
}
END {
print "LOCs: " NR;
print "var LOCs: " var_lines;
print "avg(length(var LOCs)): " total/var_lines " bytes";
savings = 2 * var_lines;
print "Potential savings (best case): " savings " bytes (2 * " var_lines
")";
"wc -c " FILENAME | getline wc_out;
split(wc_out, data);
filesize = data[1];
print "File size: " filesize " bytes";
print "Potential savings (best case): " savings/filesize * 100 " %";
}' \
object.js
LOCs: 2009
var LOCs: 66
avg(length(var LOCs)): 27.8788 bytes
Potential savings (best case): 132 bytes (2 * 66)
File size: 55694 bytes
Potential savings (best case): 0.237009 %
----
2 bytes per `var' line because changing
var.foo;\n
var.bar;\n
to
var.foo,\n
..bar;\n
saves two characters on the second, third etc. line per variable
declaration. However, it should be noted that the more readable form is
var\n
..foo,\n
..bar;\n
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).
With tab indentation, it is a change of
var.foo;\n
var.bar;\n
to
var\n
<HT>foo,\n
<HT>bar;\n
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.)
>> These are all permitted by the various standards, and they all work.
>> There is no reason that you "must" or "must not" do any of these.
>>
>> Also, "best practice" is invariably "in the opinion of the author of the
>> website / book / blog / whatever" that said example of best practice
>> appears in.
>
> There are patterns in any programming language usually adopted by
> programmers as "best coding practices". And it happens to other fields
> of study too. See <http://en.wikipedia.org/wiki/Best_Coding_Practices>
>
> If you get a bunch of authors (books, blogs, etc.) that state the same
> "best practices" in any programming language, then you can bet who is
> wrong or right...
>
>> As a matter of interest, what's your reference for "best
>> practice"? If I write a book or start a blog called "Javascript - Best
>> Practice" does that make it true?
>
> No, it doesn't, although I know you're a very experienced and smart
> programmer. However, if you published some evidence such as performance
> tests, etc., then you could state that some practice should be either
> considered a good one or avoided altogether.
Your logic is flawed: <http://en.wikipedia.org/wiki/Ipse_dixit>
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