RFD: How To Recognize Bad Javascript Code

  • Thread starter Jeremy J Starcher
  • Start date
D

Dr J R Stockton

In comp.lang.javascript message <8d803126-36b2-4752-8353-499dfd387e01@e6
g2000prf.googlegroups.com>, Wed, 2 Jan 2008 16:58:43, David Mark
Absolutely. I typically add copyright and version information at the
top after minification.


But then you have no guarantee of consistent correctness in matching the
minified version with the correct original, unless the process is
adequately automated. Anything a human can do, he/she can occasionally
do wrong. At least an automated process can be consistently or
repeatably wrong. - like this spelling-checker, which rejects
"repeatably" - repeatably.
 
D

Dr J R Stockton

In comp.lang.javascript message <cff62c4b-e5fc-4882-90f4-ab0242cc186a@i3
g2000hsf.googlegroups.com>, Wed, 2 Jan 2008 20:53:30, David Mark
Similarly, any hits for "userAgent" indicate that the code can be
dismissed out of hand.


Not necessarily. What's wrong with a page, originated by programmer A
but nowadays maintained by good programmer B, that contains

// Code replaced, B, yyyy-mm-dd : was
/* ... userAgent ... */
<feature-testing code>


For a really bad sign, consider current code that, with D = new Date(),
Y2k-corrects getYear() then uses that in conjunction with getUTCMonth()
... getUTCSeconds() !
 
A

Anthony Levensalor

-Lost said:

If for example I discuss that a woman I once knew got turned on by
talking about code, programming, and hence JavaScript -- would sexual
geek fetishes be on-topic here?


Well, I mean, are they ever NOT on topic? ;)

~A!
 
D

David Mark

In comp.lang.javascript message <8d803126-36b2-4752-8353-499dfd387e01@e6
g2000prf.googlegroups.com>, Wed, 2 Jan 2008 16:58:43, David Mark
<[email protected]> posted:







But then you have no guarantee of consistent correctness in matching the
minified version with the correct original, unless the process is
adequately automated.  Anything a human can do, he/she can occasionally

I use a batch file to call the minifier and then tack on the
copyright, etc.
 
D

David Mark

In comp.lang.javascript message <cff62c4b-e5fc-4882-90f4-ab0242cc186a@i3
g2000hsf.googlegroups.com>, Wed, 2 Jan 2008 20:53:30, David Mark
<[email protected]> posted:







Not necessarily.  What's wrong with a page, originated by programmer A
but nowadays maintained by good programmer B, that contains

        // Code replaced, B, yyyy-mm-dd : was
        /* ... userAgent ... */
        <feature-testing code>

Fair enough. Finding it in a comment is okay. Finding it parsed by
code and used in branches indicates supreme incompetence. It is
amazing to me that virtually the entire Web (and especially "Web 2.0")
runs on code that treats the presence of "msie" in the userAgent
string as meaningful information.
 
T

Tim Streater

-Lost said:
That sounds like a clever attempt at semantics and the production of
off-topic replies.

If for example I discuss that a woman I once knew got turned on by
talking about code, programming, and hence JavaScript -- would sexual
geek fetishes be on-topic here?


That sounds like a clever attempt at semantics and the production of
off-topic replies.
 
E

Eric B. Bednarz

Thomas 'PointedEars' Lahn said:
Eric B. Bednarz wrote:

There is no requirement for a hyphen in compound words in English.

You are merely splitting hairs

Not quite, I’m merely making fun of those who fancy nitpicking.
As a rule of thumb, I’d say that every correction spawns at least two
new errors, everything I write myself included.
while I was pointing out the use of wrong
technical terms -- a slight, but important difference.

Than it would be better to use the correct ones, which can be found in
the mentioned ISO standard; opinions (we are not talking French here)
about ‘English’ hyphenation conventions are covered in chapter 7 of
The Chicago Manual of Style, 15th edition (and “close†isn’t even close,
really :).
 
J

Jeremy J Starcher

(Request for Discussion)

I've put together a guide that I hope will help novice coders avoid the
same hair pulling that I went through.

<URL: http://www.mopedepot.com/jjs/HowToRecognizeBadJavascriptCode.html >

Version 0.3 is up.

Once again, I thank all of you for your comments, suggestions and
encouragement.

I know that I did not include a lot of things that were mentioned. While
most, if not all, were markers of "not great" code, I had to remain on
task and on focus. I believe that I have distilled enough
easy-to-digest-points for the complete novice to follow.

If any of the powers that be would wish to move this document, once
finalized, into a larger collection of recognized Javascript writings I
would be open to the idea. We can discuss it here or in email.
 
J

Jeremy J Starcher

Jeremy J Starcher said the following on 1/7/2008 4:58 AM:Most of these, if not all, are grammar and typo related:

Bad fingers! Bad!

The joys of being dyslexic. Normally, one of my friends is willing to
proofread things for me and catch all of that stuff before it hits the
world, but she won't proofread tech documents.

I even did the old "get away from it for a few days" trick to see if I
could spot anything I didn't catch last time through. Hopefully, edits
should be just about over.

(Mentally, I know there is a difference between "depreciated" and
"deprecated" but I don't *see* it, if that makes any sense.)

Thanks for the patience.
 
R

RobG

Bad fingers! Bad!

The joys of being dyslexic. Normally, one of my friends is willing to
proofread things for me and catch all of that stuff before it hits the
world, but she won't proofread tech documents.

You can also try:

1. Copy and paste into a word processeor and do a spelling and grammar
check

2. Use text-to-speech and listen to it read back to you (it can
usually be set to run off hotkeys)
I even did the old "get away from it for a few days" trick to see if I
could spot anything I didn't catch last time through.

A useful trick, but the law of diminishing returns still applies - it
becomes less effective the more you do it on a particular document.

Hopefully, edits should be just about over.

It will never end. :)

(Mentally, I know there is a difference between "depreciated" and
"deprecated" but I don't *see* it, if that makes any sense.)

Hence find and replace...
 
D

David Mark

Version 0.3 is up.

You might want to add a note to avoid anything that is written on top
of Prototype.js (or jQuery of course.)

I recently stumbled onto this prototypical (no pun intended) example
of bad code:

http://dev.rubyonrails.org/changeset/7056

Why would anybody write script like this, knowing they will have to
rewrite it constantly, just to keep up with four browsers? Perhaps
they just like monkeying around with code. Obviously these million
monkeys are never going to write Hamlet.

Their repository is full of gems like this. Their various blogs and
mailing lists are full of frantic bickering over the latest exposed
misunderstandings. Typically they cover them with browser sniffing
patches that most production sites don't see until after their
relative effectiveness has expired. In the meantime, exceptions like
"Event.observer is not an object" pop up everywhere.

Any programmer who builds on top of such jello must be out of their
minds. The fact that Prototype and the like are in widespread use
would seem to indicate some sort of mass hysteria among Web developers.
 
D

Dr J R Stockton

In comp.lang.javascript message said:
document.write("<p>This page last updated:" + document.lastModified) +
"<\/p>");

Extra ) after lastModified.

One should add that the result will look somewhat silly in Safari 3.0.4,
the current Windows beta (empty string); and that lastModified is a
misnomer because in practice it means "last uploaded to the server".

The only way to get a date really useful to the normal reader, who will
care only about whether there are significant content changes, is to
type it in to the HTML when a significant change is made; and to do so
in an internationally-standard format. Eschew FFF.
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] Randy Webb [...] posted:
document.write("<p>This page last updated:" + document.lastModified) +
"<\/p>");

Extra ) after lastModified.

One should add that the result will look somewhat silly in Safari 3.0.4,
the current Windows beta (empty string);

Correct. But maybe that is because it is only a beta.
and that lastModified is a misnomer because in practice it means "last
uploaded to the server".

Incorrect. The `lastModified' property shows the modification time of the
resource, essentially the value of the `Last-Modified' HTTP header. The
upload time to the server does not enter into it, although the modification
timestamp of a file is usually also set/updated when it is created or
overwritten in the filesystem as it would happen during an upload.
The only way to get a date really useful to the normal reader, who will
care only about whether there are significant content changes, is to type
it in to the HTML when a significant change is made; [...]

Fortunately, you are wrong here as well, and programming languages that are
used server-side make this a lot easier. For example, I use essentially

<?php
$modi = @filemtime(__FILE__);
echo gmdate('Y-m-d\TH:i:s+00:00', $modi);
?>

in said:
Eschew FFF.

JFTR: "FFF" probably means "Fred Flintstone Format" here, the mm/dd/yyyy
date format as it is used in Canada, the Federated States of Micronesia, the
Republic of Palau, the Republic of the Philippines, and in the United States
of America.

http://acronyms.thefreedictionary.com/FFF

(CMIIW)


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message said:
Dr J R Stockton said the following on 1/8/2008 7:47 AM:

It is not a misnomer. It reflects the date the document you are viewing
was last modified. It has nothing to do with "uploading". The FAQ
index.html, while I have been editing the FAQ, has *never* been
uploaded. The date that document.lastModified gives is exactly that,
the date that the document was last Modified. Whether that modification
is by uploading a replacement, direct editing on the server, or by
being generated on the server.


Your lack of insight is most impressive.

When a document is, for whatever reason, re-uploaded to a server
(perhaps because of a change of server, for instance), it gets a new
Last-Modified header. But if the content of the document, as seen by
the user, is unchanged, it is a disservice to assert that there has been
a modification. It is also a disservice if the change is too slight to
matter to the reader, for example correcting non-misleading typos.[*]

Please remember what WSC is alleged to have said about Americans (and if
possible provide an authoritative reference); in this respect, the task
is as yet incomplete.



ASIDE : I now have an improved Leading Zero function, LZ; it is the
fastest (in IE6) of three which all, AFAICS, always give the right
answer. See <js-tests.htm#LZ>.

function LZ(x) { return (x!=null&&x<10&&x>=0?"0":"") + x }


[*] I have a nice story about typos and random numbers ...
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>, Wed,
9 Jan 2008 00:37:40 said:
Dr said:
[...] Randy Webb [...] posted:
document.write("<p>This page last updated:" + document.lastModified) +
"<\/p>");
One should add that the result will look somewhat silly in Safari 3.0.4,
the current Windows beta (empty string);

Correct. But maybe that is because it is only a beta.

The reason is like Mr Worthing's early location. The fact remains that
the string is empty in Sf 3.0.4.
Incorrect. The `lastModified' property shows the modification time of the
resource, essentially the value of the `Last-Modified' HTTP header. The
upload time to the server does not enter into it, although the modification
timestamp of a file is usually also set/updated when it is created or
overwritten in the filesystem as it would happen during an upload.

You are once more revealing your background as an unintelligent product
of a technical college; one who can regurgitate material but cannot
think effectively. In many respects, you and Randy are equals.
The only way to get a date really useful to the normal reader, who will
care only about whether there are significant content changes, is to type
it in to the HTML when a significant change is made; [...]

Fortunately, you are wrong here as well, and programming languages that are
used server-side make this a lot easier. For example, I use essentially

<?php
$modi = @filemtime(__FILE__);
echo gmdate('Y-m-d\TH:i:s+00:00', $modi);
?>

The datestamp of a file is not necessarily the date of the last
modification to its contents; and that is not necessarily the last
modification that the page's reader will need to know about.

To consider the datestamp of any file to be *necessarily* important to
the average user of the file is naive.
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] Thomas 'PointedEars' Lahn [...] posted:
Dr said:
[...] Randy Webb [...] posted:
document.write("<p>This page last updated:" + document.lastModified) +
"<\/p>");
One should add that the result will look somewhat silly in Safari 3.0.4,
the current Windows beta (empty string);
Correct. But maybe that is because it is only a beta.

The reason is like Mr Worthing's early location.

Sorry, I don't understand.
The fact remains that the string is empty in Sf 3.0.4.

It is empty in the _beta_ version. Don't you think that this could *maybe*
be a bug that is fixed for the release?
You are once more revealing your background as an unintelligent product
of a technical college; one who can regurgitate material but cannot
think effectively. In many respects, you and Randy are equals.

You have stated that the `lastModified' property value would mean the date
of the last upload to the server, which is simply wrong. It would look
better on your record if you had the courage to admit your error that
instead of hiding behind your inept attempts at ad hominem attacks. But I
guess that is too much to ask for.
The only way to get a date really useful to the normal reader, who will
care only about whether there are significant content changes, is to type
it in to the HTML when a significant change is made; [...]
Fortunately, you are wrong here as well, and programming languages that are
used server-side make this a lot easier. For example, I use essentially

<?php
$modi = @filemtime(__FILE__);
echo gmdate('Y-m-d\TH:i:s+00:00', $modi);
?>

The datestamp of a file is not necessarily the date of the last
modification to its contents; and that is not necessarily the last
modification that the page's reader will need to know about.

True, but in my case it will suffice until I make the ES Matrix more dynamic.
To consider the datestamp of any file to be *necessarily* important to
the average user of the file is naive.

You are jumping to conclusions.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
the current Windows beta (empty string); and that lastModified is a
misnomer because in practice it means "last uploaded to the server".
It is not a misnomer. It reflects the date the document you are viewing
was last modified. It has nothing to do with "uploading". The FAQ
index.html, while I have been editing the FAQ, has *never* been
uploaded. The date that document.lastModified gives is exactly that,
the date that the document was last Modified. Whether that modification
is by uploading a replacement, direct editing on the server, or by
being generated on the server.
Your lack of insight is most impressive.
When a document is, for whatever reason, re-uploaded to a server
(perhaps because of a change of server, for instance), it gets a new
Last-Modified header. But if the content of the document, as seen by
the user, is unchanged, it is a disservice to assert that there has been
a modification. It is also a disservice if the change is too slight to
matter to the reader, for example correcting non-misleading typos.[*]

Surely that depends on how the file was uploaded. For example with the
right options on "rsync" the file's last modified timestamp doesn't
change. (It is this filesystem timestamp that is usually used by a
web server when setting the Last-Modified: header, and hence the
document.lastModified value).

Thank you. That reinforces my point that JavaScript lastModified is
only loosely connected to the actual date/time when the last significant
modification was made or uploaded. said:
In short it would depend on the filesystem, the upload method, and the
web server, as to what date is reported. In the common case of using
FTP to upload files the time stamp does change. But rsync, scp,
and other tools can maintain the filesystem's modification timestamp
unless the document was actually modified.

Indeed, 32-bit MiniTrue for Win-32 can edit a local file without
changing the datestamp.
Perhaps the way to deal with minor edits and last modification times
is to use a CMS or similar. Or you can add version numbers to your
documents that don't change for minor edits or re-uploads. (A simple
RCS/CVS/SVN/whatever repository will expand special tokens in
the document source into the current version number.)

Agreed.

It's a shame that the lastModified string was not specified to be an
exact copy of the Last-Modified header, which if RFC-compliant specifies
a time without any ambiguity.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>, Wed,
9 Jan 2008 20:26:40 said:
Dr said:
[...] Thomas 'PointedEars' Lahn [...] posted:
Dr J R Stockton wrote:
[...] Randy Webb [...] posted:
document.write("<p>This page last updated:" + document.lastModified) +
"<\/p>");
One should add that the result will look somewhat silly in Safari 3.0.4,
the current Windows beta (empty string);
Correct. But maybe that is because it is only a beta.

The reason is like Mr Worthing's early location.

Sorry, I don't understand.

You were not expected to. You are an uncultured person of narrow
"education". If you should come to wish to improve yourself, it is of
importance that you should take earnest steps to remedy that.

It is empty in the _beta_ version. Don't you think that this could *maybe*
be a bug that is fixed for the release?

Of course it could be, Pooh. Or, of course, they might not do so.
Nevertheless, the string is empty in 3.0.4.


There's another strange discrepancy between the behaviour of Safari and
that of IE, FF, and Opera when handling years above 275000 or
thereabouts, detected in <js-date8.htm> (that shows a different Opera
error, too); I'd like to understand it better before reporting it.
Other discrepancies have been reported.

You have stated that the `lastModified' property value would mean the date
of the last upload to the server, which is simply wrong. It would look
better on your record if you had the courage to admit your error that
instead of hiding behind your inept attempts at ad hominem attacks. But I
guess that is too much to ask for.

Don't use terms such as /ad hominem/ until you understand their proper
meaning - which is likely to be distinct from US usage. An /ad hominem/
example would be to say "KV's statement is wrong because KV is an
idiot", that's quite distinct from saying "KV is an idiot because KV's
statement is wrong".

One does not object to the written consequences of your abnormal
personality because they are written by you; one objects to your
abnormal personality because of what you write.


"Last-Modified" and "lastModified" should have been given a name
corresponding exactly to what they are - probably something like "File-
Datestamp". As it is, the naive programmer reports it as if it were
necessarily something significant to the3 reader of the page.
 
D

Dr J R Stockton

In comp.lang.javascript message said:
Dr J R Stockton said the following on 1/9/2008 8:52 AM:

Not near as impressive as your lack of the ability to understand simple
English.


Precisely. And that is the date that lastModified reads. And it is
*precisely* what it is supposed to read. Any argument that says it
should read otherwise is just plain wrong.

Indeed, according to its definition. But not according to the meaning
of its identifier, and such identifiers should be correctly meaningful.
That corresponds to the meaning of "misnomer".

If you use document.lastModified (or the Last-Modified header), to
indicate that to the user, then you deserve what you get. Because that
is precisely the date it holds, the date it was last modified.
Irregardless of how/why it was modified.

That is how it is commonly used on the Web, displayed in a manner that
will be taken by the normal reader as implying a change of potential
significance to himself.
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top