Live data from Hacker News

How One Missing `var` Ruined our Launch

blog.meloncard.com

201–210 of 223 posts

Re: How One Missing `var` Ruined our Launch

#201

Earlier quoted context omitted.

Global variables are basically instance variables on the process itself. They're going to be shared across all requests in all environments that handle more than a single request per process. If they had different semantics, they wouldn't really be what normal people call "global variables", would they? So you just don't use them and everything's good. Which brings you to the real problem: JavaScript makes it distres…

> Global variables are basically instance variables on the > process itself. Not in JS. See my response to ww520 below.

Ah, I see what you're talking about.

Seems like jesseedhillon was expecting that Node works like a browser: there's a lot of backend code in another language that handles the HTTP request, and then forks it over to a little JavaScript, and each request gets a new global scope.

It's not like that. There's a teeny bit of C-based backend code; it's the JavaScript part that controls alll the interesting stuff like opening sockets and parsing HTTP headers and the like to determine a request. In a single process, with a single global object, with lots of asynchronous I/O.

Node is JavaScript as a first-class programming language, not subservient to the DOM.

Re: How One Missing `var` Ruined our Launch

#202

Earlier quoted context omitted.

> Global variables are basically instance variables on the > process itself. Not in JS. See my response to ww520 below.

Ah, I see what you're talking about. Seems like jesseedhillon was expecting that Node works like a browser: there's a lot of backend code in another language that handles the HTTP request, and then forks it over to a little JavaScript, and each request gets a new global scope. It's not like that. There's a teeny bit of C-based backend code; it's the JavaScript part that controls alll the interesting stuff like openin…

This has nothing to do with the DOM. There are existing server-side JS deployments that have no DOM, but do create separate globals per request.

There is nothing other than a conscious design decision that forces the Node code that you write to run against the same global as the code that handles the I/O guts in Node...

In particular, you could have the code that listens on the socket and chunks up the byte stream into HTTP requests running against one global while all further processing of those requests runs against a different global or set of globals. It's just that Node chose not to do that. But that's not an inherent limitation of either JS the language or the V8 implementation.

Re: How One Missing `var` Ruined our Launch

#203
For handling asynchronous exceptions and bad stack traces, checkout the "trycatch" module on npm, http://github.com/crabdude/trycatch . It's an asynchronous try catch.

  trycatch(function(){
    process.nextTick(function() {
      throw new Error("This will be caught.");
    });
  }, function(e) {
    console.log(e.stack);
  });
I also integrated it into the step module to create "stepup" which adds error coalescing, http://github.com/crabdude/stepup .

Re: How One Missing `var` Ruined our Launch

#204
A un-initialised varible made our script to mail 20 copies of the same email to almost 10k users. One of our mass email script basically breaks the email list in chunks of 500 and emails them. The array variable that hold this 500 email chunk was not initialised. In our test everything was fine as the test list was never more than 500. When the time came to send emails to the production list, the script basically kept sending multiple copies of the same emails (since the array retained the old values across the loop iterations).

I realised this after about 10 minutes but already multiple copies of the same emails (20/user) was send to more than 10k users!...The end result was annoyed users unsubscribing in droves and possibly a hit in our email reputation. This is all because a stupid array variable was not initialised :)

Re: How One Missing `var` Ruined our Launch

#205

Earlier quoted context omitted.

Have you tried the GCC -Wshadow warning flag? It will generate a warning whenever you define a variable with the same name as one declared in an outer scope. Between that, and using -Werror to ensure that warnings never get ignored, I find C99-style declare-anywhere quite useful. It helps me keep the scope of variables limited to the places that need them. That said, I rarely use C99's ability to declare a variable i…

(I'm no C expert, I'm posting my take on this mainly because I'm interested in learning from counterarguments): Greatgrandparent's error was lack of such inner declaration. -Wshadow wouldn't help with that. Also, I imagine enforcing old-style declaration restrictions helps you to avoid introducing those errors in first place, but it's just as hard to debug them once they're in. My choice would be to get in the habit…

I agree with you entirely that -Wshadow (or an equivalent for JavaScript) would not solve the problem reported in the original article. However, it does solve the problem cperciva mentioned in the comment I replied to: "If you stick to declaring all your variables in one place, it's much easier to notice when you've already used a variable name.". -Wshadow detects when you've already used a variable name and reports that as a warning (or an error if you use -Werror, which you should).

I also agree about declaring a variable in the narrowest scope that makes sense. I often see code that attempts to reuse the same variable for several different purposes, rather than declaring separate variables in narrower scopes.

Re: How One Missing `var` Ruined our Launch

#206
post #198

Earlier quoted context omitted.

Umm, I find the opposite to be true. By always declaring variables where I use them (notably in for loops), I avoid accidentally reusing a variable I used elsewhere.

That is a special case. Loop headers should be considered the top of scope blocks in C. I don't know why the language designers didn't do that.

They did, in C99.

Re: How One Missing `var` Ruined our Launch

#207
post #65
post #64

Earlier quoted context omitted.

Hmm, so you wrote a careless typo while describing your "careless" coding? Not to be harsh, but maybe it's a hint?

Well if you really want to go into painful detail about it, I had originally phrased the sentence differently, something along the lines of "leaving out the var in the for loop initialisation". It wasn't very readable so I changed it, but the word 'initialise' was on my mind. In any case, I'm not sure that I need a hint to tell me that doing things quickly and carelessly is a bad idea. But we all do it sometimes, in…

Well sure. I actually wasn't trying to be harsh OR sarcastic. It's not pop psychology (I think it goes back to Freud) that in some sense there are no mistakes. We all make errors and I was just saying that two errors about the same issue might be a hint that one has "issues" about the, er, issue or that some self-sabotage in important matters might surface under stress. You're the only one can answer that. I admire your courage in sharing your experience with all these crack programmers.

Re: How One Missing `var` Ruined our Launch

#208
post #124

Earlier quoted context omitted.

I'm contemptuous of his coding practice, not his writing or grammar. His writing is pleasant to read... but so what? where are the multiple layers of defense standing between a simple syntactical or semantic error and a full scale business-impacting technical fuck up. that is what I meant by being vigilant and paranoid.

I'd be offended if it wasn't for the fact that your original accusation appears to be that I had had a happy childhood. I'll take that one on the chin. As for the rest... well, I don't understand where you're going with it. Pop psychology aside, are you really suggesting that you write utterly seamless code every single time? You've never done a build and then realised that you made an error somewhere along the way,…

>>You act as if my mistake had the potential to ruin a business. Of course it didn't- I picked up on it before the code had even been pushed to the remote repository.

Not to beat on you, but didn't you discover the error after users started using it in a big news day?

Re: How One Missing `var` Ruined our Launch

#209
post #64

Earlier quoted context omitted.

Hmm, so you wrote a careless typo while describing your "careless" coding? Not to be harsh, but maybe it's a hint?

Not to sound harsh, followed by something quite sarcastic and judgemental isn't a particularly nice way to address someone you don't know. I for one would speculate, not to sound harsh, that if you haven't made silly errors like this then you haven't been around the block enough times.

I think you misinterpreted my remark. This actually wasn't a "silly" error, it was, as many have pointed out, an avoidable error. I think many don't take javascript seriously and it bites them.
Post reply on HN