Print Story Javascript, WTF
Software
By codemonkey uk (Mon Feb 14, 2011 at 05:00:48 PM EST) (all tags)
EDIT: Problem identified and solved. I'll leave this here as an example of typo blindness.

Okay, so normally I program in a real programming language, but having made a simple quiz game in javascript, I thought I'd have a crack at making something a bit meatier, and I've already hit my first WTF is wrong with this language moment.

See inside for inexplicably broken code an example of programmers being blind to their own typing errors.



Works:

var COINLIST = [1, 5, 10, 25];

Does Not Work:

var PENNY =  1;
var NICKEL = 5;
var DIME = 10;
var QUARTER = 25;
var COINLIST = [PENNY, NICKLE, DIME, QUATER];

Does anyone know why I waste my time with this pathetic excuse for a programming language this doesn't work? 

< wizz | Weekending Stuff >
Javascript, WTF | 31 comments (31 topical, 0 hidden) | Trackback
You misspelled the 2nd incidence of Quarter. by ammoniacal (4.00 / 1) #1 Mon Feb 14, 2011 at 05:04:35 PM EST

"To this day that was the most bullshit caesar salad I have every experienced..." - triggerfinger

Nickel's wrong too. by ammoniacal (4.00 / 1) #2 Mon Feb 14, 2011 at 05:06:25 PM EST

"To this day that was the most bullshit caesar salad I have every experienced..." - triggerfinger

[ Parent ]
Thanks. by codemonkey uk (4.00 / 1) #3 Mon Feb 14, 2011 at 05:13:35 PM EST
Good spot.  Some error messages would definitely help with JS development.

--- Thad ---
Almost as Smart As you.
[ Parent ]
If you're using Firefox by gpig (4.00 / 1) #4 Mon Feb 14, 2011 at 05:28:04 PM EST
Tools -> Error Console

Not sure about other browsers but there should be something similar.
---
(,   ,') -- eep

[ Parent ]
I'm using Safari by codemonkey uk (4.00 / 1) #7 Mon Feb 14, 2011 at 06:00:13 PM EST
And the activity log doesn't show JS errors, but I expect there is probably an JS debugging facility somewhere in it, or in the xcode suite.

--- Thad ---
Almost as Smart As you.
[ Parent ]
Chrome has an interactive console by darkbrown (2.00 / 0) #16 Tue Feb 15, 2011 at 03:55:35 AM EST
I usually end up having to go through stuff line by line to see where I've typo'd when stuff doesn't work.

[ Parent ]
Safari debugger by lm (4.00 / 1) #19 Tue Feb 15, 2011 at 06:56:22 AM EST
Safari (version 3.1 or greater) comes with a Develop menu. To enable the Develop menu, open Preferences and select the Advanced tab. The "Show Develop menu in menu bar" option should be checked.

From: http://siliconforks.com/doc/debugging-javascript/


There is no more degenerate kind of state than that in which the richest are supposed to be the best.
Cicero, The Republic
[ Parent ]
Also, by gpig (4.00 / 1) #5 Mon Feb 14, 2011 at 05:29:12 PM EST
Firebug is good for disentangling HTML / CSS / JavaScript hell.
---
(,   ,') -- eep
[ Parent ]
Nice work leaving it up. by ambrosen (4.00 / 1) #6 Mon Feb 14, 2011 at 05:30:34 PM EST
We've all been there. Whole afternoons or evenings lost to a simple typo. Thanks for sharing it.

Your welcome by codemonkey uk (4.00 / 1) #8 Mon Feb 14, 2011 at 06:01:21 PM EST
Deleting it did cross my mind, but everybody makes mistakes, pretending otherwise is silly.

--- Thad ---
Almost as Smart As you.
[ Parent ]
Yes by ucblockhead (2.00 / 0) #11 Mon Feb 14, 2011 at 08:10:12 PM EST
Ask me sometime how long was spent on a bug caused by the difference between "PROFILE" and "PROFlLE"
---
[ucblockhead is] useless and subhuman
[ Parent ]
2 days lost by Breaker (2.00 / 0) #18 Tue Feb 15, 2011 at 06:39:18 AM EST
To:
if (x == SPECIAL_NUMBER);
{
    return (DoSomeStuff());

}

return (BAD_THING_ERRORCODE);


[ Parent ]
Took me a while there. by ambrosen (2.00 / 0) #20 Tue Feb 15, 2011 at 07:03:33 AM EST
Yep, that one's infuriating.

[ Parent ]
Yes. by Breaker (2.00 / 0) #30 Wed Feb 16, 2011 at 01:06:48 PM EST
Haven't made that error since, though.


[ Parent ]
I spot that instantly by codemonkey uk (2.00 / 0) #22 Tue Feb 15, 2011 at 07:18:48 AM EST
Spelling errors in words, however, I struggle with.

--- Thad ---
Almost as Smart As you.
[ Parent ]
This was about 16 years ago by Breaker (2.00 / 0) #29 Wed Feb 16, 2011 at 01:05:17 PM EST
Monitors weren't that great then, and 80x40 chars was a luxury!  Oh and compilers that actually worked.  MS C6.00 was also a bug riddled bag of shit.

I did learn an awful lot of assembler in that job, though.

Had 3 other people look at it and they didn't spot it though either.


[ Parent ]
VS2FP!!!!1111 by kwsNI (2.00 / 0) #9 Mon Feb 14, 2011 at 07:15:11 PM EST
Still, Java sucks. 

To be fair, JavaScript doesn't help here by fluffy (2.00 / 0) #10 Mon Feb 14, 2011 at 07:16:45 PM EST
Oh, you're referencing a variable that's outside this scope? Okay, I'll just reference it in the first enclosing scope where it appears.  Oh, it doesn't appear in any of them? I guess I'll just make a new global, then! Tee hee!
busy bees buzz | sockpuppet revolution
Yeah by ucblockhead (2.00 / 0) #12 Mon Feb 14, 2011 at 08:12:28 PM EST
If only you could replace JavaScript with Python.
---
[ucblockhead is] useless and subhuman
[ Parent ]
Well .... by gpig (4.00 / 1) #14 Mon Feb 14, 2011 at 09:08:11 PM EST
You can compile Python to JavaScript, is that good enough?
---
(,   ,') -- eep
[ Parent ]
Note by ucblockhead (4.00 / 3) #13 Mon Feb 14, 2011 at 08:16:39 PM EST
If you do this, you'll catch these sorts of errors:

var COINS = {'PENNY':1, 'NICKEL':5, 'DIME':10, 'QUARTER':25};

var COINLIST = [COINS.PENNY, COINS.NICKEL, COINS.DIME, COINS.QUARTER];

(Not to defend JavaScript.  Its scoping is seriously broken.)
---
[ucblockhead is] useless and subhuman

In both Chrome and Firefox by gazbo (4.00 / 2) #17 Tue Feb 15, 2011 at 05:58:44 AM EST
The code he posted results in a ReferenceError. I imagine the same is true in all browsers, because I believe it's part of the ECMA spec.  In fact, your code masks the error because you're just accessing an unset member of a hashmap which evals to undefined quite legally.

The real source of these errors is the attitude people take of "JS isn't a real language therefore I'll not treat it like a real language".  If someone's makefile had 2>/dev/null after each line, would you agree with them that $language was stupid because their code wouldn't work "for no reason"?

I'm not really having a go at codemonkey here, it's something I see constantly in my day job: "I don't know Javascript but I've decided to write some anyway by guessing how it works, and it doesn't work because JS is broken."  I can't think of any other language where people would do that.


I recommend always assuming 7th normal form where items in a text column are not allowed to rhyme.

[ Parent ]
"undefined" by ucblockhead (4.00 / 1) #27 Tue Feb 15, 2011 at 03:11:53 PM EST
Yes...that would have helped him catch it, if his list ended up [1, undefined, 10, undefined].

But I agree completely with people misunderstanding what JavaScript is.

The main trouble is that JavaScript gained popularity in tiny browser helper code.  This let people work with it without really learning it.
---
[ucblockhead is] useless and subhuman

[ Parent ]
A cow-orker of mine ... by lm (4.00 / 1) #15 Mon Feb 14, 2011 at 09:47:31 PM EST
... just last week burned through an entire day because she didn't notice a section in a config file was labeled XXXXSETTINGS rather than XXXXSETTING.

The day before that I blew an hour troubleshooting a problem that stemmed from a system setting being labelled XXXXCONNECT rather than XXXXCONNECTION.

But my all time favorite was from circa  2000 when a cow-orker came to me to review his code because it wasn't acting like he expected it to. For some reason

if (returnCode = SUCCESS)

was always entering the if block even if returnCode wasn't set to SUCCESS.


There is no more degenerate kind of state than that in which the richest are supposed to be the best.
Cicero, The Republic
(Comment Deleted) by mellow teletubby (2.00 / 0) #21 Tue Feb 15, 2011 at 07:08:16 AM EST

This comment has been deleted by mellow teletubby



In reply by codemonkey uk (2.00 / 0) #23 Tue Feb 15, 2011 at 07:26:09 AM EST
I know the problem was human error.  That was established yesterday.

As for what's wrong with associative arrays, nothing, really, but I'm learning JS as I go, and coming from a high performance C++ background, throwing a hashmap at a problem that only requires a small (flat) look up table is not my first instinct, and even on reflection, feels like a grotesque waste of cycles and memory (even though I realise JS likely implements all forms of array as a tree structure under the hood).

--- Thad ---
Almost as Smart As you.

[ Parent ]
EVERY object is a hashtable by gazbo (4.00 / 1) #25 Tue Feb 15, 2011 at 09:19:19 AM EST
Because JS has first class functions, every object is just a map of property name -> value, where value might just happen to be a function.  Arrays? Yup, they're hashtables but with a few useful methods defined and a little bit of magic that means once you create myArray[5] it sets myArray.length = 6.

If you want anything more complex than a string, number or boolean then you're going to end up with an object that is in turn a hashtable.  Embrace it! JS is actually pretty efficient at such things.  And in the cases where it really matters how many bits you're pushing around, then frankly JS just isn't the right tool.  Hmm, wonder how many browsers support <script language="c" />...


I recommend always assuming 7th normal form where items in a text column are not allowed to rhyme.

[ Parent ]
Performance by ucblockhead (2.00 / 0) #28 Tue Feb 15, 2011 at 03:17:25 PM EST
For what it's worth, JavaScript performs better than you'd think.  We've got a large non-browser based application based on JSCore that is heavily animated.  We're able to get animations running at 60 FPS with the animations themselves running in pure JavaScript.
---
[ucblockhead is] useless and subhuman
[ Parent ]
Dear codemonkey by Breaker (2.00 / 0) #31 Wed Feb 16, 2011 at 01:11:54 PM EST
Please come and work in the IT banking industry.  There's too many fakers, liars and dickweasels.

You've consistently demonstrated a "tools for the job" mentality rather than a "tools that'll make my CV look good".

We have maths (as much as you like, although probably some beneath you for some simple pricing algos), and optimised code, and less travel for you. 

And money.  No quibbles about hardware, or dev / test environments.

Come to the darkside, CM!


[ Parent ]
I feel your pain by houser2112 (2.00 / 0) #24 Tue Feb 15, 2011 at 08:14:54 AM EST
I've lost count of how much time I've lost to chasing errors that didn't exist because I forgot to commit changes to the database.

curse you HSL by TPD (4.00 / 1) #26 Tue Feb 15, 2011 at 10:00:14 AM EST
I was doing so well till then.


why sit, when you can sit and swivel with The Ab-SwivellerTM
Javascript, WTF | 31 comments (31 topical, 0 hidden) | Trackback