Article 6FGQS CodeSOD: Nada

CodeSOD: Nada

by
Remy Porter
from The Daily WTF on (#6FGQS)

In the cinematic classic They Live, the protagonist is a drifter named "Nada"- nothing. Zizek derives much meaning from this, which is an entertaining rant if nothing else.

But Nada appears in other places, like this code found by JMM.

Dim Nada As Boolean = Falsecn.Open()Using r As SqlDataReader = rs.ExecuteReaderIf r.HasRows.Equals(False) Then Nada = TrueEnd Usingcn.Close()Return Nada

As bad VB .NET code goes, this is pretty mild. But like Zizek, we can overanalyze it and find more meaning.

First, we start with the... colloquial variable name, Nada. Again, we expect it to represent nothing, but it does not represent nothing. It actually represents something quite important: are there, or are there not, records in our dataset? To describe this a nothing, as a void, as something without meaning, is both misleading and confusing.

Then we see the act of opening cn, our connection to the database, and then later on closing it- with no error or exception handling along the way. If anything goes wrong in this process, we don't clean up our open connection. This could be solved if only they used the Using keyword to create the connection- which they didn't. But it would guarantee that the Close function gets called without requiring us to do it explicitly, even if an exception happened.

But the real "meat" of this WTF is the tortured logic of its main line: If r.HasRows.Equals(False) Then Nada = True

This is a standard kind of anti-pattern, one which we see a lot of. A sign that someone doesn't understand boolean values, and what they truly mean. It could be expressed more simply as Nada = Not r.HasRows, or we could simply Return Not r.HasRows.

But there's a hint, here, of something even deeper- we run a query to see if it has rows, which implies that we're requesting actual data from the database; data which we do not need or use. The correct query here would be a COUNT, which would return one row, and then we check the contents of the COUNT to see how many meaningful rows are in the database.

We find ourselves staring at code which names its variables after the void, but what we see upon examination is that the true void is the mind which created this code; empty, seeing nothing, signifying nothing, meaning nothing- nothing but WTF.

buildmaster-icon.png [Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments