Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Difference between findPos("divThis") and findPos(divThis)

Reply
Thread Tools

Difference between findPos("divThis") and findPos(divThis)

 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-14-2011
J.R. wrote:

> On 14/11/2011 10:15, Thomas 'PointedEars' Lahn wrote:
>>>> var el = (typeof obj == 'string') ? document.getElementById(obj) : obj;
>>>> w = el.offsetWidth ;
>>>> h = el.offsetHeight;
>>>> ...
>>>>
>>>
>>> No, because the ";" marks the end of the variable declaration list. In
>>> the above code, you are indeed creating an implied global variable
>>> called h,

>>
>> No, it creates, if it succeeds (and does not throw an exception, as
>> observed in some instances with MSHTML before, and required in ECMAScript
>> Ed. 5 strict mode), a property with that name of the ECMAScript Global
>> object or overwrites a property with that name of an object in the scope
>> chain.

>
> Considering only the given code by Cal, it creates two implied global
> variables (w and h),


No. What happens is what I described, which is compliant with ECMAScript
Edition 3 Final, sections 11.13.1 and 8.6.2, and Editions 5 and 5.1,
sections 11.13.1 and 8.7.2, respectively.

> according to ECMA-262 3rd, section 12.2:


The current edition of ECMAScript is 5.1, June 2011 CE; not Edition 3
(Final), December 1999/March 2000 CE.

> "If the variable statement occurs inside a FunctionDeclaration, the
> variables are defined with function-local scope in that function, as
> described in s10.1.3. Otherwise, they are defined with *global scope*
> (that is, they are created as members of the global object, as described
> in 10.1.3) [...]"


That section's second sentence refers to *variable statements* outside of a
FunctionDeclaration, as made obvious by its first sentence.

>>> instead of creating a private variable in the function scope.

>> The proper terminology here is "_local_ variable", not what you wrote.

>
> As written above, the correct terminology is indeed "variable defined in
> the function-local scope".


No, a "variable defined *with* function-local scope" is something else than
"a variable defined *in* the function-local scope" (which you also did not
say before). The proper term for such a variable is _local_ variable, _not_
a "private" variable (whereas you said the latter before).

>> Visibility modifiers do not exist in these implementations; privacy can
>> only be achieved implicitly, through closures. So far there is no
>> closure in that code regarding those variables.

>
> ACK, except for the *only*.


Please name a counter-example using ECMAScript-conforming code where a
`private'-like property visibility is achieved explicitly.

>>> 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;

>>
>> Wrong. In fact, it turns out that syntactically *correct*, adjacent
>> `var' statements work better with editors capable of refactoring, such as
>> Eclipse JSDT.

>
> It's not wrong, it's a different approach instead.


No, it is wrong to say "must not use". "Must not" in English means "is not
allowed to". It is certainly allowed to use that syntactical construct, as
it constitutes nothing more than consecutive statements.

> ECMAScript permits using either a single var or many var statements as one
> wishes.


That is not what you said before.

> I prefer the single var pattern.


That is also not what you said before.

> And I don't care about Eclipse - I'm talking about JavaScript not JAVA.


Will you please at least *try* get yourself educated before you post?
Eclipse JSDT is the Eclipse _JavaScript_ Development Tools plugin.

> Personally, I prefer Python / Django but it doesn't matter to this
> discussion anyway.


Yes, it matters how a commonly used JavaScript IDE can deal with either of
those syntactical constructs.


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$> (2004)
 
Reply With Quote
 
 
 
 
Eric Bednarz
Guest
Posts: n/a
 
      11-14-2011
Thomas 'PointedEars' Lahn <> writes:

>>> 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;


> The comma operator does not help with this.


> The comma operator does not help with this.


> The comma …


RTFM. Anyhow, the earlier everybody exclusively uses the single var
pattern, the earlier the shitty Eclipse JSDT will get fixed.

 
Reply With Quote
 
 
 
 
J.R.
Guest
Posts: n/a
 
      11-14-2011
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.

>
>> - 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.

>> - 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. But what if we minify our code
<http://en.wikipedia.org/wiki/Minification_(programming)>, leaving out
unnecessary spaces? Code Minification is one of the best practices in
Javascript.

> 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. Code minification is a good practice, supported by
renowned JS developers and browser-vendors no matter how hard you try to
deny it just because you want do disqualify my statement. I really think
that you do not believe in your own words.

>
>>> 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>
>


When there is nothing left to say, you really enjoy finishing a
discussion off with this sort of Mr. Spock citation (Your logic is
flawed). Very characteristic.

--
Joao Rodrigues (J.R.)
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      11-14-2011
On Nov 14, 5:14 am, 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.


<URL: http://en.wikipedia.org/wiki/Anti-pattern > describes an
antipattern as:-

"In software engineering, an anti-pattern (or antipattern) is a
pattern that may be commonly used but is ineffective and/or
counterproductive in practice."

- which seems to require ineffective and/or counterproductive, both of
which could be demonstrated if true. However, ineffective is obviously
not relevant to variable declarations, as they are effective, with
near zero difference in behaviour between single variable declaration
statements and multiple variable declaration statements. So it is
counterproductive that seems pertinent, and given that this thread has
already demonstrated that continuing a statement across multiple lines
with each line except the last terminated with a comma is far from an
error resistant practice, the 'productivity' balance seems to already
have tipped away from this practice..

Personally I think that the comma is too visually indistinct and also
too visually similar to a semicolon to be a good candidate for source
code line termination. That is, it is not easy to see that you have
done the right thing while writing it, and it is also not easy for a
later reader to see what the code is attempting to do (and doing it
correctly or not). I tend to see opaque source code text as
counterproductive in itself.

>> Some people think all variables should be declared in a single
>> statement.


That would be ridiculously dogmatic of them.

>> 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.


And some people put variable declarations on a single line until that
line starts to get too long (for some arbitrary definition of too long
(or a formal coding standard if applicable) and the start a new
variable declaration statement on the next line.

> The advantages of using the Single var Pattern at the top of your
> functions are:
> - there's a single place to look for all the local variables needed
> by the function;


This is not an advantage over multiple consecutive variable
declaration statements at the top of that same function. And since a
single statement could become very long or extended over multiple
lines that is likely to become difficult to read and understand,
negating some of the advantage of only looking in a single place.

> - prevention of logical errors when a variable is used before it’s
> defined;


This is not an advantage over multiple consecutive variable
declaration statements at the top of that same function. And what do
"logical errors" consist of in this context? If variables are declared
they are declared as an execution context is entered.

In my experience when people have made a case for declaring variables
at the top of a function body it is on order that the structure/order
of the source code reflect the way in which the javascript engine is
going to behave (handling the declarations before any actual code
execution). It would seem an unjustified misapplication of this
justification to insist that those variable declarations be confined
to a single statement (especially since that same recommendation wants
the inner function declarations to appear at the top of the function
body as well, and they cannot be combined into a single statement).

> - Helps you remember to declare variables and therefore minimize
> globals;


This is not an advantage over multiple consecutive variable
declaration statements at the top of that same function.

> - only one var statement means less code to type and a reduction
> in the file size.


So it would be (at best) the opportunity to save half a dozen bytes
that is the only factor left that could justify the "antipattern"
assertion, on the grounds of the alternatives being
counterproductive. I won't be buying that argument as this would be
neutralised by zipping the resource to be sent to the client, while
the potential added obscurity in the source code looks too negative to
me.

>> 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>


Maybe, but a high proportion of "best practices" that have been
proposed for javascript have been pretty much BS. Misunderstandings/
miscomprehensions abound, but the people looking for rules that will
supposedly help them cannot easily tell. I always recommend that no
"best practice" should be taken seriously unless it is presented
alongside a reasoned justification, and that that justification stands
up to critical scrutiny. For anything that really is a "best practice"
that should be an achievable requirement.

And having seen your reasoning for only using a single variable
declaration statement per function body, it didn’t stand up.

> 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...


Not with javascript. Nonsense propagates like wildfire in this field.
Hence the need to look at the actual reasoning and not be impressed by
any argument from numbers.

>> 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.


Do you really know that? (and should it matter anyway as in isolation
that would only make for an argument from authority?)

> However, if you published some evidence such
> as performance tests, etc.,


Yes, test cases are really good at demonstrating a point. Indeed if
properly designed are much more convincing than reasoned argument. So
can the alternatives to using a single variable declaration statement
at the start of a function body be demonstrated to be
counterproductive with some test case?

> then you could state that some practice
> should be either considered a good one or avoided altogether.


Performance isn't everything. In practice most javascript performs
just fine (and ironically better and better as CPUs and javascript
engines get faster). It will be useful to know what factors impact
performance, so as to be able to achieve it when necessary or avoid
squandering it, but it has long been the case that the biggest expense
in software production is accounted for by code maintenance, making
source code clarity (readability, understandably and self-documenting-
ness) a completely valid subject for "best practices", and putting the
variable declarations at the top of a function body was originally
primarily about code clarity.

Richard.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-14-2011
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
 
Reply With Quote
 
J.R.
Guest
Posts: n/a
 
      11-14-2011
On 14/11/2011 15:19, Thomas 'PointedEars' Lahn wrote:

>> 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.


I would not say one person, but *many* experienced and renowned JS
developers and all major browser-vendors. Google and find by yourself.

>>> 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.


LOL. Very entertaining.

--
Joao Rodrigues (J.R.)
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-14-2011
J.R. wrote:

> On 14/11/2011 15:19, Thomas 'PointedEars' Lahn wrote:
>>> 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.

>
> I would not say one person, but *many* experienced and renowned JS
> developers and all major browser-vendors. Google and find by yourself.


There we have the "ipse dixit" fallacy again.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
J.R.
Guest
Posts: n/a
 
      11-14-2011
On 14/11/2011 15:18, Richard Cornford wrote:
> On Nov 14, 5:14 am, 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.

>
> <URL: http://en.wikipedia.org/wiki/Anti-pattern> describes an
> antipattern as:-
>
> "In software engineering, an anti-pattern (or antipattern) is a
> pattern that may be commonly used but is ineffective and/or
> counterproductive in practice."
>
> - which seems to require ineffective and/or counterproductive, both of
> which could be demonstrated if true. However, ineffective is obviously
> not relevant to variable declarations, as they are effective, with
> near zero difference in behaviour between single variable declaration
> statements and multiple variable declaration statements. So it is
> counterproductive that seems pertinent, and given that this thread has
> already demonstrated that continuing a statement across multiple lines
> with each line except the last terminated with a comma is far from an
> error resistant practice, the 'productivity' balance seems to already
> have tipped away from this practice..
>
> Personally I think that the comma is too visually indistinct and also
> too visually similar to a semicolon to be a good candidate for source
> code line termination. That is, it is not easy to see that you have
> done the right thing while writing it, and it is also not easy for a
> later reader to see what the code is attempting to do (and doing it
> correctly or not). I tend to see opaque source code text as
> counterproductive in itself.
>
>>> Some people think all variables should be declared in a single
>>> statement.

>
> That would be ridiculously dogmatic of them.
>
>>> 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.

>
> And some people put variable declarations on a single line until that
> line starts to get too long (for some arbitrary definition of too long
> (or a formal coding standard if applicable) and the start a new
> variable declaration statement on the next line.
>
>> The advantages of using the Single var Pattern at the top of your
>> functions are:
>> - there's a single place to look for all the local variables needed
>> by the function;

>
> This is not an advantage over multiple consecutive variable
> declaration statements at the top of that same function. And since a
> single statement could become very long or extended over multiple
> lines that is likely to become difficult to read and understand,
> negating some of the advantage of only looking in a single place.
>
>> - prevention of logical errors when a variable is used before it’s
>> defined;

>
> This is not an advantage over multiple consecutive variable
> declaration statements at the top of that same function. And what do
> "logical errors" consist of in this context? If variables are declared
> they are declared as an execution context is entered.
>
> In my experience when people have made a case for declaring variables
> at the top of a function body it is on order that the structure/order
> of the source code reflect the way in which the javascript engine is
> going to behave (handling the declarations before any actual code
> execution). It would seem an unjustified misapplication of this
> justification to insist that those variable declarations be confined
> to a single statement (especially since that same recommendation wants
> the inner function declarations to appear at the top of the function
> body as well, and they cannot be combined into a single statement).
>
>> - Helps you remember to declare variables and therefore minimize
>> globals;

>
> This is not an advantage over multiple consecutive variable
> declaration statements at the top of that same function.
>
>> - only one var statement means less code to type and a reduction
>> in the file size.

>
> So it would be (at best) the opportunity to save half a dozen bytes
> that is the only factor left that could justify the "antipattern"
> assertion, on the grounds of the alternatives being
> counterproductive. I won't be buying that argument as this would be
> neutralised by zipping the resource to be sent to the client, while
> the potential added obscurity in the source code looks too negative to
> me.
>
>>> 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>

>
> Maybe, but a high proportion of "best practices" that have been
> proposed for javascript have been pretty much BS. Misunderstandings/
> miscomprehensions abound, but the people looking for rules that will
> supposedly help them cannot easily tell. I always recommend that no
> "best practice" should be taken seriously unless it is presented
> alongside a reasoned justification, and that that justification stands
> up to critical scrutiny. For anything that really is a "best practice"
> that should be an achievable requirement.
>
> And having seen your reasoning for only using a single variable
> declaration statement per function body, it didn’t stand up.
>
>> 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...

>
> Not with javascript. Nonsense propagates like wildfire in this field.
> Hence the need to look at the actual reasoning and not be impressed by
> any argument from numbers.
>
>>> 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.

>
> Do you really know that?


Yes, I do.

> (and should it matter anyway as in isolation
> that would only make for an argument from authority?)


No, it shouldn't matter because of the "fallacy of authority"
<http://www.constitution.org/col/logical_fallacies.htm>

>> However, if you published some evidence such
>> as performance tests, etc.,

>
> Yes, test cases are really good at demonstrating a point. Indeed if
> properly designed are much more convincing than reasoned argument. So
> can the alternatives to using a single variable declaration statement
> at the start of a function body be demonstrated to be
> counterproductive with some test case?
>
>> then you could state that some practice
>> should be either considered a good one or avoided altogether.

>
> Performance isn't everything. In practice most javascript performs
> just fine (and ironically better and better as CPUs and javascript
> engines get faster). It will be useful to know what factors impact
> performance, so as to be able to achieve it when necessary or avoid
> squandering it, but it has long been the case that the biggest expense
> in software production is accounted for by code maintenance, making
> source code clarity (readability, understandably and self-documenting-
> ness) a completely valid subject for "best practices", and putting the
> variable declarations at the top of a function body was originally
> primarily about code clarity.


Dear Richard,
Besides being a gentleman while writing your brilliant ideas down, you
were impeccable in your above comments! I must agree with you. Thank you
very much indeed.

PS. It's a pity that Thomas Lahn is not well educated and polite as you
and many others in this newsgroup. Although he really knows a lot about
JavaScript, his responses are usually unpleasant and aggressive. Also he
has a stupefying habit of changing his points of view just to discredit
someone.

--
Joao Rodrigues (J.R.)
 
Reply With Quote
 
Denis McMahon
Guest
Posts: n/a
 
      11-14-2011
On Mon, 14 Nov 2011 14:14:50 -0200, J.R. wrote:

> On 14/11/2011 12:03, Denis McMahon wrote:
>> On Mon, 14 Nov 2011 03:14:20 -0200, J.R. wrote:
>>
>>> The advantages of using the Single var Pattern at the top of your
>>> functions are:

>>
>> I'm not objecting to the suggestion that best practice includes
>> defining all your variables with var statements at the start of a
>> function. I agree that this is indeed best practice.
>>
>> I am, however, objecting to (a) your assertion that it "must" be done,
>> or (b) that best practice requires the use of a single var statement.


> You are right, I should have not used the "must not" as it expresses
> prohibition in English, and a code pattern is not necessarily one of the
> "best coding practices", although, in many cases, they seem to walk hand
> in hand. And sticking to a strict coding style is usually considered one
> of the best practices in many programming languages.
>
> Examples of "best practices" in JS:
> <http://dev.opera.com/articles/view/javascript-best-practices/>


Yep, read that.

> The above link is propped up by the Mozilla Developer Network
> <https://developer.mozilla.org/en-US/learn/javascript>, and there are
> other pages at MDN concerning to best practices in other aspects of
> development (extensions, security, ect.).


Noted that.

Can't see the bit about combining all the variable declarations into a
single var statement though.

Psst, jslint doesn't seem to want that either, it's quite happy as long
as (a) all variables are defined (b) at the start of the function.

Minimisation is good, but the following:

var a = "df";
var b = 17;
var c = [{s:'monkey',x:'male',a:3},{s:'elephant',x:'female' ,a:7},
{s:'mule',x:'neuter',a:1}];
var c1 = ["alpha","beta","gamma"];
var c2 = {o:'peter',f:'grass',z:'oregon',d:[{x:'male',q:3},
{x:'female',q:4}]};
var d = "mary had a little lamb";
var e = f = g = 0;

is going to be much easier to maintain than a single minimised statement:

var a="df",b=17,c=[{s:'monkey',x:'male',a:3},
{s:'elephant',x:'female',a:7},{s:'mule',x:'neuter' ,a:1}],c1=
["alpha","beta","gamma"],c2={o:'peter',f:'grass',z:'oregon',d:
[{x:'male',q:3},{x:'female',q:4}]},d="mary had a little lamb",e=f=g=0;

even if you do use more sensible variable and property names than the
ones I've selected.

Rgds

Denis McMahon
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      11-14-2011
Denis McMahon wrote on 14 nov 2011 in comp.lang.javascript:

> var e = f = g = 0;


This is not the same as:

var f;
var g;
var e = f = g = 0;

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 04-15-2011 04:00 AM
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 PM
difference between between these "char"s arnuld C++ 33 03-05-2007 03:11 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57