Article 4R25K CodeSOD: Do You Need this

CodeSOD: Do You Need this

by
Remy Porter
from The Daily WTF on (#4R25K)

I've written an unfortunate amount of "useless" code in my career. In my personal experience, that's code where I write it for a good reason at the time- like it's a user request for a feature- but it turns out nobody actually needed or wanted that feature. Or, perhaps, if I'm being naughty, it's a feature I want to implement just for the sake of doing it, not because anybody asked for it.

The code's useless because it never actually gets used.

Claude R found some code which got used a lot, but was useless from the moment it was coded. Scattered throughout the codebase were calls to getInstance(), as in, Task myTask = aTask.getInstance().

At first glance, Claude didn't think much of it. At second glance, Claude worried that there was some weird case of deep indirection where aTask wasn't actually a concrete Task object and instead was a wrapper around some factory-instantiated concrete class or something. It didn't seem likely, but this was Java, and a lot of Java code will follow patterns like that.

So Claude took a third glance, and found some code that's about as useful as a football bat.

public Task getInstance(){ return this;}

To invoke getInstance you need a variable that references the object, which means you have a variable referencing the same thing as this. That is to say, this is unnecessary.

otter-icon.png [Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today! TheDailyWtf?d=yIl2AUoC8zAYfcJZC5kDt4
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