Check out a Classics republish of one of our most read articles from 2014. Enjoy!

When you read a lot of bad code, you start to get a sense of why the code exists. Often, it’s ignorance- of the language, of the functional requirements, of basic logic. Sometimes, it’s management interference, and the slavish adherence to policy over practicality. Other times, it’s just lazy or sloppy work.

And sometimes, the mysterious logic that gave birth to a WTF is just that- a mystery.

Timo can’t help but wonder why this method exists:

public DataModel getEditionModel() {
   if ( true )
       throw new IllegalArgumentException( "You shouldn't be here" );
   return editionModel;

Angela is still puzzling over this one:

String timeStampLength = "                          ";
int lengthOfTimeStamp = timeStampLength.length();

Can you imagine a clearer way to express a numeric length?

Dennis found some code that needs to check the length of an array, so it does this:

function countDocuments() {
    var count = 0;

    for ( var i = 0; i < user.documents.length; i++) {

    return count;

If only there were a built-in method that could tell us the length of an array...

And finally, Andrew sends us this example of defensive programming, that’s about as safe as we can make it:

Private Sub ImageList_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ImageList.DataBound

    Catch ex As Exception
        If TypeOf ex Is ArgumentOutOfRangeException Then
            Throw New Exception("item not found in the list...")
        End If
    End Try
End Sub
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!