News for hackers. Stuff that matters.
Re:Worst Bug Ever by fosap on Monday January 21, 2013 @10:55AM (#5091969)
IMO Matlab is the worst programming language widely used in production.

This perfectly fits my expectations of Matlab:

- i is a mutable, but should be constant

- It has a horrible, non obvious name because mathematicians are used to it. (And they are not used to var names longer than two chars)

- It is in the global scope.

- It's a feature that has a special syntax (sort of).

/edit Apologies for php programmers, maybe php is worse. But I'm not sure.

Re:Worst Bug Ever by nemetroid on Monday January 21, 2013 @11:17AM (#5092077)
> - i is a mutable, but should be constant

It's not really mutable, because it isn't a variable. However, if there is a variable by that name, Matlab will use it instead of the complex constant.

> - It a feature that has a special syntax (sort of).

The problem is rather a lack of special syntax (or too much leniency). Python also has built-in complex numbers:

    >>> 1 + 1j
    (1+1j)
but it doesn't allow you to omit the constant before the j:

    >>> 1 + j
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'j' is not defined
You can do it the same way in Matlab to avoid this problem (1 + 1i is always OK since implicit multiplication is not allowed), so the problem is that Matlab lets you use i syntactically like a variable when it in fact isn't.
Re:Worst Bug Ever by fosap on Monday January 21, 2013 @11:36AM (#5092168)
>It's not really mutable, because it isn't a variable. However, if there is a variable by that name, Matlab will use it instead of the complex constant.

This is what i mean with a sort of special syntax. It's awful corner case.

I don't like the Python syntax either. What would be wrong with Math.C(1,1) for 1+i? Or how about 1 + Math.I ?

Re:Worst Bug Ever by potatolicious on Monday January 21, 2013 @12:09PM (#5092339)
The idea is expressivity - your solution seems boilerplatey and doesn't scale to longer expressions. The idea is to make math expressions as easy to write (and read) as it would be on paper, or at least get as close to it as possible.

I don't have a problem with using i or j as the complex constant, as long as the necessary sanity checks exist to make sure it's not misused (e.g., duplicate declaration of i/j, overwriting i/j, etc).

Re:Worst Bug Ever by andreasvc on Monday January 21, 2013 @12:11PM (#5092356)
Well it is more verbose which matters if you do a lot of complex math. IMHO i, j, l should be avoided as variables anyway, as they are too similar visually; I prefer a, b, c & n, m for loop variables and indices.
Re:Worst Bug Ever by geon on Monday January 21, 2013 @03:56PM (#5093722)
> if there is a variable by that name, Matlab will use it instead of the complex constant.

Sort of like how PHP will treat undefined constants as unquoted strings. Like if that made any sense... At least PHP will trigger a warning if you do that.

Re:Worst Bug Ever by andreasvc on Monday January 21, 2013 @12:14PM (#5092383)
It will only give that NameError if 'j' is in fact undefined; 'j' can be used as a variable which would lead to a similar bug in Python (and why is it 'j' instead of 'i' ... they're both common one-letter variables).
Re:Worst Bug Ever by nemetroid on Monday January 21, 2013 @12:23PM (#5092443)
The difference is that in Matlab, 1 + j is valid as a complex literal and equal to 1 + 1j. In Python it is never valid to write j as an implicit 1j, so this particular bug (a correctly working function stops working because you've put a new name into scope) can't arise.

The use of j is common in electrical engineering, where i denotes current.

Re:Worst Bug Ever by DoubleMalt on Monday January 21, 2013 @12:20PM (#5092417)
Electrical engineers use j instead of i to distinguish it from current (that is commonly referenced by i).
Re:Worst Bug Ever by shardling on Monday January 21, 2013 @01:47PM (#5092949)
Whereas physicists use capital I for current. (And J for the current density.)
Re:Worst Bug Ever by fosap on Monday January 21, 2013 @06:29PM (#5094410)
I have seen j for current density only so far. But physics are convinced you can tell the difference by context (which is usually true, but probably impossible for a compiler, even if physics is "typed" with units). For handwritten text at least my professors managed to come up with at least two different ways to write each letter to avoid confusion. My (not serious) attempts to use Chinese characters haven't been very successful.
Re:Worst Bug Ever by d0mine on Monday January 21, 2013 @07:28PM (#5094676)
It won't lead to the bug: `j` has no connection to `1j`.

The bug described in the article is impossible in Python (and probably in most other programming languages with native support for complex numbers) because `1j` is not a valid identifier.

Re:Worst Bug Ever by jerf on Monday January 21, 2013 @12:26PM (#5092462)
j is the abbreviation used by some disciplines; see the third paragraph of http://en.wikipedia.org/wiki/Imaginary_unit .
Re:Worst Bug Ever by _pmf_ on Monday January 21, 2013 @11:10AM (#5092038)
Having worked with both Matlab and VBA, it's hard to say for me what is worse.

Nowadays, whenever I have to do something (UI, non-numeric data processing) in Matlab, I tend to write a Matlab-shim that calls a Java-component (the Java interaction is fortunately very nice).

The mere fact that one gets things done with less hassle in Java than with a scripting language is a quite damning verdict for said scripting language.

Re:Worst Bug Ever by fafner on Monday January 21, 2013 @06:10PM (#5094330)
Matlab and PHP are very similar in their history. They weren't invented by programming language designers or compiler writers. The creators saw a problem and came up with a makeshift quick solution. Which sucked but was good enough to solve the problem.

Matlab was created to provide a simple way to make FORTRAN Libraries available to non-programmers. And it was quite successful at that. It was quickly picked up by engineers and all kinds of features were piled on top of it.

And the worst thing is that Matlab is a de-facto standard in many areas now. As a webprogrammer you can probably easily avoid PHP by using Python, Ruby, JavaScript, Perl or whatever. But in engineering many people only know Matlab. They don't care about anything else.

Re:Worst Bug Ever by danialtz on Monday January 21, 2013 @12:39PM (#5092550)
I was using a very well-known popular computation software direct competitor to ANSYS Multiphysics. I was using it since early version 3 days, through 4.x over a period of 3 years in my phd in the field of fluid-structure interaction (e.g. fish in flow). The computations usually took over several days on many cpus for 1 second of real-life simulation, making testing quite painful. Hence, many studies do not involve testing like in software field.

I was calculating a parameter enhancement where my calculations were showing a great 180% enhancement; this result would make it into very high ranking (15+ IF) paper right away. By then, several people had written numerous papers on this matter using this multiphysics software in well-known journals. But my gut feeling didn't let me. Eventhough my professors were pressing me to publish, I took 6 painful months (toward the end of my phd) to rigorously test the software itself, having nothing to do with my work, since it was closed-source.

After 6 months, I found out that a velocity term used in their stabilization algorithm does not use relative velocity, but absolute velocity, leading to enormous amount of fake diffusion into the model. I had to travel to another city in europe to convince them that there is such bug.

Every single work in my field that used this software till then had been severely invalid. And what the developers did: they fixed the bug in the next sub-release without putting it in the changelog like nothing happened.

Re:Worst Bug Ever by KMag on Wednesday January 23, 2013 @10:31AM (#5103469)
I don't suppose you were able to publish a peer-reviewed journal article calling previous results into question, were you?
Re:Worst Bug Ever by jballanc on Monday January 21, 2013 @12:12PM (#5092364)
I know the feeling, but I think I have a bug that beats even this one...

Once, I was working on the "alias" keyword in MacRuby, and getting it to work with methods defined in Obj-C. Since MacRuby objects are just extensions on Obj-C objects, some methods are defined in Obj-C and require special casing in order to alias them to a different identifier. For example, if you want to alias "Array#length" to a different name, you'd actually be aliasing "-[NSArray length]".

So I do everything I should need to do to make it work: get a handle to the object, the object's class, the method defined for the class, a selector for the new name...but no matter what I tried, every time I aliased the method, and attempted to call the method by the new name, I would always get the same error: "Method not defined".

Finally, I decided to dive deep with gdb and find out exactly what was going on. To my shock and amazement, I came to realize that I was properly aliasing the method but, due to the nature of NSArray's implementation as a class cluster (and the fact that Obj-C doesn't have proper virtual methods like C++), the implementation of "-[NSArray length]" was literally:

    - (NSInteger)length
    {
      [NSException raise:@"Method not defined." format:@"%"];
    }
Re:Worst Bug Ever by eridius on Monday January 21, 2013 @03:18PM (#5093471)
Are you saying that when a name is aliased in MacRuby, the alias becomes a copy of the IMP from that particular class, such that subclass overrides of that method are never invoked? Because that sounds like a bad idea.
Re:Worst Bug Ever by jballanc on Tuesday January 22, 2013 @10:36AM (#5097545)
Aliasing in MacRuby (or Obj-C for that matter) involves adding a new entry to a class's method table with the new name but a pointer to the old implementation. Method lookup from subclasses to superclasses proceeds the same as it would for any method.
Re:Worst Bug Ever by eridius on Tuesday January 22, 2013 @01:37PM (#5098762)
Yes, but if you message the subclass with the alias, only the superclass's method gets invoked, because the alias apparently contains the IMP of the superclass's method. Or it must, if what you said before is to be believed.

This is pretty nasty, because it breaks the idea of message sends and subclass overrides of methods. There's two ways you could solve it:

1. Set the IMP to a static function that looks up the method it should forward to and re-send the message there. This is hard to do in a manner that supports all arguments, and you'd need at least 2, possibly 3 versions anyway (one for regular, one for strret, and on some architectures, one for fpret). It would probably need to be written in assembly in order to forward the arguments.

2. More recently (in iOS 4.3, and whatever the corresponding OS X release was), the runtime got a nice little function called imp_implementationWithBlock(). This takes a block and returns an IMP. Bill Bumgarner has a nice blog post about it (http://www.friday.com/bbum/2011/03/17/ios-4-3-imp_implementa...). You could use this function on every alias in order to invoke the correct method, passing along the correct arguments.

Re:Worst Bug Ever by jballanc on Wednesday January 23, 2013 @02:07PM (#5104650)
That may be nasty, but that's how the semantics of method aliasing in Ruby work. Try this Ruby snippet to see what I mean:

    class Foo
      def say; puts "hello"; end
      alias shout say
    end
    
    class Bar < Foo
      def say; puts "howdy"; end
    end
    
    Foo.new.say #=> "hello"
    Bar.new.say #=> "howdy"
    Foo.new.shout #=> "hello"
    Bar.new.shout #=> "hello"
Re:Worst Bug Ever by eridius on Wednesday January 23, 2013 @08:18PM (#5106493)
Oh ugh. When is an alias not an alias? When it's Ruby, I guess.
Re:Worst Bug Ever by jasonkester on Monday January 21, 2013 @10:49AM (#5091945)
I had a similarly fun time back in the 90s debugging a VBA script a co-worker had written for an Excel spreadsheet. She was looping through a bunch of time values...

... so she named her variable "time"

... which, when modified, would (naturally) set the system time of the computer.

I'm not sure whether they've fixed that one yet.

Re:Worst Bug Ever by ja27 on Monday January 21, 2013 @11:39AM (#5092188)
At least once a quarter I'd have a student working on some programming assignment on one of our unix-ish systems where they named the executable "test" and couldn't figure out why it wasn't doing anything they expected.

I also got to the point with FORTRAN (yes, I'm old) students where I'd cut them off and tell them to count the columns and make sure each line started with 6 blanks, not 5.

My favorite bug is still some piece of C code buried in a library that was like this:

   if (flag == TRUE) ...
Or there's the old for loop typo (much easier to detect on today's high-DPI screens):

   for(float x=0.0; x < 3,4; x = x + 0.1) ...
Re:Worst Bug Ever by viraptor on Monday January 21, 2013 @12:30PM (#5092493)
I don't get what's wrong with the flag check apart from being a bit too verbose.
Re:Worst Bug Ever by mpyne on Monday January 21, 2013 @01:21PM (#5092803)
TRUE is going to be 1. The flag value might have the value 0x2, 0x4, 0xsomething_else, etc., which will not equal exactly 1 (TRUE).

What you want varies but is probably something like:

    if (flag & FLAG_VALUE) ...
    if (flag) ....
    if ((flag & FLAG_VALUE) == TRUE) ...
that kind of thing.
Re:Worst Bug Ever by geon on Monday January 21, 2013 @03:51PM (#5093691)

    if ((flag & FLAG_VALUE) == TRUE) ...
That would still check if flag is 1, right?
Re:Worst Bug Ever by azth on Monday January 21, 2013 @04:04PM (#5093754)
That is correct.
Re:Worst Bug Ever by mpyne on Monday January 21, 2013 @09:23PM (#5095147)
Indeed, great catch (this is why I use things like QFlags now, as it has a simple "testFlag" method)
Re:Worst Bug Ever by to3m on Monday January 21, 2013 @04:43PM (#5093905)
flag&FLAG_VALUE will be FLAG_VALUE or 0, so unless FLAG_VALUE==TRUE the answer could be wrong.

Suggest (flag&FLAG_VALUE), or, if you need the expression to be of type bool, ((flag&FLAG_VALUE)!=0) or (!!(flag&FLAG_VALUE)).

Re:Worst Bug Ever by mpyne on Monday January 21, 2013 @09:24PM (#5095151)
Very true, I just checked to see if I can edit but I can't, so mea culpa.
Re:Worst Bug Ever by ja27 on Monday January 21, 2013 @09:58PM (#5095262)
Boolean true / TRUE wasn't defined in any standard way before C99. Back in the "olden days", most programmers #defined it as 1, but not always. I ran into one of those "not always" cases in some inherited code.
Re:Worst Bug Ever by anonymouz on Monday January 21, 2013 @04:59PM (#5093982)
I'm curious about the test one. Do you have "." in your PATH? Or do students simply not realize that they should call their program as ./test anyway?
Re:Worst Bug Ever by ja27 on Monday January 21, 2013 @10:01PM (#5095274)
Student accounts had "." in their PATH by default. I'm sure someone did it to keep students from asking why their program wouldn't run at all. Fun times.
Re:Worst Bug Ever by bch on Monday January 21, 2013 @12:21PM (#5092426)
I had this with Perl, using $a or $b for a throw-away script, but Perl uses these internally for sorting. Didn't take -too- long to sort it out, but certainly broke my flow.
Re:Worst Bug Ever by jey on Monday January 21, 2013 @04:13PM (#5093788)

    using $a or $b for a throw-away script, but Perl uses these internally for sorting
Everything I learn about Perl just makes me hate it more. But a lot of people seem to love it -- it must have some redeeming qualities, right?
Re:Worst Bug Ever by draegtun on Tuesday January 22, 2013 @06:27AM (#5096580)
There is nothing to be afraid of here, sort just makes use of localised $a & $b package variable names as references to the sort block:

    our $a = 100;   # using $a package variable

    # then later do a sort on @list
    my @sorted = sort { $a cmp $b } @list;

    # @sorted worked correctly, $a = 100 had no effect on it
    # and $a is still 100
And if we change our to my (so lexical variable) then script will fail to compile giving the fatal error: Can't use "my $a" in sort comparison at...

For more information on sort see: http://perldoc.perl.org/functions/sort.html

PS. The OP problem may have been due to issue/bug in older version of Perl. Also using use strict; use warnings do help highlight issues with code.

Re:Worst Bug Ever by bch on Saturday January 26, 2013 @03:06PM (#5121569)
Indeed.

First 3 lines of all perl code for me are:

  #!/usr/bin/env perl
  use strict;
  use warnings;
Re:Worst Bug Ever by kamaal on Tuesday January 22, 2013 @12:08AM (#5095641)
Everything I learn about the world makes me hate it more. But a lot of people seem to love it --it must have some redeeming qualities, right?

Answer: Yes, depends on where in the world you live.

Re:Worst Bug Ever by Achshar on Monday January 21, 2013 @12:26PM (#5092460)
Are you saying changing the value of a variable in excel changes system time? Isn't that a little... nevermind. What about the privileges required to change time? Did it bypass them or it only worked for admins?
Re:Worst Bug Ever by StavrosK on Monday January 21, 2013 @12:55PM (#5092671)
What privileges? It was Windows in the 90s.
Re:Worst Bug Ever by Achshar on Tuesday January 22, 2013 @05:37AM (#5096467)
Oops, missed that part.
Re:Worst Bug Ever by StavrosK on Tuesday January 22, 2013 @05:40AM (#5096478)
Yes, it was the excellent security model of "let's not bother with security models".
Re:Worst Bug Ever by niggler on Monday January 21, 2013 @12:27PM (#5092471)
Time is a reserved keyword according to MS-VBAL. My guess is that excel itself didn't follow their standard ...
Re:Worst Bug Ever by dpark on Monday January 21, 2013 @10:45AM (#5091923)
>the imaginary constant i in MATLAB was getting overwritten!

The constness of i in Matlab is also imaginary, which is pretty terrible.

Re:Worst Bug Ever by Dove on Monday January 21, 2013 @12:33PM (#5092515)
Yeah, I'm hoping by "overwritten" he really meant "shadowed". I don't know MATLAB, but being able to change the value of i seems both useless and dangerous.
Re:Worst Bug Ever by dpark on Monday January 21, 2013 @04:04PM (#5093751)
I think it's shadowed globally, which has the same effect as it being overwritten. But I'm not certain.
Re:Worst Bug Ever by jstanley on Monday January 21, 2013 @11:27AM (#5092128)
Unlike many of the other commenters, I don't see much problem with having "i" mean the imaginary unit. Where MATLAB falls down though is:

1.) not having lexical scoping

2.) not having warnings when a global is overridden in a local scope

these are pretty inexcusable. For anyone looking for a "better MATLAB", Python has NumPy, SciPy and Matplotlib which are pretty much everything.

Re:Worst Bug Ever by w0utert on Monday January 21, 2013 @12:18PM (#5092410)
>> For anyone looking for a "better MATLAB", Python has NumPy, SciPy and Matplotlib which are pretty much everything.

I hear this at work all the time, "just use Python, we're going to use Python everywhere we use Matlab from now on". It simply doesn't work like that.

Usually it's programmers who are too overly enthusiastic about replacing Matlab with Python, because they are comfortable with all aspects of programming languages. The people typically writing the most complex Matlab code aren't, more often than not they are physicists or mathematicians. Python + SciPy + Matplotlib is not nearly as accessible as Matlab, and you drag in all kinds of typical 'software engineering' problems that don't exist when you stay inside the Matlab IDE and its related tools. I can conjure up a plot in Matlab and manipulate it incrementally for example, while Matplotlib often requires a lot of crazy data mangling and setup code that isn't required in Matlab (trust me, I've spent months re-implementing a library of Matlab plotting scripts in Matplotlib, and the code of some of them is completely untraceable to the original Matlab code, and full of stuff completely unrelated to plotting). Also, lots of Matlab code relies on all the crazy implicit operations and type juggling allowed by Matlab, many of which are almost fundamentally incompatible with a well-defined programming language suited for production code.

This is all even ignoring the fact that Matlab comes with a pretty extensive library of toolboxes that have no equivalent in SciPy or Numpy. If you're really determined and know how to program in Python, you can basically get a lot of stuff done without requiring Matlab, but if you've always been using Matlab like a prototyping tool or a very elaborate graphical calculator (and this constitutes a very extensive part of Matlab users), Python + all its scientific libraries is no substitute.

Re:Worst Bug Ever by jstanley on Monday January 21, 2013 @12:27PM (#5092472)
Good points, and that provides some perspective I hadn't considered.

However, I still think my points about where MATLAB falls down are valid. There is no reason for it not to have lexical scoping, or warnings about when you are doing stupid things. It just doesn't have them, and it causes frustrations like the one in this article.

Re:Worst Bug Ever by w0utert on Monday January 21, 2013 @01:36PM (#5092891)
Oh totally agree, Matlab-the-language is terrible, and Python is a million times nicer to work with. My comment was only about the suitability of Pyhton + Numpy + SciPy + Matplotlib as a Matlab replacement for non-programmers.
Re:Worst Bug Ever by anonymouz on Monday January 21, 2013 @05:06PM (#5094010)
Octave would be a closer equivalent of Matlab, but is also terribly lacking in features compared to Matlab.

And despite not being a fan of closed source software in general, I must say that Matlabs "IDE" is hugely convenient and, for its purpose, far ahead of anything else out there.

Re:Worst Bug Ever by eddins on Monday January 28, 2013 @04:34PM (#5130799)
Can you say more about these complaints? I would like to understand them better.

With respect to complaint #1, based on the Wikipedia definitions of lexical and dynamic scoping, MATLAB has lexical scoping of function variables. Can you say more about what you mean?

With respect to complaint #2, I think it might be based on other comments in this thread. Other commenters have suggested that "i" in MATLAB is a special kind of global constant that is prone to getting unintentionally overridden. That is not the case, however. "i" is simply a function in MATLAB, as are "j" and "pi".

When a user assigns to "i", as in i = 5, it has the effect of creating a variable called "i". The variable "i" shadows the function "i" based on the rule that local definitions of names take precedence over names defined further away. That's a perfectly ordinary and common programming language convention.

If you create a variable "i" by assignment and then call a function, that function has its own set of variables and doesn't see the variable "i" in the caller. There's no global override happening.

Re:Worst Bug Ever by untog on Monday January 21, 2013 @12:57PM (#5092685)
I made a similar error once working in Adobe Air (if I recall):

    for (x = 0; x < thing.length; x++) { }
I didn't put a "var" before the x- so Air used the window.x object, and shifted the entire contents of the window to the right by a seemingly random number of pixels.

To look at it like that, it's blindingly obvious. But it was in the middle of a whole lot of other code, and of course, the for loop ran perfectly fine.

Re:Worst Bug Ever by yuvadam on Monday January 21, 2013 @10:44AM (#5091920)
Nice anecdote, but that's a rather uber-sensational title - not in a good way.
Re:Worst Bug Ever by cfinke on Monday January 21, 2013 @04:36PM (#5093885)
Agreed; I expected it to be the story of Therac-25 (http://en.wikipedia.org/wiki/Therac-25)

It was involved in at least six accidents between 1985 and 1987, in which patients were given massive overdoses of radiation, approximately 100 times the intended dose. These accidents highlighted the dangers of software control of safety-critical systems, and they have become a standard case study in health informatics and software engineering.

Re:Worst Bug Ever by swanson on Monday January 21, 2013 @05:08PM (#5094021)
It's linked to in the first paragraph.
Re:Worst Bug Ever by revelation on Monday January 21, 2013 @10:40AM (#5091903)
Thats not so much a "nice bug" but rather a big fat warning sign for anyone trying to use MATLAB.
Re:Worst Bug Ever by stevoski on Monday January 21, 2013 @11:02AM (#5092007)
Matlab: the language designed so poorly it makes PHP look good.
Re:Worst Bug Ever by dubcanada on Monday January 21, 2013 @11:29AM (#5092138)
I love that you said that, I didn't read my daily dose of crying about PHP today. Thanks!
Re:Worst Bug Ever by electic on Monday January 21, 2013 @11:08AM (#5092024)
This is not the worst bug ever. Nice story, however, the title is way over the top.
Re:Worst Bug Ever by bascule on Monday January 21, 2013 @02:53PM (#5093330)
Yeah, I was thinking more like Therac-25 giving people radiation overdoses or the Patriot missile failures due to floating point roundoff error
Re:Worst Bug Ever by CodeCube on Monday January 21, 2013 @10:51AM (#5091954)
Kind of along the same lines in the sense of being my biggest facepalm moment; many years ago in my first professional programming gig we had full SA access to our sql server. We regularly did troubleshooting via direct SQL statements (a practice I've since shied away from). One particular day, we were troubleshooting a problem in our monthly billing process. I went to delete a few rows and foolishly forgot to include a limiting statement on the WHERE clause, which resulted in most of the data being blown away.

Thankfully, we were able to restore that table from a backup and rerun everything to rebuild the table's state to a correct place ... but suffice to say the experience taught me many things about attention to detail, and how debugging in production should happen.

Good times!

Re:Worst Bug Ever by jmelloy on Monday January 21, 2013 @12:11PM (#5092354)
My biggest production whoops was an update gone awry ... instead of updating one row, updated all 300 million. Spotted it pretty quick, cancelled the update, but then the rollback locked the table and wouldn't allow inserts while it was rolling back ...
Re:Worst Bug Ever by Erwin on Monday January 21, 2013 @11:13AM (#5092055)
A good practice there is to do tricky DELETE/UPDATE inside a transaction. In e.g. Postgres, executing DELETE/UDPATE tells you number of affected rows, so if you see 1243 rows were updated rather than 1 as you expected, you can ROLLBACK.
Re:Worst Bug Ever by bartonfink on Monday January 21, 2013 @11:28AM (#5092137)
Another good approach is to replace 'delete' with 'select count(1)' or something similar. The where clause can remain the same, you know how many rows will be affected and there's less overhead of remembering to set up a transaction, writing an update statement, rolling back, etc.
Re:Worst Bug Ever by CodeCube on Tuesday January 22, 2013 @10:53AM (#5097656)
Yeah, if I ever did have to troubleshoot in SQL directly, I definitely started doing this ... only changing it to update or delete when I was 100% sure it was affecting only what I wanted to affect.
Re:Worst Bug Ever by seanpont on Monday January 21, 2013 @11:35AM (#5092165)
As a mechE, I was brought up on Matlab. We were taught to use ii, jj, kk... as our index variable names. Many years later, I still prefer them to i, j, and k.
Re:Worst Bug Ever by varjag on Monday January 21, 2013 @02:58PM (#5093360)
Like stuttering of a shell-shocked veteran, it's a battle scar of Matlab survivor.
Re:Worst Bug Ever by pfortuny on Monday January 21, 2013 @10:52AM (#5091957)
That is a pretty awful MATLAB perk, using 'i' as the sqrt(-1) (and indexing the arrays from 1...).

It makes polyglot programmers make a lot of silly mistakes. It would have been SO easy to call the imaginary constant just 'I' instead of 'i'...

Re:Worst Bug Ever by TeMPOraL on Monday January 21, 2013 @11:02AM (#5092002)
Or just make it not mutable. Throw a big, fat error when someone assigns value to i.
Re:Worst Bug Ever by chimeracoder on Monday January 21, 2013 @12:05PM (#5092315)
Or better yet, do what Python does. Require a constant in front of the 'i', even if it's only '1'. Since identifiers can't begin with a numeral, there's no way to shadow a valid use of a complex number this way.
Re:Worst Bug Ever by pfortuny on Monday January 21, 2013 @12:53PM (#5092656)
This would have been the best way to learn it, some kind of 'CRASH Eh! MAN your files have been formatted and all this project has been deleted because

i=sqrt(-1)

Sorry for the mess, hope you learn from this...'

But I keep tripping over the same stone...

Re:Worst Bug Ever by KMag on Wednesday January 23, 2013 @10:14AM (#5103385)
Presumably the parent meant to throw an exception and show a script-level stack trace, not silently exit with no diagnostic messages.

Insisting on doing something, even if it's the wrong thing, is much worse in most applications than refusing to run in order to avoid producing incorrect and misleading results.

Re:Worst Bug Ever by jpwright on Monday January 21, 2013 @03:32PM (#5093575)
That would drive mathematicians insane, and this is coming from an electrical engineer's standpoint (where we use j instead)
Re:Worst Bug Ever by nollidge on Monday January 21, 2013 @12:09PM (#5092338)
Just want to say thanks to all the Hyperbole Police in this thread pointing out this isn't actually the worst bug ever.
Re:Worst Bug Ever by swanson on Monday January 21, 2013 @12:27PM (#5092474)
Ya - I was going for a reference to Comic Book Guy: http://en.wikipedia.org/wiki/Comic_Book_Guy

Thought the periods would make it clear. Oh well!

EDIT: Looks like the periods got removed from the title - I can see why that would make the reference a bit more obtuse.

Re:Worst Bug Ever by arielserafini on Monday January 21, 2013 @12:21PM (#5092428)
I honestly don't think this is Hyperbole Policing - people were just expecting a lot more.
Re:Worst Bug Ever by spalletti on Monday January 21, 2013 @10:51AM (#5091955)
it's the most frequent error you can do when writing 'a-little-bit-more' complex scripts using MATLAB. it's sad to say but MATLAB takes too much care of backward code compatibility and they didn't fix this! all matlab's newcomers do that error. :)
Re:Worst Bug Ever by geon on Monday January 21, 2013 @03:40PM (#5093636)
On my first programming job (2004), I was adding shopping cart functionality to a proprietary CMS written in PHP. For some reason I couldn't get a simple item listing to work. I had spent several hours debugging it before I found the problem.

The CMS had a database query result class with a method named something like "numberOfRecords". It returned the number of records minus one...

I told my boss about this, and his answer was "Yeah I know, but we don't want to fix it because a lot of code depend on that bug."

Re:Worst Bug Ever by carlob on Monday January 21, 2013 @10:51AM (#5091956)
I'm baffled by the fact that the single most common name for an integer variable has a special meaning (though it's not a reserved word). I'm pretty sure this is a bug in MatLab, not in OP's code.

But still I was wondering: isn't that code horribly un-idiomatic for an array based programming language?

Re:Worst Bug Ever by jarek on Monday January 21, 2013 @02:18PM (#5093161)
I'd bet i has been used to denote the imaginary unit long before it was used as a loop counter in programming languages. Wikipedia appears to date the former to sometime in the 18th century.
Re:Worst Bug Ever by wereHamster on Monday January 21, 2013 @10:53AM (#5091965)
Oh the joys of side-effects. Wouldn't you love to have a programming language that doesn't have them?
Re:Worst Bug Ever by SoftwareMaven on Monday January 21, 2013 @12:04PM (#5092310)
It would never work for something as side-effect prone as pure mathematics!
Re:Worst Bug Ever by manojlds on Monday January 21, 2013 @10:49AM (#5091944)
If that is the worst bug ever, I would die a happy programmer. Btw, why would anyone use the 3*i form when there is the much clearer 3i form?
Re:Worst Bug Ever by daeken on Monday January 21, 2013 @11:20AM (#5092086)
What if you're dealing in imaginary units that aren't constant? E.g. foo*i.
Re:Worst Bug Ever by manojlds on Monday January 21, 2013 @11:57AM (#5092286)
Ah, right. I am surprised there is not a better syntax for it though.
Re:Worst Bug Ever by Stwerp on Monday January 21, 2013 @12:15PM (#5092388)
It should still be ' foo * 1j '
Re:Worst Bug Ever by lazugod on Monday January 21, 2013 @12:23PM (#5092445)
(foo)i, maybe?
Re:Worst Bug Ever by why-el on Monday January 21, 2013 @12:54PM (#5092664)
Re:Worst Bug Ever by varjag on Monday January 21, 2013 @12:04PM (#5092313)
Fun, but there are worse things than scope shadowing bugs. At least this one is deterministic and can be zeroed in with a few trace prints.
Re:Worst Bug Ever by anonymouz on Monday January 21, 2013 @05:13PM (#5094052)
Python (before version 3) leaks variables from list comprehension, while it seems natural to assume that the comprehension has its own scope. Makes for some funny bugs:

  i = ...
  ... # lots of code 
  [ do_stuff(i) for i in range(n) ]
  ...
  # whoops, our outside i has now been modified
Re:Worst Bug Ever by solox3 on Monday January 21, 2013 @01:49PM (#5092959)
If you call this the worst bug ever (t = [-infty, +infty]), then what's this?

https://github.com/MrMEEE/bumblebee-Old-and-abbandoned/commi...

Re:Worst Bug Ever by paines on Monday January 21, 2013 @11:29AM (#5092140)
That is not what I call "worst" bug. It is a pretty cool bug. Screw Matlab for hiding this from you.
Re:Worst Bug Ever by geuis on Monday January 21, 2013 @10:45AM (#5091924)
Maybe change the title.
Re:Worst Bug Ever by antsam on Monday January 21, 2013 @01:26PM (#5092838)
I spent a few hours debugging an OpenGL program I was working on only to realize that I forgot to account for normalized coordinates. All I had to do was individually multiply x and y by the canvas size.

Always start with the small, obvious stuff first!

Re:Worst Bug Ever by jblock on Monday January 21, 2013 @12:14PM (#5092377)
For some reason, I've found more bugs like this in MATLAB than in any other domain. Ever.
Re:Worst Bug Ever by agumonkey on Monday January 21, 2013 @11:43AM (#5092201)
Colleague had a long long debugging session doing MDA based code generation, turns out JET (java/ibm/eclipse) template engine had a stupid arithmetic bug, the kind you'll only investigate after everything in the stack has been checked.
Re:Worst Bug Ever by Pinckney on Monday January 21, 2013 @11:45AM (#5092213)
Thanks for posting this -- I just went and fixed just such a loop in my own code.
Re:Worst Bug Ever by swanson on Monday January 21, 2013 @12:04PM (#5092309)
Ha! Glad I could help :)
Re:Worst Bug Ever by Stwerp on Monday January 21, 2013 @12:13PM (#5092374)
Me too! I changed my array counter to 'j'. :P

I work in matlab a lot and have had this error creep up several times. It's very annoying, to say the least.

Re:Worst Bug Ever by swanson on Monday January 21, 2013 @12:28PM (#5092482)
The better fix - which I mentioned as well - is to use `2+3i` instead of `2+3*i`. The former will always use sqrt(-1).
Re:Worst Bug Ever by Stwerp on Monday January 21, 2013 @01:23PM (#5092814)
I agree. I think I picked up this syntax from Python. My response was intended as tongue-in-cheek answer since Matlab allows both i and j as the complex number. I mainly see this when editing or modifying scripts written by others in my lab and have to pinpoint their counter usage.
Re:Worst Bug Ever by SaulOfTheJungle on Monday January 21, 2013 @10:58AM (#5091988)
Why did he get the correct results when the script was run stand-alone?
Re:Worst Bug Ever by earless1 on Monday January 21, 2013 @11:31AM (#5092145)
He was passing the numbers directly when running stand-alone. His other code was looping through an array.
Re:Worst Bug Ever by teilo on Monday January 21, 2013 @01:10PM (#5092743)
I don't know what I would do without list comprehensions. I can hardly imagine using iteration loops and counters anymore, and this is one among many reasons why.
Re:Worst Bug Ever by recursive on Monday January 21, 2013 @09:40PM (#5095212)
List comprehensions don't really handle performing a list of actions for their side effects alone.
Re:Worst Bug Ever by teilo on Tuesday January 22, 2013 @11:33AM (#5097886)
The example in the code was not a side-effect. They were creating a new list by invoking a function on every member of the old list.

Here's the same thing re-written in Python:

  d = [compute_diffraction_at_wavelength(x, WAVELENGTH) for x in LensLayers]
Or oldschool Python (and still faster, I think):

  d = map(compute_diffraction_at_wavelength(WAVELENGTH), LensLayers)
Re:Worst Bug Ever by jtanderson on Monday January 21, 2013 @11:45AM (#5092212)
Oh, the things a little syntax highlighting can prevent...
Re:Worst Bug Ever by seanhandley on Monday January 21, 2013 @12:47PM (#5092625)
"those where"

</grammar nazi>

Re:Worst Bug Ever by swanson on Monday January 21, 2013 @01:02PM (#5092704)
Re:Worst Bug Ever by grayrest on Monday January 21, 2013 @10:54AM (#5091966)
Further evidence that j is the one true imaginary unit.

Title is still sensationalist.

Re:Worst Bug Ever by erikb on Monday January 21, 2013 @01:47PM (#5092953)
Who upvoted this?
Re:Worst Bug Ever by martinced on Monday January 21, 2013 @02:34PM (#5093241)
Nice article but there's a lesson here.

First I'll come with an old bug of mine (early nineties). The bug was very hard to reproduce because it was very very occasional (but terrible in consequences). I simply couldn't find it. I knew I was probably smashing some memory somewhere (I was not that experienced in C) but couldn't find where.

So what did I do after weeks of hunting the bug? I moved my design away to an entirely deterministic one and started recording all the inputs the program received and then I could replay them. At one point, logically, the bug happened. And so was it "recorded". Because my program was now deterministic, I could simply feed it the inputs (and the time at which they happened) and, surely enough, the bug was there.

And then finding the cause of the bug was of course trivial.

What's the lesson?

Well, first obviously bugs that you cannot easily reproduce because you cannot "reproduce the state" are typically kinda hard to track down.

The less "state" in your program, the easier it is to reason about your program and the easier it is to reproduce the state.

The lesson?

Functional programming rocks.