Live data from Hacker News

How One Missing `var` Ruined our Launch

blog.meloncard.com

151–160 of 223 posts

Re: How One Missing `var` Ruined our Launch

#151

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

Sure, blame the victim, not the idea that default variable scope is global.

There are libraries in other languages providing node-like capabilities. He could use those and not have the global scope problem. Not saying that the idea makes any sense either...

Re: How One Missing `var` Ruined our Launch

#152
You will probably get hundreds of comments with unsolocited advice, but let me just say to checkout and setup JSLint:

https://github.com/douglascrockford/JSLint

Setup and how you use it is more important than just using it. I run it in three places:

1) In my IDE (bound in vim on save, same in TextMate)

2) As a Git checking hook

3) In deployment / build scripts

Turn all the warnings way up and it always catches redeclaration bugs.

Set it up once and don't allow any code to get checked in or deployed with even a single warning present.

I also pass all Javascript through the Google Closure Compiler, but with the lowest compilation setting, because it is very good at picking up small errors as well.

(if anybody is interested in how to set this all up I can publish my configs and scripts)

Re: How One Missing `var` Ruined our Launch

#153
post #48

Why has it become popular for dynamic languages to conflate establishing a binding with assigning it a new value? Ruby, Python, and Javascript are all guilty of this. Scheme got it right sometime in the 1970's. (let ((x initial-value)) ; binding (set! x new-value)) ; assignment Or in infix syntax (Dylan): let x = initial-value ; x := new-value ;

Io has a similar distinction. In the Io language, := is syntax sugar for newSlot and = only means updateSlot. So if you accidently = a slot that doesn't exist, it doesn't new-up a magic global, it raises an error.

I recently took time to learn Io and I love it. It is a great language to know and learn if you do a lot of Javascript development.

Re: How One Missing `var` Ruined our Launch

#154
post #75
post #57

Earlier quoted context omitted.

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

CoffeeScript doesn't allow you to shadow variables in enclosing scopes. The compiler will declare the variable in the "outer-most" scope in which is is used and then any uses in enclosed scopes refer to that one. In this case as long as OP was not using the variable in an enclosing scope, it would have been made local to the function.

And CS also wraps modules in closures (by default), so an outermost-scoped variable is still local to the script, whereas it would be global to all scripts in vanilla JS.

Re: How One Missing `var` Ruined our Launch

#155

"It’s a damn tragedy. I’m not an expert in JavaScript" Hell yea, the valley's full of smart kids who don't know what the hell they're doing with JavaScript and thinks they're one hell of a hacker when they discovered that they can do shit with a few lines of JS and trumpeting on the how great NodeJS or whatever the greatest and newest shiny framework out there. If you don't really understand scoping in JS, please don…

I think "a damn tragedy" is a pretty apt description. Well-designed languages shouldn't require reams of experience. Just a little intelligence and a reference for any questions that arise should get you pretty far.

I'm sure this guy "really understand[s] scoping in JS", but he still got bitten. The blog post isn't frivolous, its point is that javascript is a minefield. Which is an important lesson. Use JSLint or "use strict" or pay the price.

Re: How One Missing `var` Ruined our Launch

#156

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.

I think the author of the blog post understands JS scope rules. His point isn't that you shouldn't need to have a "var" in front of your declarations or that javascript is stupid and he doesn't understand it, it's that there should be a better way to find bugs like the one that bit him.

Of course, there is a way to find his particular bug (JSLint, "use strict") that he didn't know about. Still, the point about debugging being a nightmare is absolutely true.

Re: How One Missing `var` Ruined our Launch

#159
post #58
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…

Now there's an argument for syntax highlighting. It also doesn't happen in languages where you must say self.x to access a property/field called x .

Exactly my first thought: "This wouldn't happen with Python".

Re: How One Missing `var` Ruined our Launch

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

Now that I understand what you're saying: This is one of the reasons I hate the "feature" in C++ and C99 that you can declare variables anywhere. 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.

Better to use this feature, but turn warnings on and treat all warnings as errors.
Post reply on HN