Live data from Hacker News

How One Missing `var` Ruined our Launch

blog.meloncard.com

111–120 of 223 posts

Re: How One Missing `var` Ruined our Launch

#112
post #88

Earlier quoted context omitted.

"...never learned to by hyper-vigilant..." My eyebrow just raised so far my forehead cramped.

Muphry's law.

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.

Re: How One Missing `var` Ruined our Launch

#113

Earlier quoted context omitted.

Python doesn't let you write outside of local scope without a special keyword. Exactly the inverse of JS. If you want to write to global state, and you're aware that you're writing to global state, then you use the "global" keyword. That's all.

It still conjures up variables out of thin air. sum = 0 for v in someList: smu += v print sum # prints 0, doh!

Does that actually run? I don't have an interpreter handy, but the += operator should refer to `smu` before assigning to it, resulting in a NameError.

Re: How One Missing `var` Ruined our Launch

#114
This was a problem of laziness more than anything else. To the beginner developers out there: Learn to write good code and to pay attention to the details. Don't become just another co-founder trying to do the bare minimum just to make a buck. Take pride in your work and use best practices. Be careful to avoid the careless mistakes made by the author...

  1. Did a major last minute code change
  2. Did not run jslint
  3. Did not use strict mode
  4. Did not run load tests
  5. Did not use an editor that catches scoping problems

Re: How One Missing `var` Ruined our Launch

#115
post #57

This is why I like both Scala and Coffeescript approaches. On Coffeescript vars are created for you, and on Scala, you can't not use it (you either use var or val, making it easy to change from re-assignable variables to final ones). Note that nowadays going to Coffeescript from Javascript is quite easy: http://js2coffee.org/

I don't know Coffeescript - does it implicitly create a local var when there is already a global with the same name?

It always creates a local var. You can only assign to globals indirectly, through properties of the global object e.g. window.foo = 123.

Re: How One Missing `var` Ruined our Launch

#117
post #61

Earlier quoted context omitted.

Well, that's what global variable means, shared among all functions in the process. Since there's only one process to handle all requests, they can access the global variables. All languages used in web server that don't spawn a new process for each request have the same feature.

In JS, "global variable" means "property of the global object". No reference to "process". It's trivial to have a single-process JS application with multiple JS global objects in it. Case in point: any web browser (and renderer processes don't change this: every iframe on a page has a separate global object). It's just that Node _chose_ to reuse the same global object for multiple requests instead of using a clean ex…

I have to amend my previous statement, these guys are right. I created this Python script here https://gist.github.com/1329943 and ran it. If global context were not shared, then each request would receive a new random number, but they don't. Each request gets the same number.

Even without doing this experiment, the result is obvious. There would have to be some meta process to start new interpreter processes here in order to prevent the same environment from being reused. Since that is not the case, because you have to invoke the python interpreter manually here, you cannot get multiple distinct results.

(Theoretically, in this code, you could get two different responses if they both entered the `if ... is None` clause in send_head() before one of the requests could cause the global variable to be set.)

Re: How One Missing `var` Ruined our Launch

#118

Earlier quoted context omitted.

It still conjures up variables out of thin air. sum = 0 for v in someList: smu += v print sum # prints 0, doh!

Does that actually run? I don't have an interpreter handy, but the += operator should refer to `smu` before assigning to it, resulting in a NameError.

You're right. It should be smu = sum + v

Re: How One Missing `var` Ruined our Launch

#119

Earlier quoted context omitted.

It still conjures up variables out of thin air. sum = 0 for v in someList: smu += v print sum # prints 0, doh!

Does that actually run? I don't have an interpreter handy, but the += operator should refer to `smu` before assigning to it, resulting in a NameError.

  Traceback (most recent call last):
    File "", line 2, in 
  NameError: name 'smu' is not defined

Re: How One Missing `var` Ruined our Launch

#120

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 other…

Excellent advice. You simply have to understand JS scope rules, and the Crockford post lays it out compactly. It's an idiosyncratic language.
Post reply on HN