473,696 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Top warning signs of bad code?

It seems most people get there JS off web sites, which is
entirely logical. But it is also a great pity since most
of that code is of such poor quality.

I was looking through the JS FAQ for any question that
identifies the warning signs to look out for, the things
that most easily and clearly identify the author of code
as something less than a master of the art.

I did not find an FAQ that answered it, but I think the FAQ
should contain the info., so, here is my proposed list..

<FAQENTRY class='more_Spe culative_For_Di scussion_OK?'>
1 <SCRIPT LANGUAGE='JavaS cript'> // last millenium, or another reality?
2 if (navigator.user Agent.indexOf(" IE")) // #FAQ4_26
3 eval(); // #FAQ4_40 (usually)
4 if (..==true) // !understanding boolean.
5 <a href="javascrip t:somefunction( )"> // #FAQ4_24
6 <NOSCRIPT> // If missing, the script does not degrade gracefully.
7 document.write( "<p>Importa nt content.."); // delivering content.
</FAQENTRY>

What is your opinion? Are these the top 7?
Which 28 did I miss?

Also, why does the FAQ not contain the words "degrade",
"graceful" or "noscript" (I guess I might have been
looking for the wrong term with the first two, but
no mention of NOSCRIPT? That surprised me.)

BTW - The Jibbering JS FAQ is the single greatest FAQ*
I use, but I just have a hankerin' to get mentioned in
it, or at least contribute to it. ;-)

* I have my own (fledgling) Java FAQ that I hope might
approach the quality of the JS FAQ one day, and am a
contributor to the recently expanded and improved
c.i.w.a.s. FAQ.

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
Jul 23 '05
109 5881
On Sun, 19 Sep 2004 23:42:04 GMT, Andrew Thompson
<Se********@www .invalid> wrote:
6 <NOSCRIPT> // If missing, the script does not degrade gracefully.
This is definately not BAD, I never use NOSCRIPT, it's bad (it relies
on the idea that script support is a boolean - it's not! perhaps if
UA's after erroring worked as if script was disabled and redrew the
page with NOSCRIPTS then maybe... but they don't!)
BTW - The Jibbering JS FAQ is the single greatest FAQ*
I use, but I just have a hankerin' to get mentioned in
it, or at least contribute to it. ;-)


It's 8 or 9 years old now, with many different editors and lots of
input from the group, it's a good example of what you can get when
usenet groups do contribute well to the faq I think.

Jim.
Jul 23 '05 #11
On Mon, 20 Sep 2004 02:15:01 GMT, Andrew Thompson
<Se********@www .invalid> wrote:
On Mon, 20 Sep 2004 00:37:59 GMT, Andrew Urquhart wrote:
10. Attempting to use IE-only methods and properties in a WWW
environment.


.. or being UA specific in a WWW context, which I
agree is very bad.


Afraid not, if your content degrades, it doesn't matter that you're UA
specific, since JS is only an optional extra, there may be very
sensible reasons to keep the QA and maintenance costs manageable by
only attempting a subset of UA's - it depends on what you're doing of
course, but I don't think it's as cut and dried as you suggest.

Jim.
Jul 23 '05 #12
On Sun, 19 Sep 2004 23:04:30 -0500, "Matt Kruse"
<ne********@mat tkruse.com> wrote:
Andrew Thompson wrote:
I agree with this, but I would not agree with this:
if (var==1) //bad
bviously, 1 evals to true and so the ==1 is not required, but I disagree
with many who say this is bad style. I think it's better to include the ==1
or ==0.


+1
5 <a href="javascrip t:somefunction( )"> // #FAQ4_24


This is too broad of a condemnation. Similar to eval, it does have its
place.


Hmm, I don't agree with this, there's always more reliable ways to do
the same - especially with IE service packs removing the ability to
script the resulting document (when somefunction returns a document).
Did you have something in mind?

Jim.

Jul 23 '05 #13
On Mon, 20 Sep 2004 07:05:29 GMT, Jim Ley wrote:
On Mon, 20 Sep 2004 02:15:01 GMT, Andrew Thompson
<Se********@www .invalid> wrote:
On Mon, 20 Sep 2004 00:37:59 GMT, Andrew Urquhart wrote:
10. Attempting to use IE-only methods and properties in a WWW
environment.


.. or being UA specific in a WWW context, which I
agree is very bad.


Afraid not, if your content degrades, it doesn't matter that you're UA
specific, since JS is only an optional extra, there may be very
sensible reasons to keep the QA and maintenance costs manageable by
only attempting a subset of UA's - it depends on what you're doing of
course, but I don't think it's as cut and dried as you suggest.


OK. I must admit that sometimes my definition
of 'degrade gracefully' is to have a message
appear in older browsers using CSS import hacks
to display the message 'we have not been able
to extend the provision of ... to this browser'

I was more thinking of JS that works in only a single
browser due to that particular UA maker's use of a
proprietary element or functionality.

In the reply to Matt, I mentioned an example of
a SlideShow with fade transtitions. The SlideShow
itself cross-browser, but the fading effect available
only on IE.

Could that be a suitable example (for a WWW context)
of a 'SlideShow' - yes, of a 'SlideShow with fading
image transitions', no.

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
Jul 23 '05 #14
How about adding lame efforts to disable the context menu (e.g.
"document.oncli ck = function(){retu rn false};")

--
Philip Ronan
ph***********@v irgin.net
(Please remove the "z"s if replying by email)
Jul 23 '05 #15
Andrew Thompson wrote on 20 sep 2004 in comp.lang.javas cript:
4 if (..==true) // !understanding boolean.

I'm unsure of chastising #4 owing to the things
that can be implicitly cast to a Boolean.


I may have written that incorrectly, or expressed it
badly, I meant to indicate testing for true when
the result of a computaion was already 'true'.


... already boolean [could be false too]
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #16
On Sun, 19 Sep 2004 22:40:40 -0400, Randy Webb wrote:

(Andrew Urquhart)
11. Use of 'InnerHTML' can be considered bad style, but it is
significantl y more usable (though abusable) than the alternatives.

(A.T.) Can you expand on that? I have only started paying
attention to innerHTML recently, and have not really
got the gist of it yet.


innerHTML is a proprietary method created by Microsoft and adopted by
other browsers. The alternative is to create a textNode and insert it,
which seems to have less browser support.


From that description, I do not think a blanket
'use of innerHTML is bad' is justifiable, and the
circumstances where it *is* bad style seem to come
back to the more general idea of degrading gracefully
based upon feature detection.

Does that sound right?

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
Jul 23 '05 #17
On Mon, 20 Sep 2004 03:05:58 GMT, Andrew Thompson <Se********@www .invalid>
wrote:
On Mon, 20 Sep 2004 02:27:15 +0100, Richard Cornford wrote:
[snip]
..The valid HTML question (while I support and
endorse it) is more ambiguous as the LANGUAGE attribute will never
directly harm a script in an HTML browser in itself. Should it actually
be considered bad to include the superfluous?


I vote yes. It is extra bytes for no good reason,
it is invalid and effectively 'gets in the way' of
seeing the actual HTML errors when validating*.


The problem with using the language attribute occurs when a version is
specified, and a browser actually respects that version.

Richard's example of v1.2 is the worst as some basic language rules were
changed, resulting in unexpected behaviour. An example is:

var a = 10,
b = 20;

if(a = b) {

Normally, this would assign the value of b to a and the expression,
converted to boolean, would evaluate as true. However, under v1.2 rules,
this would actually be a comparison and evaluate to false.

This is the worrying thing about most script collections. They use the
language attribute, needlessly specify version information; completely
unaware of its potential implications.

Richard did write a piece on the SCRIPT element and its attributes for the
FAQ notes, but it hasn't made it in yet.

[snip]
4 if (..==true) // !understanding boolean.


Maybe (it certainly demonstrates a questionable grasp of the nature of
boolean values, and that is not a good sign in a programmer).


I will see what others have to say. So far Andrew
felt it shouldn't be in, and you question it, it's
looking like I might have been wrong.


I agree with Richard. It's not bad code, per se, but it does show a lack
of knowledge or confidence with the language. Most programmers are
comfortable with the implicit type conversions that occur and know
precisely what the evaluation of numbers as booleans will do. Those less
comfortable will prefer to explicitly state the comparison.

I wouldn't recommend it, though, purely from a style perspective. It's
redundant and clutters expressions.

[snip]
Were you saying recently that even NN 4.78 was conducive
to element display/hide using JS?


You can hide layers (LAYERs or positioned DIVs) in 4.77 (that the earliest
I have) using the visibility property. However, the syntax changes
somewhat, and the values are different.

<div id="test" style="position : absolute;">Page content</div>

var d = document.layers['test'];
d.visibility = 'hidden'; // Hide the DIV
d.visibility = 'visible'; // Show the DIV

The awkward part comes when you try to check the current state. Whilst you
can assign hidden and visible and the achieve the expected behaviour,
reading the value back will give you 'hide' and 'show', respectively.

I don't believe this can be applied to any other elements, though.

[snip]
Browser detecting by object inference should be included.


Do not recall seeing the term, but I think I can guess
what it means, I will research it a bit further.


The common example is:

if(document.all ) {
// Using IE

which is ridiculous because Opera supports the all collection, and I'm
lead to believe that recent versions of ICEbrowser does, too. I'm sure
there will be others.

This particular strategy can fail disasterously as all it takes for many
dependent scripts to break is for Mozilla to include the all collection in
its DOM. I believe there's some discussion about this, but hopefully it
won't happen, simply because I hate IE-isms and that one is particularly
useless with the advent of the W3C DOM.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #18
*Andrew Thompson* wrote in comp.lang.javas cript:
*Andrew Urquhart* wrote:
11. Use of 'InnerHTML' can be considered bad style, but it is
significantly more usable (though abusable) than the alternatives.


Can you expand on that? I have only started paying
attention to innerHTML recently, and have not really
got the gist of it yet.


http://www.developer-x.com/content/innerhtml/
--
Andrew Urquhart
- FAQ: http://www.jibbering.com/faq/
- Archive: http://groups.google.com/groups?grou...ang.javascript
- Contact me: http://andrewu.co.uk/contact/
Jul 23 '05 #19
>><FAQENTRY class='more_Spe culative_For_Di scussion_OK?'>
1 <SCRIPT LANGUAGE='JavaS cript'> // last millenium, or another reality?
2 if (navigator.user Agent.indexOf(" IE")) // #FAQ4_26
3 eval(); // #FAQ4_40 (usually)
4 if (..==true) // !understanding boolean.
5 <a href="javascrip t:somefunction( )"> // #FAQ4_24
6 <NOSCRIPT> // If missing, the script does not degrade gracefully.
7 document.write( "<p>Importa nt content.."); // delivering content.
</FAQENTRY>
8. document.getEle mentById("myId" ).style.color = "#ff3333"; // Not
testing for methods and properties before using them
9. document.myForm Name.myTextInpu tName.value = "dummy"; // Using
hierarchy shortcuts that may not work as intended in non-IE browsers.
(NB: I really need to check up on how modern browsers cope with such
things)
10. Attempting to use IE-only methods and properties in a WWW
environment.
11. Use of 'InnerHTML' can be considered bad style, but it is
significantly more usable (though abusable) than the alternatives.
12. onblur, onfocus, onchange - usually used to surprise the user by
making form controls behave in unexpected non-standard ways.


0. It does not pass JSLINT. http://www.crockford.com/javascript/lint.html
Jul 23 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.