tags:

views:

1569

answers:

14

List examples of programmers doing stupid things in their code.

I just came across this little baby in PHP:

if(strpos($row['themename'],"some string") !== false){

This is wrong on so many levels. First, there is the double negation in saying 'not equals false'. Then there's the explicit checking of truth (why not do ($something == false) == true while you're at it?) and finally there's the calling of strpos for a simple string comparison.

I thought it would be fun to see what other examples people can come up with.

+7  A: 
#define TRUE (1)
#define FALSE (!TRUE)

The author of this C++ gem managed to define TRUE and FALSE as (a) different types, with (b) different sizes.

Genius!

And (c) the semantics.
leppie
You usually see this in legacy code that was converted to C++ from C.
jmucchiello
I've seen some really crazy stuff when stdbool is missing but that takes the cake.
Tim Post
`#define TRUE FALSE // have a nice debug, scumbags` (from a fired employee)
abatishchev
+7  A: 

Probably anything people have written and put live at 5pm on a Friday!

Rob Wells
damn...did that today!! :)
kenny
+16  A: 

How about http://thedailywtf.com/?

Enough stupidity to procrastinate over for a decade in there.

Ishmaeel
But enough about the forum users...
Geoffrey Chetwood
Well, thedailywtf is more about stories, this is more of snippets.
Ubersoldat
Theres's a post category for snippets: [CodeSOD](http://thedailywtf.com/Series/CodeSOD.aspx) (Snippet Of the Day).
intuited
The comments are the best part. I stopped reading them because it was giving me an aneurism.
David Lively
+23  A: 

I had to explain this one to so many students I lost count.

if(condition == true) {
    return true;
}
else {
    return false;
}
Bill the Lizard
Occasionally this pattern can be useful - when debugging it lets you set a breakpoint only on one branch of the if (presumably the less frequent one).
Rob Walker
Some IDE's (I only know for sure about Eclipse) allow conditional breakpoints, i.e. only stop if condition == false, which is surely preferable :-)
Grundlefleck
Although the pattern might be useful for setting breakpoints, the first part is not excusable: if (condition == true)....
Carl
Carl, yes, it was really both parts I was illustrating. Rob, that's an excellent point, but I would probably fix this after I was finished debugging.
Bill the Lizard
Isn't this Joel's favourite? (-:
Rob Wells
Yes, the `if (booleanVariable == true)` (or `== false`) makes my teeth hurt! But there are many the code we have at work, and those writing them (among them the team leader!) says it is more readable...There is also the variant without the else...
PhiLho
what about if it's a nullable bool? so it can have values true, false and null.
endian
I think the only instance this is useful is if you are logging somthing different before returning...
Omar Kooheji
what, no comments on the code? how am i supposed to understand that logic? hehe
melaos
Why the else?if (condition == true) return true;return false;
Tim Post
@tinkertim: There is no need for an if statement. You can just do a return condition
mdec
@mdec: What if condition was an int?
nilamo
oh my gosh you are breaking the "one exit point" rule!!!!!!!
Joe Philllips
I see this every day in production version of electronic commerce software of fortune500 company
abatishchev
@Tim: `return (condition == true) ? true : false;`
abatishchev
+17  A: 

Uhm, it doesn't do 'not equals false' what it actually does is checks the expression and makes sure the result is not of type boolean false, not converted to boolean false as it would be if $row['themename'] was located at the start of "some string"

EDIT: Better explained here and here

Edit 2: In lieu of Graemes comment, to answer this question, I would like to claim this question as an example of a programmer doing something silly =]

mdec
This shouldn't be an answer, it should be a comment on the original question.
Graeme Perrow
+1 for checking the documentation.
Bill the Lizard
There's always that too. Humour's a better excuse though.
Matthew Scharley
You mean "in light of Graeme's comment". I think. ...:~/
intuited
+17  A: 
try
{
    // do something
}
catch (Exception ex)
{
    if (ex is FileNotFoundException)
    {
        // blah
    }
    else if( ex is InvalidOperationException)
    {
        // blah
    }
    else if( ex is AnotherException)
    {
        // blah
    }
    // etc.
}
Romain Verdier
This is in production code where I work!! ;)
Christopher
At least, it catches exceptions...
Romain Verdier
You're better off catching individual exceptions imho
Kris
Kris, of course you have to catch individual exceptions, but using an IF-ELSE is very stupid.
Ubersoldat
Ha-Ha-Ha
Joe Philllips
+5  A: 

I've seen this used:

someVariable = GetObjectByName(ThatObject.Name)

in this sort of structure:

for i = 1 to 6
   select case i
   case 1: 
     dostuffwith(obj1)
   ..
   case 6: 
     dostuffwith(obj7)
   end select
next

And let's not forget the classic:

someFlag = True;
if(someFlag == False) 
{ 
   someFlag = True;
}

I, of course, saw the obvious problems in the above and refactored it:

for(someFlag = True; someFlag == False; someFlag = True);

After all, the original code fragment would be wrong if the assignment operation were to fail twice. My patented method allows for infinite failure.

Trevel
switch within a for loop... I've see that where I work.
Christopher
Switch within a for loop has one (and only one purpose). In a testing program where you can say: execute tests 3 through 7. The for loop then starts at 3 and run test the tests in order. That was the only time I've ever seen that make even a little sense.
jmucchiello
duff's device, anyone?
David Lively
+31  A: 

Somebody wrote a loop to find out the size of an array:

int size = 0;
for (int i=0; i<array.length; i++) {
    size++;
}

That's stupid on so many levels its unbelievable.

martinus
That is amazingly silly. I'm having trouble imagining the thought process there.
Neil Aitken
me too, I think he somehow got this by refactoring and removing lots of code that originally were inside of the loop.
martinus
or ... it's just dumb.
kenny
This is why I browse old SO questions and answers - I have just been rewarded with a good laugh! Luckily I've never dealt with code that dorky in real life. So far...
DarenW
+4  A: 
return i == 7 ? true : false;
Hao
This is actually an improvement on my answer above. :)
Bill the Lizard
return i == 7;
Joe Philllips
+1  A: 
double val;
...
if(val == 0.1) {
  //do something
}else{
 //do something
}

assuming real number and floating number is the same thing subconsciously.

jscoot
+5  A: 
try
{
    // something
}
catch (Exception ex)
{
    throw ex;
}

Brilliant... you've added 7 lines of code that do nothing except corrupt the stack trace of the exception.

Greg Beech
that's not stupid at all, depending on what //something is... catching and rethrowing of exceptions can be a very clever help in complex exception handling.
tharkun
@tharkun - I think you're missing the point. All this does is catch an exception and rethrow it, without doing anything else. If you're logging, translating, etc. then it isn't stupid, but as written above it is.
Greg Beech
I assume the point is use 'throw' and not 'throw ex'
kenny
depending on the language (java) you just set the method as "... throws Exception" and let the parent class deal with it
Joe Philllips
This does worse than "catch an exception and rethrow it without doing anything else." [throw;] rethrows an exception "without doing anything else." [throw ex;] throws ex at that point, which means the stack trace starts at that point. If you have A() calling B() calling C(), and C does a throw ex, all you'll see in the stack trace is A(), so you won't know where the error happened.
Kyralessa
@Kyralessa - Yes I know. I said that in the answer. That was the whole point of the answer. You're taking that quote out of context; it was replying to tharkun with the meaning that the block does not have any other functionality as written.
Greg Beech
To someone who writes "throw ex" and doesn't know why it's bad in the first place, "corrupt the stack trace" is not likely to have any meaning.
Kyralessa
We used this (and other tricks) to hide internal exceptions and stack trace to the final user, so not to reveal inner working details of our framework.
Stefano Borini
@stefano if THAT is how you're hiding internal workings of your framework from end users, you should consider another career.
David Lively
@David : I don't see why.
Stefano Borini
@Stefano I should have phrased my comment in a more friendly manner; for that I apologize. I think that validating input into your classes and returning errors or raising exceptions accordingly should be the preferred way to report errors back to the host application, which may have been what you were saying. I guess I can understand where this would be desired, as long as the framework/library isn't using exceptions for flow control. If the user is, say, providing an invalid connection string, returning an exception from a class than the database connection object would be cprrect.
David Lively
+3  A: 

I found one time this:

if (someBoolean != false) {
    // do something
}

And another time, from same author:

if (true != false) {
    // do something
}
Patrizio Rullo
Uhm, the second one is a valid pattern to check whether the code runs in a logical universe.
phresnel
+1  A: 
IF NOT EXISTS
    SELECT 1 FROM customer WHERE customer_id = @customer_id
BEGIN
    INSERT INTO customer
    (stuff, fluff, foo, bar, top, secret, column, names, but, still, funny)
    SELECT stuff, fluff, foo, bar, top, secret, column, names, but, still, funny
    FROM customer
    WHERE customer_id = @customer_id
END
David V McKay
A: 

/* Copyright (C) 2005 Free Software Foundation, Inc. This file is part of the GNU C Library.

The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */

ifndef _FEATURES_H

error "Never use directly; include instead."

endif

ifndef _PREDEFS_H

define _PREDEFS_H

/* We do support the IEC 559 math functionality, real and complex. */

define STDC_IEC_559 1

define STDC_IEC_559_COMPLEX 1

endif /* predefs.h */

um, why would you need to put that in it's own file?

noah