Javascript Best Practices Document v1.0

T

Thomas 'PointedEars' Lahn

VK said:
By "not far enough" you mean NN 2.x - NN 4.x ?

Not only, but they are included. At least NN 4.x definitely is.
IE supports it since 3.02

So? IE is probably the widest distributed, but not the only user agent
around. And the `in' operator has AFAIS not been specified in ECMAScript
before Edition 3.
If some other browser still doesn't have it implemented properly, the
primary task would be to see such browser disappeared from the human
memory as quick as possible, rather than support it an any way.

Let's just say your perception of reality appears to me to be somewhat
strange.
The most important is that if (someObject) method fails easily in too
many curcumstances appeared after prototypes and constructors
introduction.

Nobody unconditionally recommended `if (someObject)', instead you
used it as vessel for your "argumentation". You are confused, yes?
Check this

(... not Valid ...)
sample:

<html>
<head>
<title>Testcase</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
function test() {

self.abort = undefined;

So you had to construct a less-than-probable case to prove
your otherwise unfounded assumption. You made my day.
// Good practice:

No, it is not. The "in" operator is certainly a viable approach, but since
it lacks wide support, it cannot be considered best practice or even good
practice. But then you have seldom, if ever, bothered with such details.

Your experience in JS programming is still (despite several attempts of
explanation by experts to you) questionable at best. Since this is not
the first time, I strongly advise you read more before you post further
such bold statements here.
alert(self.hasProperty)

Your point being?
Where did you get method? You mean HasProperty moniker from .Net
framework? It has no relation with the discussion.

No, I mean(t) Object.prototype.hasOwnProperty() as specified in
ECMAScript 3 and implemented in JavaScript 1.5+, JScript 5.6+.
Since I seldom have to use it, I remembered the identifier wrong.
Why in the name you need to check the type of property in question?

Because that is the most viable approach to check it exists in the
sense that it refers to useful value.
What can it prove?

It can prove that it is not undefined, i.e. that it was not defined or
assigned undefined. It also can prove that the call operator may be
applied to it (see below).
Even if it's indeed a method (so it's typeof "function"),

.... or typeof ... == "object", for IE's DOM objects
what proof do you have that this is *that* method and not
some bogus?

I don't have. However, it has proven to be a viable approach to expect
methods that are named as documented to do what is documented, with only
a few exceptions in the implementation of the W3C DOM in IE.

I know when I mess with those methods in my included scripts and I know
who to blame then if it does not work. And since JS's object model is
dynamic, if either the UA's vendor or the user decide to cripple its
client-side script support or DOM, there is nothing a JS developer can
do about it. As JS/ECMAScript developer you always have to cope with the
host environment it runs in. So I simply don't need to bother with it.
For example if a property called `document.getElementsByTagName' exists
and it is a method, I have to assume that it can be called and returns
either `null', a HTMLCollection or not a HTMLCollection. Of course I
have to check its return value in some way before referring to it.
And even if it's indeed *that* method, what proof do you
have that it was implemented as documented? You can only protect
yourselve from initial "Object expected error" by doing
if ( (neededObject in self) && (neededStuff in neededObject) ) {...}

Rubbish. There cannot be such an error produced (and it can only in IE)
unless the call operator is applied to a non-undefined property or the
object model the host environment is completely broken. However, I
consider implementations that return something other than "function" or
"object" for a method to be badly broken, and so I do not support them.
The rest remains in the hands of the Lord and browser makers.

No, there are certain standards specified to adhere to, and UA vendors
state in the documentation what standards they support. Most of the time,
those statements have proven to be correct.
"self" has the only context to be.

You are talking nonsense. `self' has been and it is still a property of
Window objects, not by definition of the Global Object. Get yourself
informed, please.
Unless you've created a "self" variable and did your whole script dizzy:
self = something;
// var self = something ?
// window.self = something ?
...

That proves or disproves which one of my arguments? Again you are virtually
*constructing* something to try and fail to prove one of your arguments.
As "self" is one of so-called "tasty words" for identifiers, I see this
happens here and there and should be each time pointed.

Pardon? You are the one who suggested using `self' in the first place.

Unfortunately, I more and more get the impression that you do not really
know what you are talking about nor, more important, do you have any idea
what you are argueing against/for. Maybe a pillowful of decent sleep will
help you. Goodnight.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
VK said:
And even if it's indeed *that* method, what proof do you
have that it was implemented as documented? You can only protect
yourselve from initial "Object expected error" by doing
if ( (neededObject in self) && (neededStuff in neededObject) ) {...}

Rubbish. There cannot be such an error produced (and it can only in IE)
unless the call operator is applied to a non-undefined property [...]

It should read "unless ... to an undefined property", of course.
 
V

VK

So you had to construct a less-than-probable case
to prove your otherwise unfounded assumption.
You made my day.

And to make your evening also take a look at the title of this thread.
If say method X works in 98% of cases and method Y works in 99% of
cases then given all other issues being equal (simplicity, code
readability etc.), then method Y is a better practice.
In the "IN vs. UNDEFINED" case I don't see "other issues" equal
neither. They are definitely benifiting to "IN":

[1]
if ('someProperty' in someObject) {...}
follows the human language structure so it's easy to understand and
remember even for a non-experienced programmer: "if [there is]
someProperty in someObject". It's not a requirement of cource in the
programming languages but it's a suggested part of the common
programming culture.


[2]
if ('someProperty' in someObject) {...}
contains only one operation (hash LOOKUP)
while
if (someObject.someProperty) {...} has two operations:
1) resolve someObject.someProperty (hash LOOKUP)
2) do background casting to false/true

so the same for
if (typeof(someObject) == 'function')
1) get someObject typeof
2) compare with string

On double check
if ('someProperty1' in someObject) && ('someProperty2' in
someProperty1) {...}
we save on two unnecessary operation avoided.


The "in" operator is certainly a viable approach, but since
it lacks wide support,...

Do not get redicilous now. You mean that 99's ECMA lacks wide support?
Something like: "Behold: Pan 0.13.0 - The Whole Remains Beautiful just
failed to execute my script" ?

As we are talking about cross-platform scripting here then terms like
"some browsers" should be sctrictly avoided in the discussion.
Otherwise it's just a hidden way to say "I simply don't like it/you".

There are not "some browsers". There is IE 3.x - 7.x, Netscape 2.x -
8.x, Firefox 1.0.x, Pan 0.x and so on and so on. You think that a
particular method will fail in some environment? So tell us where
exactly including brends and major/minor versions. In many cases the
issue will desappear by itself just because you'll not be willing to
get funny on public by showing *what* browser you're hiding you
personal preferences behind of.

I mean(t) Object.prototype.hasOwnProperty() as specified in
ECMAScript 3 and implemented in JavaScript 1.5+, JScript 5.6+.
Since I seldom have to use it, I remembered the identifier wrong.

hasOwnProperty() method deals with script objects *only* and it doesn't
work for DOM interface check, which we are discussing now (try
hasOwnProperty on window). IN method covers both cases.

But I'm still glad to see that despite some luck of experience you're
showing more theoretical knowledge as it was demonstrated before. KOK
(Kick On Kick)
 
T

Thomas 'PointedEars' Lahn

VK wrote:

[Please learn how to quote, see the FAQ. Quotation levels corrected here.]
And to make your evening also take a look at the title of this thread.

Ahh, yes. Quite bold of you to give it v1.0; it's more like v0.1.
If say method X works in 98% of cases and method Y works in 99% of
cases then given all other issues being equal (simplicity, code
readability etc.), then method Y is a better practice.

Which exactly is why "in" is not.
In the "IN vs. UNDEFINED" case I don't see "other issues" equal
neither. They are definitely benifiting to "IN":
^^^^^^^^^^
(May I say that even as a person with English as foreign language myself
I am quite astonished by the overall "style" of your pos(t)ings?)
[1]
if ('someProperty' in someObject) {...}
follows the human language structure so it's easy to understand [...]

You are completely missing the point again. This feature of the operand
identifier was not debated. What was debated and is still debatable was
the extend of the support of the operand as a feature of the programming
language implementation(s).
[2]
if ('someProperty' in someObject) {...}
contains only one operation (hash LOOKUP)

No, if you had bothered to read ECMAScript 3 (as suggested to you
several times before), section 11.8.7., you would have known that
the "in" operation requires 8 steps total. If all subalgorithms
are considered, it takes even more steps:

| The production
|
| RelationalExpression :
| RelationalExpression in ShiftExpression
|
| is evaluated as follows:
|
| 1. Evaluate RelationalExpression.
| RelationalExpression :
| ShiftExpression
| RelationalExpression < ShiftExpression
| RelationalExpression > ShiftExpression
| RelationalExpression <= ShiftExpression
| RelationalExpression >= ShiftExpression
| RelationalExpression instanceof ShiftExpression
| RelationalExpression in ShiftExpression
|
| (I save you the details of that here, read for yourself)
|
| 2. Call GetValue(Result(1)).
| 1. If Type(V) is not Reference, return V.
| 2. Call GetBase(V).
| GetBase(V). Returns the base object component of the reference
| V.
| 3. If Result(2) is null, throw a ReferenceError exception.
| 4. Call the [[Get]] method of Result(2), passing GetPropertyName(V)
| for the property name.
| GetPropertyName(V). Returns the property name component of the
| reference V.
| 5. Return Result(4).
|
| 3. Evaluate ShiftExpression.
|
| ShiftExpression :
| AdditiveExpression
| ShiftExpression << AdditiveExpression
| ShiftExpression >> AdditiveExpression
| ShiftExpression >>> AdditiveExpression
|
| (again, no more details here)
|
| 4. Call GetValue(Result(3)).
| 1. If Type(V) is not Reference, return V.
| 2. Call GetBase(V).
| 3. If Result(2) is null, throw a ReferenceError exception.
| 4. Call the [[Get]] method of Result(2), passing GetPropertyName(V)
| for the property name.
| 5. Return Result(4).
|
| 5. If Result(4) is not an object, throw a TypeError exception.
| 6. Call ToString(Result(2)).
See section "9.8 ToString" for details.
|
| 7. Call the [[HasProperty]] method of Result(4) with parameter Result(6).
| 8. Return Result(7)
while
if (someObject.someProperty) {...} has two operations:
1) resolve someObject.someProperty (hash LOOKUP)
2) do background casting to false/true

Actually, it's

| 1. Evaluate MemberExpression.
| 2. Call GetValue(Result(1)).
| 3. Evaluate Expression.
| 4. Call GetValue(Result(3)).
| 5. Call ToObject(Result(2)).
See section "9.9 ToObject" for details.
| 6. Call ToString(Result(4)).
See section "9.8 ToString" for details.
| 7. Return a value of type Reference whose base object
| is Result(5) and whose property name is Result(6).

and then type-cast the return value to Boolean.
so the same for
if (typeof(someObject) == 'function')

typeof is an operator, not a function. The parentheses are unnecessary.
1) get someObject typeof
2) compare with string

That's actually

| 1. Evaluate UnaryExpression.
| 2. If Type(Result(1)) is not Reference, go to step 4.
| 3. If GetBase(Result(1)) is null, return "undefined".
| 4. Call GetValue(Result(1)).
| 5. Return a string determined by Type(Result(4)) according to the
| following table: [see "11.4.3 The typeof Operator" for more]

and then compare the return value with the string value.
On double check
if ('someProperty1' in someObject) && ('someProperty2' in
someProperty1) {...}
we save on two unnecessary operation avoided.

See above. What looks simple in the source code is not at all simple
or efficient in the implementation. This could be considered rule of
thumb since complexity does not simply vanish because of convenience.

BTW: You do know that the former throws a TypeError exception if
`someObject' does not refer to an object, too?
Do not get redicilous now. ^^^^^^^^^^
What?

You mean that 99's ECMA lacks wide support?

The specification is called ECMAScript or ECMA-262. ECMA is the
standardization body that publishes it. ECMAScript Edition 3 Final
is dated March 24, 2000.

Most features of ECMAScript 3 are implemented as (and extended by)
JScript 5.6 (IE 6), JavaScript 1.5 (Mozilla/5.0) and by the scripting
engine of newer Opera versions.
As we are talking about cross-platform scripting here then terms like
"some browsers" should be sctrictly avoided in the discussion.

Pardon? "some browsers" and interoperability is the whole point of
cross-platform scripting!
Otherwise it's just a hidden way to say "I simply don't like it/you".

You are talking nonsense again. More important, you are fighting your
own argument. If "I simply don't like you" is not what you want, then
insisting on having a not supported feature to be called best practice
is a step towards the wrong direction by 180°.
There are not "some browsers".

There are.
There is IE 3.x - 7.x, Netscape 2.x - 8.x, Firefox 1.0.x, Pan 0.x and
so on and so on.

Obviously you gave last part not enough thought.
You think that a particular method will fail in some environment?

I *know* that it will fail in some environments just because environments
are different. The probability that "in" will fail is higher than the
probability that "typeof" will fail, just because the former came last.
So tell us

That's cute of you talking the "we" although replies indicated that
your humble opinion is definitely not shared by everyone interested.
where exactly including brends and major/minor versions.

I fail to catch the meaning of this sentence. Probably you mean I have to
prove something here. Well, I don't. I did anyway on occasion here in the
hope you might learn from that.
In many cases the issue will desappear by itself just because you'll not
be willing to get funny on public by showing *what* browser you're hiding
you personal preferences behind of.

You express yourselves to the degree you appear to be able to understand the
matters discussed here.
hasOwnProperty() method deals with script objects *only*

What exactly are you calling a "script object"?
and it doesn't work for DOM interface check,

That depends on what DOM we are talking about.
which we are discussing now

Ohh, thank you very much for telling me what we were *really* discussing.
IYVHO.
(try hasOwnProperty on window).

this.hasOwnProperty('window') in global context yields `true' here
[Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20050922
Firefox/1.0.7 (Debian package 1.0.7-1) Mnenhy/0.7.2.0].
Since `window' is a property of the Global Object in almost all
HTML DOMs including the Gecko DOM, that result is correct.

The same goes for window.hasOwnProperty("document").

It does not for document.hasOwnProperty("images") and other
HTMLCollections.
IN method covers both cases.

It does, but you are still missing the point.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Michael said:
On 26/10/2005 22:33, VK wrote:
[The in operator]
IE supports it since 3.02

That's odd. IE5.0 errors out, here. Just me?

Dunno, I have no IE here. Either that or the MSDN Library is
again not as correct as it should be (nothing new, though.)


PointedEars
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 10/27/2005 3:12 PM:
VK wrote:

[Please learn how to quote, see the FAQ. Quotation levels corrected here.]

And to make your evening also take a look at the title of this thread.


Ahh, yes. Quite bold of you to give it v1.0; it's more like v0.1.

Perhaps you should go back and look at who started this thread, who
created that document, and gave it a version number. It was *not* VK
 
R

Richard Cornford

VK said:
... and related scripts.

I think that really best practice for object/method detection
should be the usage of "in" operator.

The applicable 'Best Practice' is to perform feature detection tests to
verify support for and behaviour of, features of the environment (and
suspect features of the language) prior to using them. The nature of
those tests should not be considered subject to any arbitrary rules. A
test is intended to provide a discriminating answer to a question. So
you start choosing a test by deciding what it is you want to know, and
having done that you determine which formulations of code will provide a
discriminating answer. And only in the event that there is more than one
possible test formulation do you need to even start considering which
formulation is the best to use in the applicable context.

The - in - operator has the significant problem that - in - is a late
addition to the language and produces a syntax error when not supported.
Syntax errors may result in an error dialog being shown to the user (the
biggest indicator of author incompetence possible) and they cannot be
recovered from, so they preclude controlled paths of failure. And
language features that cause syntax errors when not supported are among
the most difficult aspects of the language to apply reliable feature
detection test to.
It's nice that JavaScript converts undefined and null
into boolean false for us, but it seems a bit durty
and may hurt you when moving on strictly typed languages.

Loosely typed languages are very much in the minority, so it is much
more likely that someone would come to javascript with preconceptions
acquired from a strongly typed language rather then the other way
around. However, using a loosely typed language demands a appreciation
of the way in which type-conversion happens and hiding form
type-conversion would just be seriously tying your own hands and require
a much better justification than some vague perception of disapproval.

Knowing that an object has a property is not the same as knowing that
the value of that property type-converts to true, and knowing that the
value type-converts to true is not the same as knowing which string that
value does (or does not) produce when the - typeof - operator is applied
to it. First decide what it is that you need to know, and then decide
which text will best tell you the answer.
So instead of
if ( (someObject) && (someObject.someMethod) )

The implication of this test is that "someObject" is an existing
property name of an object on the current scope chain, that its value
type-converts to true (is an Object of some sort, or boolean true, a
non-empty string or a non-zero and non-NaN number). If the first
expression does type-convert to true then the second expression is safe,
as any of the 'true' primitive values will be type-converted to their
corresponding object (Boolean, String or Number objects) and none of
those objects has a 'someMethod' property[1], so the second expression
will evaluate to false. Only if the first expression actually is an
Object (of some sort) could the second expression evaluate to true. So
you know that - someObject - is an Object reference and that its -
someMehtod - property type-converts to true.

The only possible exception-throwing condition is when 'someObject' is
not an existing named property of some object on the scope chain. Once
that is know to be the case the following code will not error-out under
any circumstances.

[1] This assumes that the prototypes of Boolean, String and Number have
not been augmented with a 'someMethod' property, but that should be a
known condition in the environment as the script author is the one who
would be augmenting their prototypes.
we should use:
if ( (someObject in objectContainer) && (someMethod in someObject) )

Correcting the above to the much more rational:-

if ( ('someObject' in objectContainer) &&
('someMethod' in objectContainer.someObject) )

This also starts with the assumption that 'objectContainer' is a named
property of an object on the scope chain, which can never be true of a
reference to the Activation/Variable object itself. It also assumes that
that property is a reference to an object (as the ShiftExpression is not
subject to type-conversion), all primitive values will cause exceptions
to be thrown. The first - in - expression is true whenever the -
objectContainer - object (or any object on its prototype chain) has a
property with the name represented by the type-converted to string,
value of - 'someObject' -. The actual value of that property is not
tested and so could be a non-Object value, which would cause the
second - in - expression to throw an exception when evaluated.

So, you have precluded the possibility of testing local variables, and
the passing of the first - in - expression does not guarantee that the
second will successfully execute. While the restriction form the
type-converting test, that the first unqualified Identifier, refer to an
existing named property of an object on the scope chain, applies just as
much to this test.

But if this test is necessary it should be a test that will not
error-out. And so not only is the - in - test dubious because of
language support, it is dubious because it is pretty much necessarily to
be certain of values of the properties prior to verifying that they
exist, which is a bit of a contradiction. But otherwise you have no
guarantee that execution will even get to the end of test, let alone be
able to act upon the result.
As objectContainer nearly always is window, the most common
case would be:
if ( (someObject in self) && (someMethod in someObject) )
// using "self" reference to the *current* window is more reliable
// than simply "window" because "window" may be contectually
// ambiguous in some very rare but possible cases.

If that was true it would be possible to create a test case that
demonstrates the phenomenon. As the - window - and - self - properties
are non-specified but traditional properties of the window object in web
browsers they have effectively equal status and are likely to be subject
to equivalent handling in browser environments. So each would be as
reliable as the other.

Without a demonstration of the phenomenon your assertion deserves to be
dismissed as just another manifestation of your misconception of browser
scripting.

Richard.
 
R

Richard Cornford

Matt said:
It does require extensive learning, trial-and-error, and
personal experience. A reference document is not enough.
One needs to experiment with browsers and quirks in order
to build something that is useable, in many cases.

And a similar statement can be made about any other learnt skill.
Obviously. _Nothing_ is required. You could write plain
text and not learn HTML, too.

Yes, the skills you are willing to acquire should be expected to limit
what you can achieve.
The point is, sometimes some more advanced functionality is
desired, and not everyone can fulfill your 'requirements'
to be able to accomplish that functionality.

Why should a desire to do something excuse the consequences of someone
who only knows enough to do it badly doing so anyway? A desire to be a
surgeon will never be enough on its own to justify having a go at it,
and a desire to use javascript will not be enough on its own to avoid
attempting to use it from doing harm.
I believe your position makes you _highly_ biased, and I
question your ability to identify with the needs and
conditions that a more typical web developer is faced with.
You seem to be unable to grasp that not everyone is capable
of being in the position you are, either in terms
of work environment or ability to learn skills.

I don't expect everyone to be capable of being in the piston I am in,
and I don't expect the vast majority of everyone particularly wants to.
But I do believe that anyone capable of effectively functioning as a web
developer is capable of learning everything that I have learnt (and much
that I haven't) in exchange for trying.

I can see your belief that the average web developer is an intellectual
jelly would suggest otherwise.
Your solution for someone who is not like you seems to be
to become more like you.

My "solution"? Solution to what exactly? If someone expresses a desire
to understand javascript I am likely to propose that they learn
javascript. It is not really a solution, as it is really too obvious.
Which is highly unrealistic, and perhaps not even desireable.

If you were an average web developer

I am an average web developer.
without much experience,

And as I am older than the web there inevitably was a time when I had no
experience of it.
put into a position where you needed to develop some
javascript functionality,

How exactly do you imagine that I started writing javascript?
I think you would be like a deer in headlights.
You wouldn't know what to do.

Throughout my time programming there has been a continuous sequence of
new challenges. My experience is that the best way of handling these has
been to put some effort into understanding the pertinent subject, which
is why I will continue to recommend that strategy to others.
Your only recommendation would be to spend weeks or months
learning the skills required to implement things from
scratch, which is a naive and completely impractical suggestion
for most.

So your would imagine that I learnt JSP by writing a Java Application
server?
You missed the point entirely.
Many people learn and practice web development and javascript
outside of a work environment.

And if they never clam to be capable of being professional web
developers it doesn't really matter (and isn't particularly surprising)
if they are not very good at. They are also not going to be in a
position to allow their lack of skills to do harm to others.
The idea of spending many hours learning a
programming language is not at all practical.

Was that the "point" I missed? If the idea of spending many hours
learning a programming language is not practical then the fact that it
takes many hours learning any programming language would man that there
are no programmers.

Sometimes they don't, no.

This would be your "average web developer" again?

I realize this.
But if you have solved the problem of, for example, finding
the position or size of an object in a way that works in
as many browsers as possible and as many specific cases as
possible, ...

A large part of the point of the exercise is not to address as many
specific cases as possible, but instead to only implement the component
versions for the specific cases actually encountered, as and when the
are encountered. Avoiding writing specific code until it is needed, and
still creating easily re-useable code, is one of the characteristics
that makes the strategy efficient.

This prerequisite of yours has already been shown to be
unrealistic and impractical for many people.

I don't know about 'shown', we only have your word for it that the
average web developer is intellectually incapable.
And you know the term used for such people? Elitist assholes.
<snip>

It is ironic that you should describe others as "Elitist assholes" when
your characterisation of "the average web developer" as incapable of
achieving even your level of understanding of the subject carries the
implication that you consider yourself their innate superior.

On the other hand, your attitude toward the "average web developer" may
explain why you have never yet posted a single technical explanation of
anything to this group.


Richard.
 
M

Matt Kruse

Richard said:
If someone expresses a desire to understand javascript I am
likely to propose that they learn javascript.

The point you missed: Not everyone wants to understand javascript. Some just
want to take advantage of its features by implementing packaged solutions.
If the idea of spending many hours
learning a programming language is not practical then the fact that it
takes many hours learning any programming language would man that
there are no programmers.

Your statement is as ridiculous as saying that if it weren't practical for
people to learn to repair their own cars, we wouldn't have auto mechanics.
It's not, in fact, practical for many people. I didn't say it was not
practical for anyone.
A large part of the point of the exercise is not to address as many
specific cases as possible, but instead to only implement the
component versions for the specific cases actually encountered, as
and when the are encountered. Avoiding writing specific code until
it is needed, and still creating easily re-useable code, is one of
the characteristics that makes the strategy efficient.

I disagree.

You are solving the specific case each time a new one comes up. I find it to
be more challenging, rewarding, and beneficial to try to solve the general
case (as much as possible) and then apply it to the specific case at hand.

If someone asks, "how do I find the position of my object on the page?" and
you supply the simplest solution which will work on their example page, you
may not be helping as much as you could. Tomorrow, if they introduce
scrollable areas into their page, change their layout with CSS, or put some
browsers into standards mode, your solution may break. And the person will
once again have no idea to fix it, because you gave them a very
limited-scope solution which can't be applied in varied situations.

Instead, you can solve the general case which also happens to solve their
specific example. Then, when their page changes, they don't need to change
their code, because the general solution already considers it correctly. The
few extra k of code which is not actually executed in each case is easily
offset by the fact that you don't need to go back and re-visit the code to
solve the new specific case, or change the code used every time your layout
changes.

The obvious fact to me is that most people prefer the approach taken by me
and many others. You may not, and that's fine, because obviously you have
the time and talent to do things your own way. But I see many people every
day downloading and using my solutions and thanking me for them. I don't see
you converting anyone to your approach with your postings on this group or
your web site examples. I do hear _lots_ of people say "wow, I found this
packaged solution, implemented it in 10 minutes, and got exactly what I
wanted". I don't hear anyone saying, "Thank god I didn't implement a
packaged solution, but instead spent the last 3 months learning and
experimenting with javascript so I could build up my own low-level function
library and solve the specific requirement that I had 3 months ago!"

Compare it to some every-day examples:

When you buy a new TV, do you learn electronics from scratch and build your
own to only have the features you want? Or do you buy one that costs a
little more and has more than you need, but gives you a TV right away and
gives you options in the future?

When you want a new computer, do you research and buy all the parts
separately and spend time assembling them to get the maximum possible
performance? Or you do buy a pre-packaged computer that may not match your
needs exactly but is certainly more convenient than building your own from
scratch?

When you choose an OS, do you build it from scratch and make it consist of
the fewest possible features that you need to use, in order to optimize
performance? Or do you use a packaged solution with lots more functionality
than you'll ever need, which uses more disk space and CPU, but makes your
life easier and more convenient?

Using a pre-packaged, generalized solution to a given problem is not lazy or
a cop-out as you seem to suggest. It's a realistic approach to take when you
consider all the factors involved and decide that building your own from
scratch just isn't advantageous. Why you object to it as a concept continues
to baffle me. I can only assume that you either have no understanding of the
real problems and priorities faced by many developers in the world, or that
you must dismiss generalized solutions in order to justify your own work to
yourself.
It is ironic that you should describe others as "Elitist assholes"
when your characterisation of "the average web developer" as
incapable of achieving even your level of understanding of the
subject carries the implication that you consider yourself their
innate superior.

Incorrect.

Many people can achieve the level of understanding that I, you, or anyone
else has. I understand that many people don't _want_ to, and many people
don't have _time_ to, and many people have higher priorities. I'm not
superior, and they aren't stupid. They just aren't focused on the same
things as I am, and I see no reason to tell them that they need to be (as
you do).
 
T

Thomas 'PointedEars' Lahn

Matt said:
The obvious fact to me is that most people prefer the approach taken by me
and many others. You may not, and that's fine, because obviously you have
the time and talent to do things your own way. But I see many people every
day downloading and using my solutions and thanking me for them. I don't
see you converting anyone to your approach with your postings on this
group or your web site examples. I do hear _lots_ of people say "wow, I
found this packaged solution, implemented it in 10 minutes, and got
exactly what I wanted". I don't hear anyone saying, "Thank god I didn't
implement a packaged solution, but instead spent the last 3 months
learning and experimenting with javascript so I could build up my own
low-level function library and solve the specific requirement that I had 3
months ago!"

Giving people what they want is not always the best approach. Most people
do not know what they really want. One has to consider what they probably
*really* *need*.
Compare it to some every-day examples:

When you buy a new TV, do you learn electronics from scratch and build
your own to only have the features you want? Or do you buy one that costs
a little more and has more than you need, but gives you a TV right away
and gives you options in the future?

No, because the effort necessary on my part would not justify the result
to be expected. This is of course natural laziness in some way intrinsic
to the psyche of all living beings. What makes them thrive and evolve is
need. What throws them back again is the urge to satisfy that need with
the least effort possible.

If you deny that you have not looked into it deep enough and you deny
yourself. Because you (and I) then would not have build libraries but
write that code from scratch every time. But, may I say, both of us are
professionals who wrote the code ourselves, who have come to understand
the basics needed for that. Others are in most cases merely consumers
of that code which puts them into a completely different position. They
have to be guided on how to use that work of ours properly which includes,
but not ends with, proper documentation.
When you want a new computer, do you research and buy all the parts
separately and spend time assembling them to get the maximum possible
performance?

Actually, I did, about four years ago. And I have not regretted it
to date, even though I have had some hardware and software upgrades.
Or you do buy a pre-packaged computer that may not match your
needs exactly but is certainly more convenient than building your own from
scratch?

The convenience of a pre-packaged solution is almost always balanced by
the problems that arise later because it does not fit special needs.

For example, pre-packaged solutions often contain bulk or low-performance
components to make the solution cheaper and easier to use. The components
are certainly able to do their basic tasks, but when it comes to getting
more of the average out of them they not only inevitably fail to do so but
cause problems when trying to enhance the particular performance level.
Think about a on-board graphics card often part of such hardware solutions
which is not good enough for your brand-new 3D game but interferes with the
new graphics card you just bought to counteract that, even though the
former is disabled in the BIOS Setup.
When you choose an OS, do you build it from scratch and make it consist of
the fewest possible features that you need to use, in order to optimize
performance? Or do you use a packaged solution with lots more
functionality than you'll ever need, which uses more disk space and CPU,
but makes your life easier and more convenient?

I use GNU/Linux, where I have downloaded the Debian distribution from
<http://debian.org/> and the Linux source from <http://kernel.org/>.
I compile my kernels to be suited to my computer (aided by sophisticated
configuration menu and make scripts), because not only their overall
performance then is better than the pre-packaged solution (which exists
anyway) but it also saves me trouble disabling things (kernel options,
including modules) I seldom or never want or need but which will
potentially and are known to interfere with the things I do need.
That does not mean I have to build the OS from scratch.

On the other hand, I use Debian packages for GNU whenever possible and
viable (up-to-date) because I have come to love the apt-get tools and
their efficient child's-play way to maintain the rest of the operating
system.

It's a not a black-and-white universe but one with many colors and shades.
Using a pre-packaged, generalized solution to a given problem is not lazy
or a cop-out as you seem to suggest. It's a realistic approach to take
when you consider all the factors involved and decide that building your
own from scratch just isn't advantageous.

As I said, the advantage is easy implementation which is why users of
pre-packages solutions are almost always very thankful for them.

The disadvantage in that is that if you have not bothered to learn to
understand how the implementation works, you will definitely have a hard
time or even be unable to apply it to your specific needs later. This
makes you completely dependent on the author of the solution -- whom you
will definitely not thank for and almost never contact them about it --
and on his will to fix or extend it if it does not work as supposed
-- for which you will rather wait because you have become so accustomed
to being fed that you will seldom consider the alternatives. It is the
way how Closed Source software works, and it's definitely a Bad Thing,
at least in the mid-term.

This is of course a matter of (personal) philosophy. Mine and it seems
also Richard's is:

"Give a man a fish; you have fed him for today.
Teach a man to fish; and you have fed him for a lifetime."


PointedEars
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 10/29/2005 6:41 AM:
Matt Kruse wrote:

This is of course a matter of (personal) philosophy. Mine and it seems
also Richard's is:

"Give a man a fish; you have fed him for today.
Teach a man to fish; and you have fed him for a lifetime."

True but Richards, at times, seems to expand to:
"Give a man a fish; you have fed him for today.
Make a man a commercial fisherman; and you have fed him for a lifetime."
 
M

Matt Kruse

Thomas said:
Giving people what they want is not always the best approach. Most
people do not know what they really want. One has to consider what
they probably *really* *need*.

In some cases I agree, and in other cases I think that is an elitist
knot-it-all attitude. It depends.
The convenience of a pre-packaged solution is almost always balanced
by the problems that arise later because it does not fit special
needs.

Yet for most average 'consumers', the pre-packaged solution works just fine.
And in the few cases where it doesn't, they don't mind.

Most people are not 'power users' of a given technology or device. The
'typical' version works fine. Most people don't tweak their OS, customize
their web browsers, choose hardware piece by piece, mod their xbox, etc,
etc. Yet most people who are 'experts' (especially when it comes to
computers) can't quite comprehend this. They think everyone should learn and
tweak like they do, to get the most of everything. This is also why computer
nerds get made fun of so often ;)

With javascript development, the 'consumer' isn't just the end user viewing
the results in their web browser. The 'consumer' of code is also thousands
of average people who may not have much (or any) programming experience, but
who are working on a web site or a web app for their own use, and want to
put some advanced functionality into it.

If they were required to start from scratch and learn programming concepts
and then the javascript language and then the quirks of a web browser
scripting environment, many probably would never touch it. But if you can
give them a solution to plug in, it will solve their problem now and perhaps
give them insight into how it works. They can examine the code and read more
about it if they wish, and the whole idea of javascript programming won't
seem so intimidating.
As I said, the advantage is easy implementation which is why users of
pre-packages solutions are almost always very thankful for them.
The disadvantage in that is that if you have not bothered to learn to
understand how the implementation works, you will definitely have a
hard time or even be unable to apply it to your specific needs later.

This is indeed the trade-off, and I've never denied that such a trade-off
exists.

Pre-packaged solutions can work great for the exact cases which they cover.
When a more customized solution is required, the user may lack the ability
to make the changes they need, and be lost. Or, perhaps the user *is*
knowledgeable enough to customize the code, then they have their needs
mostly met, with only minor additional coding needed. This is the trade-off
with *any* packaged solution. You get convenience, but at the expense of a
solution tailored to your specific needs.

As a consumer, I sometimes prefer the pre-packaged solution, and I sometimes
prefer to build things myself.
But to deny the benefit of pre-packaged solutions entirely, as Richard and
others often do, is extremely biased and naive. When a person advocates a
single viewpoint and refuses to acknowledge that other viewpoints might have
some credibility, it makes them less believable, imo.
This is of course a matter of (personal) philosophy. Mine and it
seems also Richard's is:
"Give a man a fish; you have fed him for today.
Teach a man to fish; and you have fed him for a lifetime."

Using analogies usually takes a discussion off-course, but I'll bite ;)

1) If someone is starving, teaching them to fish isn't helping. They need
fish now. They might also learn to fish on their own, which will take time,
but for right now, they need a fish!

2) Someone need not learn how to fish if they know how to go to the store
and buy fish every time they need some. We can't all learn to fish!

Plus, I don't even like fish ;)
 
M

Matt Kruse

Randy said:
True but Richards, at times, seems to expand to:
"Give a man a fish; you have fed him for today.
Make a man a commercial fisherman; and you have fed him for a
lifetime."

Or how about,

"If you give a man a fish, you have catered to a lazy person who can't even
spend the time required to learn how to get their own fish. You've also
given them bones and other parts that they probably don't want to eat.
Furthermore, you haven't informed them of the potential harm that eating
fish could do to them. What if they are pregnant and they get too much
mercury? To give the person a fish without them understanding all of the
potential hazards is irresponsible. Therefore, fish should never be given to
anyone.
Instead, anyone in the world who wants fish should become a fisherman. There
is plenty of information out there about how to fish. It just takes
practice. And a boat. And a net. And lots of free time. If you don't have
all that, you don't deserve to eat fish anyway. But if you really want fish,
then you need to buy those things and practice and build up your skills so
that some day you can catch the exact kind of fish you want to eat. And by
the way, if anyone else asks you for a fish that you caught, tell them hell
no. Get their own damn fish."

:)
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 29 Oct 2005
11:05:16, seen in Matt Kruse
"If you give a man a fish, you have catered to a lazy person who can't even
spend the time required to learn how to get their own fish. You've also
given them bones and other parts that they probably don't want to eat.
Furthermore, you haven't informed them of the potential harm that eating
fish could do to them. What if they are pregnant and they get too much
mercury?
... "

IMHO, very few men worry about the possibility of being themselves in
the state described in the last quoted sentence.
 
M

Matt Silberstein

Thomas 'PointedEars' Lahn said the following on 10/29/2005 6:41 AM:




True but Richards, at times, seems to expand to:
"Give a man a fish; you have fed him for today.
Make a man a commercial fisherman; and you have fed him for a lifetime."

Teach a village to fish and they soon depopulate the lake.

Teach a man to cook a fish and he starves to death with a greater
appreciation of what he is missing.

--
Matt Silberstein

Do something today about the Darfur Genocide

http://www.beawitness.org
http://www.darfurgenocide.org
http://www.savedarfur.org

"Darfur: A Genocide We can Stop"
 
T

Thomas 'PointedEars' Lahn

Matt said:
Yet for most average 'consumers', the pre-packaged solution works just
fine. And in the few cases where it doesn't, they don't mind.

Well, as I wrote, due to the nature of people, you cannot know neither for
sure. Not that they are few cases and not that people don't mind. For
example, I have heard too many people complaining about MS Windows: that
it is too slow for them, takes too much system resources for simple tasks,
too whatever; yet the same people in almost all cases refuse to put some
effort into just exploring alternatives, let alone using them. And when
I ask them about it, still nobody ever considered sending those complains
to MS to make the OS better; they rather wait for it to happen.
Most people are not 'power users' of a given technology or device. The
'typical' version works fine.

Not in all, most or even many cases, which is the problem.
Most people don't tweak their OS, customize their web browsers, choose
hardware piece by piece, mod their xbox, etc, etc.

Which does not mean in any way that they are happy with what they have.
It does not mean the opposite either, of course.
Yet most people who are 'experts' (especially when it comes to
computers) can't quite comprehend this. They think everyone should learn
and tweak like they do, to get the most of everything. This is also why
computer nerds get made fun of so often ;)

Public opinion is seldom based on rational thinking.
With javascript development, the 'consumer' isn't just the end user
viewing the results in their web browser. The 'consumer' of code is also
thousands of average people who may not have much (or any) programming
experience, but who are working on a web site or a web app for their own
use, and want to put some advanced functionality into it.

That's the point. When I write `consumer' I mean the first-level consumer,
that is, the Web site responsible. And the Web site is for consumers, too,
even if only non-commercial visitors. However, if your customer/consumer
does not really know what he does, how can you expect that he will provide
a viable service to his customers/consumers? He depends entirely on you,
so he makes you responsible if something goes wrong, whether you deny that
in a legal statement or not.

I appears to be a Good Thing to shift responsibilities by allowing the
first-level customer to learn. I don't know whether that is a viable
approach, but why not provide a solution of components instead of a
complete solution? This would create the need for that customer to
understand how to make the components work together to achieve what
he wants.
If they were required to start from scratch and learn programming concepts
and then the javascript language and then the quirks of a web browser
scripting environment, many probably would never touch it.

To completely refuse learning before doing, would that not be a sure sign
of incompetence? Would they not deserve to have a not-so-good Web site,
if any? Ahh -- but there's the commercial aspect which brings us back to
the least-effort behavioral pattern. Why put effort in learning if it
*appears* that you can buy (or copy) knowledge?
But if you can give them a solution to plug in, it will solve their
problem now and perhaps give them insight into how it works. They can
examine the code and read more about it if they wish, and the whole
idea of javascript programming won't seem so intimidating.

Will they ever bother to learn if you keep feeding them?
But to deny the benefit of pre-packaged solutions entirely, as Richard and
others often do, is extremely biased and naive. When a person advocates a
single viewpoint and refuses to acknowledge that other viewpoints might
have some credibility, it makes them less believable, imo.

It of course depends on the matter discussed, but I tend to agree.
Using analogies usually takes a discussion off-course, but I'll bite ;)

I'm a hell of a good "fisherman", I suppose? ;-)
1) If someone is starving, teaching them to fish isn't helping. They need
fish now. They might also learn to fish on their own, which will take
time, but for right now, they need a fish!

I doubt that they really "starving". But to speculate on people's motives
won't bring us further in this discussion.
2) Someone need not learn how to fish if they know how to go to the store
and buy fish every time they need some. We can't all learn to fish!

But perhaps to make "fish soup".
Plus, I don't even like fish ;)

Me neither, I'm vegetarian. Yet I happen to like the proverb.
You may replace "to fish" with "to grow crops" or so ;-)


PointedEars
 
V

VK

I was following this discussion and wondering: what in the name these
fisherman's talks have to do with the announced topic? My wild guess
would be that it is somehow related with the question "What of our
godlike knowledge should we give to mortal people and what is suitable
to know between gods only?". If I'm right here then I engage you to
leave the Olympus right now as you get there by the wrong ticket.

comp.lang.javascript is a good newsgroup but not only one source of
knowledge of JavaScript - and it's definitely *not* the ultimate source
of knowledge of common programming principles. Anyone can get the
answer from here or from any other place of her choice.

If you decided to give some "Best Practice" programming advises in
relation to ECMAScript-based languages then let's discuss it. If anyone
still remembers these are:

1. Square Bracket Notation
2. Referencing Forms And Form Elements
3. Referencing Forms From Element Handlers
4. Problems With Concatenation
5. Using onClick in <A> tags
6. Eval
7. Detecting Browser Versions
8. Don't Use document.all
9. Getting And Setting Form Values

I see nothing where what wouldn't be covered already in FAQ's but OK -
it could be a nice FAQ add-on. If the discussion is set over, OP is
welcome set a link on it from the FAQ page.

Most importantly you need to decide what is the "Best Paractice"? Is
the code that follows the "Best Practice" i) most readable or ii) most
universal or iii) most error-protected or iv) most resource effective
or v) most time effective.

These are all different and often mutually-exceptive issues. I say that
the best practice has to lead to code which is the most universal and
(upon the possibility priority level 1) most error-protected and (upon
the possibility priority level 2) most readable.
From this position say "Eval is Evil" is totally right and nothing to
discuss here.
But let's us take "iv) most resource effective or v) most time
effective" priorities, and "Eval is Evil" becomes a good sounding but
pointless slogan. With the above mentioned priorities we should say
instead "Recursion has a Curse on". JavaScript is an interpreted
language and you have to accept this fact as say its loose typing. As
one of consequences of it recursions in JavaScript are almost never as
effective as runtime code generation. I have a set of *practical*
samples (starting from full array copy *including* nested arrays) to
demonstrate it. Actually better you may try to make a non-"new
Function()" based solution which would *be no more than two times
slower* than a "new Function()" based.

But again - it's all priorities' based. So the parting question would
be: what is the "Best Practice" definition in this thread?



P.S. And please stop profanities like "reusable libraries should not be
used in the programming". There may be children around here to hear
that.
 
M

Matt Kruse

Thomas said:
For example, I have heard too many people complaining
about MS Windows: that it is too slow for them, takes too much system
resources for simple tasks, too whatever; yet the same people in
almost all cases refuse to put some effort into just exploring
alternatives, let alone using them. And when
I ask them about it, still nobody ever considered sending those
complains to MS to make the OS better; they rather wait for it to
happen.

Which illustrates my point: That people value the convenience of a
pre-packed solution far more than the value of a better solution which
requires additional effort. And even when better alternatives are available,
people will _still_ stick with the packaged solutions. Which means, no
matter how hard Richard or you or anyone else promotes the idea that
packaged javascript solutions are bad, it won't matter. People will still
search for them and use them. You can either let them all use low-quality
offerings, or write your own solutions and aspire to higher quality so that
you can benefit a wide range of people.
why not provide a solution of components instead of a
complete solution? This would create the need for that customer to
understand how to make the components work together to achieve what
he wants.

This is exactly the approach I would like to take. I would like to build a
repository of components which solve very specific problems, but which by
themselves offer no functionality. I would love to get the best programmers
on this group to combine their efforts into a standard repository of these
components. Then anyone building sites or apps or even packaged solutions
can reuse these solid low-level components and achieve higher quality.

Given a standard repository of components, I'm certain that a number of
developers who now choose packaged solutions would instead decide to build
their own. Because despite what some might argue, some of these low-level
components are not, in fact, trivial to develop and test. Developing
cross-browser scripts even for some simple things requires digging into
different DOM implementations, testing in a wide variety of browsers,
reading through different documentation, and dealing with quirks in browsers
where things don't work exactly as they ought to.

When I promote something like that, Richard replies with, "we won't help
you, because we know what your goal is, and your goal sucks." That's what I
call the 'elitist asshole' attitude. And when he promotes his own
methodology and publishes examples using his own low-level components, yet
refuses to make them available to others to benefit from, or even document
his approach, I have to wonder what he's really thinking...
To completely refuse learning before doing, would that not be a sure
sign of incompetence? Would they not deserve to have a not-so-good
Web site, if any? Ahh -- but there's the commercial aspect which
brings us back to the least-effort behavioral pattern. Why put
effort in learning if it *appears* that you can buy (or copy)
knowledge?

Let's not deny that every one of us benefits from buying, copying, or using
the packaged knowledge of others. I don't think that the viewpoint that we
must all learn to do everything ourselves and not benefit by using the
solutions of others has any merit at all. The only way to achieve greater
and more complex goals is to treat some knowledge as a 'black box' and add
your own knowledge to go one step further.

Clearly, if someone's goal is to be javascript programmer or an expert web
developer, then relying only on packaged javascript solutions is not a good
plan. They should learn the skills required to develop the code on their
own.

But if their goal is to have a decent web site where their small church can
make information available (for example), then understanding the underlying
technologies to their fullest is _not_ something they care about. They can
achieve their goal by either paying someone who understands everything
(often not an option) or they can use the knowledge of others without fully
understanding it, so they can achieve their goal. To say that they only
deserve a not-so-good website, if any, is an elitist response that I find
incredibly rude. If they can achieve decent results and accomplish their
goal and benefit from it - even if they didn't learn to do it themselves -
then I say good for them. I certainly wouldn't tell them that they don't
deserve it.
 
R

Richard Cornford

Randy Webb said:
Thomas 'PointedEars' Lahn said the following on 10/29/2005 6:41 AM:

True but Richards, at times, seems to expand to:
"Give a man a fish; you have fed him for today. Make a man a
commercial fisherman; and you have fed him for a lifetime."

That might be it, or I may be pointing out that there would be a
difference between teaching someone to fish with a hook and line, a net
and with dynamite.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top