Live data from Hacker News

How One Missing `var` Ruined our Launch

blog.meloncard.com

41–50 of 223 posts

Re: How One Missing `var` Ruined our Launch

#41
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?

He means declaring.

Re: How One Missing `var` Ruined our Launch

#42
post #22

> I would posit here that nothing I could do in best practice (manual front-end testing, unit testing, error handling, etc.) would have caught the offending line. jshint would have caught it. You need to run jshint on your code or you will get silly errors like this. Simple.

Indeed, JSHint is designed to catch those kind of mistakes. Here is the offending code and its JSHint report: http://www.jshint.com/reports/57010

Re: How One Missing `var` Ruined our Launch

#44
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?

Yes, sorry, I meant declaring. Wrote that post in a hurry as I headed out the door, I wasn't paying too much attention.

Re: How One Missing `var` Ruined our Launch

#46
post #30
post #24

Earlier quoted context omitted.

Even better is setting your editor to run JSHint when you save a .js file, and let you know if there are problems. Not only does it avoid stupid bugs, it saves time round-tripping to the browser for trivial issues like syntax errors.

Sounds like a good idea. I know emacs flymake mode can be set up to underline problems detected by jshint. Personally I like to have the tests and jshint run by a hotkey so I can happily move the code through invalid states (towards a valid goal) without being constantly complained at :)

I wrote a jshint mode for emacs

https://github.com/daleharvey/jshint-mode

Re: How One Missing `var` Ruined our Launch

#47
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.

Try using the version I linked rather than the original. It has a bunch of improvements including how it handles indenting functions.

I've done some moderately complicated jQuery development with it, as well as some node.js stuff for fun, and have had no issues in either case.

Re: How One Missing `var` Ruined our Launch

#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 ;

Re: How One Missing `var` Ruined our Launch

#49
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 ..

Yeah, that's all he needed. Making it a comment isn't necessary either.

Maybe Node should be strict by default? I can understand it not being the case for browsers that need to support legacy code, but for Node it doesn't really need to care about that.

Re: How One Missing `var` Ruined our Launch

#50

Do I understand this right -- global variables are shared across all requests in node.js? If so, that is an insane design.

It's not really a fault of node so much as a fault of JavaScript. While JavaScript is a beautiful and expressive language, it does have some annoying pitfalls; this is just one of them.

Happily, this is all changing--in the long run, future versions of JavaScript should polish away these issues while maintaining the fundamentally sound core of the language. In the short run "use strict" lets you realize some of the upcoming improvements in otherwise legacy code.

Post reply on HN