
Shit programmers write - mat-mcloughlin
http://shitprogrammerswrite.com/
======
collyw
A lot of these seem to be attempts to fit into existing "best practices". Unit
tests that prove nothing. Boilerplate Java code that does nothing.

Maybe we should reevaluate some best practices. I have debated before on here
that many unit tests seem useless as the units are too small, and you
essentially end up testing your language or framework which you already know
works. Integration testing on the other hand makes a lot more sens.

When I started with Java, it seemed appropriate to put hundreds of getter /
setter methods. Is there much advantage to that over allowing the variable to
be accessed directly? (Perl felt very strange at first having getter and
setter methods combined as one method).

As I get more experienced as a developer, my skill set has grown but my coding
becomes simpler. Don't use every language feature to show how knowledgeable
you are. Use it when appropriate, and when it makes the code more readable /
reusable / simpler. Sometimes a higher level abstraction is more difficult to
understand , but is overall better choice. An example would be a map as
opposed to a for loop. Less chance of side effects in a map, as we don't have
to track the iterator variable, but a map is not as intuitive for less
experienced coders.

~~~
sanderjd
I always feel happiest writing unit tests for _functions_ that contain non-
trivial _logic_. But I always feel sad when testing methods that mostly just
introduce side effects. It seems like when I'm running into more of the
latter, it is often worthwhile to find ways to refactor toward having more of
the former, but sometimes not.

~~~
wlievens
True. I find unit tests the most useful and economical when I'm testing
algorithms, rather than data bookkeeping or user interfaces.

------
sbierwagen
Little counterproductive to censor the submission title when the domain name
is right next to it.

------
mmahemoff
More frequently updated: [http://thedailywtf.com/](http://thedailywtf.com/)

~~~
flatline
A few of these are funny/wrong, but most just look like laziness or some minor
confusion about language/framework specifics. Thedailywtf definitely has
better content.

------
ep103
public bool ShowOptional() { bool bolReturn = false;

    
    
        return bolReturn;

}

The above actually seems reasonable if you assume the programmer that wrote it
was intelligent. I'm imagining that the above code is called multiple times by
the application, for a new optional feature in development. Currently, they
haven't developed the feature, and so we are always returning false. But in
the future, we likely will want to show the feature given some condition. So
this programmer has (hopefully) decided that (s)he will write the current code
to take this future development into account, and rather than pass some
boolean or config value throughout the code, has isolated it to one specific
method.

Now, when they go to implement Optional, whatever that is, that developer can
just update this one method with the expression, and go about coding their
feature without any knowledge of the previous code base, which is exactly
what's supposed to happen.

~~~
michael_h
Why have a variable then?

    
    
      return false;
    

Done.

~~~
pmelendez
I used to write this kind of snippet when creating the skeleton of a function,
this seems like a leftover that was over-sighted in the code review (assuming
that there was a code review at all)

------
MattBearman
Love this one:

    
    
      return foo == null ? null : foo;

~~~
wreegab
This reminds me of this one, which is quite common:

    
    
       return foo == null ? true : false;

~~~
adamors
I sometimes do that in PHP because I hate the equivalence between "", false,
null, '0' and 0. Seeing true and false written out makes it more readable.

~~~
wreegab
The result of "foo == null" (or better "foo === null") _is_ boolean, I don't
know what is the "equivalence" problem here.

~~~
sixothree
Maybe this is a programmer who likes to spell things out for future readers.
It's easier to convey meaning with return foo == null ? true : false;

~~~
wreegab
Let's picture a future reader:

"Hmmm, if 'foo == null' is true, than it's true... I see..."

Seriously..

~~~
jebblue
From a maintenance perspective, it's a bit more involved than that. Returning
the explicit evaluation result says, "I do care what foo is and these are the
only valid possible return values after expression evaluation, today just
return null but in the future we might return something different, either way
the return value at this point is critical and needs to be explicitly
considered and processed".

------
smikhanov
If this:

    
    
      function nop()
      {
          if (Math.random() == 0.1234567890)
          {
              for (var i = 0; i < arguments.length; i++)
              {
                  console.log(arguments[i]);
              }
          }
      }
    

wouldn't have been written in JavaScript, my guess would be that this is an
attempt to create an empty function (no-op) and prevent it from being
optimized away by the compiler. Still unclear who may need to call it.

------
hrktb
> _if (Session[ "startDate"] + "" == "")_

This feels horribly familiar. I'm not sure to recognize the specific language
used, but there must be cases where one would like to target empty of filled
with non processable characters strings only, and let null and falsy values
pass through. This kind of use would typically need a line of comment, but
hey...

~~~
goldenkey
I'm guessing you don't realize that the plus operator has less precedence than
the == operator.

That if is equivalent to if (startDate + true)

EDIT: I was wrong. I haven't slept.

~~~
krcz
Does it mean if(1 + 1 == 2) is equivalent to if(1 + true)?

~~~
kirab
1) + has a higher precedence than ==

2) even if == had a higher precedence, then 1 == 2 would still be false :D

------
fdej

        if (Math.random() == 0.1234567890)
        {
            for (var i = 0; i < arguments.length; i++)
            {
                console.log(arguments[i]);
            }
        }
    

Reminds me of the code I have to insert to prevent overzealous compilers from
completely optimizing away my benchmark loops.

------
pmelendez
I haven't slept well lastnight (only 4 hours) so please bear with me... but
what is the joke on this snippet?

"I Don’t Know".ToJson();

    
    
        public string ToJson()
        {
            var s = new StringBuilder("{");
            for (var i = 0; i < CustomField.CustomFieldOption.Count; i++)
            {
                var item = CustomField.CustomFieldOption[i];
                s.Append("\"" + item.CustomFieldOptionId + "\"");
                s.Append(":\"" + item.OptionName + "\"");
                if (i < CustomField.CustomFieldOption.Count - 1)
                {
                    s.Append(",");
                }
            }
            s.Append("}");
            return s.ToString();
        }

~~~
mephi5t0
I assume item itself is missing, so nothing to convert... My guess

~~~
potatolicious
Been a while but:

    
    
      var item = CustomField.CustomFieldOption[i];
    

Shouldn't that be the item to convert? This looks like something stuck onto a
class to serialize to JSON.

I might be missing the joke also :(

------
pron
I wonder how much of that is due to problems in documentation/documentation
discoverability. This seems precisely the kind of data that we need to make
docs better.

~~~
npsimons
Or, you know, write programming languages that don't suck. See also
[https://www.destroyallsoftware.com/talks/wat](https://www.destroyallsoftware.com/talks/wat)

------
JoeAltmaier
The one about if (condition) {...} else if (!condition) ... May actually have
a point, if 'condition' is a boolean which can be true or false or undefined.

------
jtheory
For the "Siamese HTML Document" \-- that one rings a bell... I swear there was
a bug in one of the ancient horrible versions of IE that required you to
include your "no cache" tags AGAIN in a second head tag, if you wanted to use
http-equiv tags instead of real HTTP headers.

I'm pretty sure there hasn't been reason for anyone to do that for a decade or
so, though.

------
slaman
Is anyone else as infuriated as I am that you can't actually read any of the
code?

Maybe I'm missing a button somewhere, but I needed to open up the DOM
inspector or RSS feed to read every single one.

Unacceptable

------
tkellogg
A lot of these seem to be Microsoft technologies like C# or SQL server. Is
that because the owner is more familiar with the MS stack? Or do MS devs have
more to complain about?

~~~
danabramov
From my experience, such code is not specific to MS technologies, but is
indeed often found in “enterprisey” code, a lot of which just happens to be in
Java and C#. And enterprisey code is like that because it's often outsourced
to the lowest bidder.

I once had to _print_ a method that took 50 pages of paper, so I could
understand what it does, and after 15 minutes I realized it's the same 60
lines repeated over and over with different conditions. The guy who wrote it
had no idea he could have extracted a method.

~~~
guardian5x
Just curious: Why does printing it on paper helps you to understand code
better than reading it on screen?

~~~
danabramov
It helps to understand the structure. I was looking for repetitions (and
luckily I found a lot), but I needed to look out for things that change
between repetitions.

I just found the picture I took that day. The boxing gloves just happened to
be in the same conf room.

[http://i.imgur.com/bQCA08C.jpg](http://i.imgur.com/bQCA08C.jpg)

~~~
hamburglar
Looking as close as I can at those printouts, my only reaction is, "that's
CODE?!" Good lord.

------
walshemj
Oh I thought the shit programmers write was stuff like "its; 90% complete" or
"thats not a bug its a feature"

