News for hackers. Stuff that matters.
Re:How One Missing `var` Ruined our Launch by untog on Monday October 31, 2011 @07:03PM (#3179562)
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<variable.length;x++) {
    // blah
    }
I wasn't declaring the x variable, so it was using the x part of the x/y positioning of the UI container. Woops.

edit: initialise/declare brain freeze

Re:How One Missing `var` Ruined our Launch by swanson on Monday October 31, 2011 @07:17PM (#3179628)
Ha, that reminds me of a program I was writing in MATLAB. I used `i` for a loop index in one part of the script and then later when doing some complex number calculations (`2 + 3i`)...it was one of those so-stupid-you-have-to-laugh moments when I finally figured it out.
Re:How One Missing `var` Ruined our Launch by joeyo on Monday October 31, 2011 @10:21PM (#3180282)
It's for this precise reason that I use 1i rather than i when doing complex arithmetic in matlab.
Re:How One Missing `var` Ruined our Launch by ajross on Tuesday November 01, 2011 @11:45AM (#3182591)
Wait... matlab allows "AB" to be inferred as "A*B"? How is that not a disaster? Parsing a token that doesn't even exist in the lexer stream? Even C++ wouldn't try that.
Re:How One Missing `var` Ruined our Launch by gujk on Tuesday November 01, 2011 @12:04PM (#3182687)
1i is special, sort of like 1d or 1f in C. The i refers to the imaginary number datatype. i also can be reassigned, which is a horrible design bug in Matlab.
Re:How One Missing `var` Ruined our Launch by finnw on Monday October 31, 2011 @08:36PM (#3179900)
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.

Re:How One Missing `var` Ruined our Launch by Luyt on Tuesday November 01, 2011 @04:44AM (#3181190)
Exactly my first thought: "This wouldn't happen with Python".
Re:How One Missing `var` Ruined our Launch by joesb on Wednesday November 02, 2011 @03:27PM (#3188523)
Only for case of instance variable. Python still has nested function, non-instance variable, and implicit declaration.

Only it is worse in Python because, lacking `var` keyword, you will shadow the variable in Python

Re:How One Missing `var` Ruined our Launch by cperciva on Monday October 31, 2011 @07:39PM (#3179701)
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 by untog on Monday October 31, 2011 @08:00PM (#3179793)
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 by da5e on Monday October 31, 2011 @09:06PM (#3180007)
Hmm, so you wrote a careless typo while describing your "careless" coding? Not to be harsh, but maybe it's a hint?
Re:How One Missing `var` Ruined our Launch by untog on Monday October 31, 2011 @09:21PM (#3180058)
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 both our programming and our writing.

Re:How One Missing `var` Ruined our Launch by da5e on Tuesday November 01, 2011 @08:04PM (#3184736)
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 by VBprogrammer on Monday October 31, 2011 @09:26PM (#3180082)
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.

Re:How One Missing `var` Ruined our Launch by da5e on Tuesday November 01, 2011 @08:19PM (#3184783)
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.
Re:How One Missing `var` Ruined our Launch by sometimes on Monday October 31, 2011 @09:49PM (#3180174)
> 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.

on the other hand, those of us who have been around the block enough times have almost certainly encountered co-workers who were bright, talented, cute, whatever - but at the end of the day were just ... sloppy ... lazy ... careless ... in their approach. It's a personality thing - they had nice childhoods, they never learned to by hyper-vigilant and paranoid, and as a consequence... they write buggy code that can't be trusted.

Re:How One Missing `var` Ruined our Launch by Vivtek on Monday October 31, 2011 @10:29PM (#3180312)
"...never learned to by hyper-vigilant..."

My eyebrow just raised so far my forehead cramped.

Re:How One Missing `var` Ruined our Launch by cperciva on Monday October 31, 2011 @11:36PM (#3180505)
Muphry's law.
Re:How One Missing `var` Ruined our Launch by sometimes on Tuesday November 01, 2011 @12:18AM (#3180606)
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 by untog on Tuesday November 01, 2011 @01:02AM (#3180705)
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, gone back and fixed it? 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.

You can live in a world where everyone does everything perfectly, every time (and pay the price when you inevitably don't) or you can set up systems with unit testing, user testing, and- yes- developer testing that results in bugs being dealt with in a timely manner before a single end-user sees anything.

But hey, each to their own. Whatever works for you. If you get it right first time, every time, then you are a better programmer than I, and I congratulate you on it.

Re:How One Missing `var` Ruined our Launch by da5e on Tuesday November 01, 2011 @08:11PM (#3184761)
>>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 by Permit on Monday October 31, 2011 @07:56PM (#3179778)
He means declaring.
Re:How One Missing `var` Ruined our Launch by cperciva on Monday October 31, 2011 @09:52PM (#3180183)
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.
Re:How One Missing `var` Ruined our Launch by gnaritas on Monday October 31, 2011 @09:56PM (#3180193)
And much easier to wind up with lots of bad dead code or worse, reusing variables over and over leading to tightly coupled code. IMHO, variables should be declared as closely as possible to where they're used in the smallest scope possible. C languages do it right.
Re:How One Missing `var` Ruined our Launch by JoshTriplett on Tuesday November 01, 2011 @12:00AM (#3180566)
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 in the middle of a block. I primarily use the C99 feature of declaring variables as part of a loop.

Re:How One Missing `var` Ruined our Launch by euccastro on Tuesday November 01, 2011 @06:06AM (#3181377)
(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 of declaring a variable in the narrowest scope that makes sense. I can't see much of a problem with this (esp. in the context of C!) barring old habit. I don't think Scheme programmers, for example, have this stuff any easier and I don't think scoping is perceived as a problem there.

Re:How One Missing `var` Ruined our Launch by JoshTriplett on Tuesday November 01, 2011 @04:01PM (#3183897)
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 by colanderman on Tuesday November 01, 2011 @09:21AM (#3181836)
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.
Re:How One Missing `var` Ruined our Launch by gujk on Tuesday November 01, 2011 @12:07PM (#3182695)
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.
Re:How One Missing `var` Ruined our Launch by JoshTriplett on Tuesday November 01, 2011 @04:02PM (#3183900)
They did, in C99.
Re:How One Missing `var` Ruined our Launch by Peaker on Wednesday November 02, 2011 @08:54AM (#3186414)
That makes variables scope over far more than they need to, opening up a pretty frequent bug avenue of using the wrong variable that you did not intend to use.
Re:How One Missing `var` Ruined our Launch by vonkow on Monday October 31, 2011 @07:26PM (#3179662)
Classic! I did that with rotation in my js gaming engine once, fun times.
Re:How One Missing `var` Ruined our Launch by Veera on Monday October 31, 2011 @11:50PM (#3180549)
exactly what happened to me. I had two for loops defined in my JS code which used the variable "i" without declaring. Hence, when the code ran, the second for loop behaving weirdly. It took sometime before catching the bug.
Re:How One Missing `var` Ruined our Launch by napierzaza on Tuesday November 01, 2011 @10:10AM (#3182080)
PHP experience can poison development in other languages.