Re: Proper coding? (JS newbie)



In microsoft.public.scripting.jscript message <46dd957b$0$24571$ecde5a14
@news.coretel.net>, Tue, 4 Sep 2007 13:22:46, Paul E. Schoen
<pstech@xxxxxxxxx> posted:

My code was severely criticized by someone, and I want to understand why
that might be. Please comment on the code I have posted on my website at:

http://www.smart.net/~pstech/DogLicenseJS.htm

First, looking at the displayed page.

Here, I see two sets of radio-buttons have no preselect, but the other
does.

I don't see any point in making some of the words bold.

"obedience training>".

Evidently you've not checked it with W3's free downloadable TIDY or with
their Web checker. Bad HTML might cause all sorts of problems.

Input boxes are possibly too short. If your dog is called "William
Jefferson Clinton" then with IE6 defaults its name will be visually
curtailed, which is inelegant. Someone living at Apartment 456, Dwight
D Eisenhower Towers, William Jefferson Clinton Boulevard will be more
annoyed. There should be a visual space after each of those entry
fields, IMHO.

Now, looking at the source code of the page.

By reading the Weight onChange, you could save having to read it in many
places in the code. If your users can be trusted to enter only digits,
then parseInt is not needed; use unary +. Otherwise, the field should
be pattern-validated - parseInt(X) for X="1O" will return 1, etc.

The functions which alert can also handle the fee change.

Given that you have several sets of buttons, you should consider a
function which, given a reference to the set, will return a number
representing its state, which can be used to index an array. Result :
less code.

I use
function RadBtns(Rbtn, Arr) { var Q, J=0
while (Q=Rbtn[J]) { if (Q.checked) return Arr[J] ; J++ } }
but

function RadBtns(Rbtn) { var Q, J=0
while (Q=Rbtn[J]) { if (Q.checked) return J ; J++ } }
might suit you better. They may return the undefined value.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
.


Loading