Live data from Hacker News

How One Missing `var` Ruined our Launch

blog.meloncard.com

11–20 of 223 posts

Re: How One Missing `var` Ruined our Launch

#13
post #3

Am I right in thinking that the javascript /* "use strict" */ construct would have caught this mistake (just like it would have done for me in perl code)? Seems to me that some such feature is absolutely and utterly necessary in any environment where you're doing a lot of closure creation ..

Yes:

  $ node
  > "use strict"; initial = "foo";
  ReferenceError: initial is not defined
      at repl:1:17
      at Interface. (repl.js:168:22)
      ...

Re: How One Missing `var` Ruined our Launch

#14
Damn, I feel for ya; scope is hell sometimes, and js is all about giving you the power to shoot yourself in the foot. Interesting how bugs like this, while terrible and terribly easy to miss, tend to do a lot less damage on the front-end of things (or doesn't show up in a single thread/user environment). It was the potential of things like this happening that drove me towards languages like erlang and java on the server (even if js is still my favorite language).

Re: How One Missing `var` Ruined our Launch

#15
post #5

Reminds me of one of the more confusing bugs I've ever encountered in my life. I was throwing together a quick UI with Adobe Flex, and for some reason every time you clicked a particular button, the entire UI would shift 20 or so pixels to the right. I spent hours scratching my head until I noticed this for loop: for (x=0;x I wasn't declaring the x variable, so it was using the x part of the x/y positioning of the UI…

Ha, that reminds me of a program I was writing in MATLAB. I used `i` for a loop index in one part of the script and then later when doing some complex number calculations (`2 + 3i`)...it was one of those so-stupid-you-have-to-laugh moments when I finally figured it out.

Re: How One Missing `var` Ruined our Launch

#17
Common problem. Live and learn.

But the problem is a bit more meta than Javascript scoping. Something would have happened no matter your environment. It's always extremely risky to make huge changes just before a launch day. Stuff always breaks. Sounds like you did a good job of communicating with your customers. Ironically, they will probably be better customers than they would have been if you didn't have any problems. Overcoming adversity brings people together.

Add some concurrency to your test suite. If you have any system level or black box tests, you can often just run the same test multiple times in parallel with different threads. No new tests required. Note that this may (probably will) also uncover other previously inconceivable concurrency issues.

Re: How One Missing `var` Ruined our Launch

#18
post #9

Since everyone is chiming in with ways to prevent this sort of thing, here is another: js2 mode for Emacs[1]. This is a mode originally written by Steve Yegge and then modified by some other people (be sure to get that version) that actually parses the code and, among other things, highlights global variables in a different color than local ones. I find this, along with the other things js2 does, helps prevent a whol…

I was going to say the same thing, it also makes me avoid missing semicolons by making those errors feel like sores in my code. I can't let them mess things up they need to be gone before I can write another line.

Re: How One Missing `var` Ruined our Launch

#20

Ouch. I always thought JS's global-variables-by-default schtick was it's worst crime, but I've never seen such terrible consequences for it up close before. Note to self: before products get to forbes, do some load testing. Even if I am using node, with its magic event loop of invulnerability.

As a rule, in js I declare every var at the top of the function (or globe) in one giant var statement just because of this. Still, it's easy to get lost in the moment and mess up just once. I used to think this was just the way things were but pair-programming/code-review and/or a language with better scoping rules would've prevented this from happening.
Post reply on HN