Live data from Hacker News

How One Missing `var` Ruined our Launch

blog.meloncard.com

31–40 of 223 posts

Re: How One Missing `var` Ruined our Launch

#31
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…

I wasn't initialising the x variable

Do you mean s/initialising/declaring/, or am I just really confused?

Re: How One Missing `var` Ruined our Launch

#33
My var story was a little harder scoping issue where a variable held the information for a pending call. It would asynchronously process the call, then mark the call as completed. Well, if there were more than one call going through, the callback was only getting one record, causing it to repeat the call about five times before I caught it and fixed the scoping.

Re: How One Missing `var` Ruined our Launch

#34
As many have pointed out, there are absolutely tools that would catch this kind of problem in development. But the experience also speaks to the lack of sophisticated observability tools for Node (and just about every other popular dynamic language too).

Re: How One Missing `var` Ruined our Launch

#35
JS is so easy to cock up in, I commonly find myself logging to the console just to make myself sure of the scope and other things.

This technique totally went to shit when I came across one of our scripts that gratuitously used `apply()` all over the place.

The other common error is array iteration, and I've not quite understood why iterating through one array in the same scope as where it was created works fine, but passing it to another function and performing the exact same routine also goes through the prototype methods after the elements.

Of course, particularly with the var mistake, you'd never really understand the magnitude of it until you attempted to use JS on the server side. This post has, quite thankfully, likely exposed a bug in my own code I couldn't quite understand a while ago. :)

Re: How One Missing `var` Ruined our Launch

#36
post #28
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…

Have you had any luck getting it to handle modern JavaScript style? Whenever I've tried js2-mode it really hasn't liked jQuery style nested anonymous functions.

Do you have a specific example in mind? It works pretty well for me.

Re: How One Missing `var` Ruined our Launch

#37

One thing you can do to help avoid this: use JSLint (or something equivalent) to check for missing var keywords. And, the obvious (as you already mentioned) coffeescript. Would love to hear of other suggestions on how to effectively debug this, especially in node.

JSLint was my first response, but I have to agree with someone higher up -- use strict is probably a better solution, since it'll happen whether you like it (or forget it) or not.

Re: How One Missing `var` Ruined our Launch

#38
I'm not an expert in JavaScript...

Given that you said that, and that the problem you hit is a common pitfall for Javascript developers (especially if you're not very seasoned with the language), I'd strongly recommend picking up a copy of Douglas Crockford's Javascript: The Good Parts. Not only does he inform readers of this particular gotcha, but he also elaborates on Javascript best practices and tools that others are bringing up in their comments.

Re: How One Missing `var` Ruined our Launch

#39
Similar, but not quite as world-ending issue we had launching our Android app. Shortly (~36hrs) after launching, we had noticed that we were compiling with Cupcake (and we only intended to support Eclair+). We were pushing to have it out for SXSW; in a rush we just re-compiled, made sure it still launched, and pushed to the Market.

After everyone got some sleep, we realized that Cupcake didn't have multi-res support; and later phones quietly re-scaled everything to compensate. Compile to Eclair and well, things were a bit out of proportion.

Re: How One Missing `var` Ruined our Launch

#40
this question can be found in the first chapter of every good JavaScript Book: What is the difference between a and b in the following statement?

a = b = 0;

In JavaScript you CAN define a variable without var, but then it becomes global. As code is read from the right side, b is assigned 0, but b is a variable now, so a is assigned to a variable and is not global then. The difference is: b is global and a is local. What do we learn from this? NEVER forget var unless there is a reason ..

Post reply on HN