Garbage Collection

I’ve seen my fair share of code; some written by me, some written by vast teams; and a lot in-between. And I’ve learned many of ways to spot Garbage. That is, rubbish code. Code that will bring you pain and will be the source of ‘Magic’ to the less experienced.

And I’m going to share with you a simple trick to help you spot it; meaningless class names.

Granted this is not new, Steve McConnell of Code Complete fame extols the virtue of good class names as do, no doubt, many others. But where they tell you to think hard about naming your classes, I give you the taxonomy of other people’s smells. And we all prefer to gloat than code carefully…so hear goes.

Manager

Any class with manager in it is a sure sign it was written in a hurry to fill some gap. Manager? What is it managing? Is it a collection? Does it construct objects? Dose it run salary reviews for it’s managed objects? Who knows?

If you see ‘managers’ in your code ask yourself if what they actually do could be expressed more clearly. Replace ConnectionManager with ConnectionPool, ConnectionLookupTable, OpenConnectionSource or (if you must) ConnectionFactory. Each of the later class names gives you a better idea of what the ‘Manager’ actually does.

Helper

Any class with helper in it is a sign that it was written to do some random look-up or complex object manipulation. This is sure sign that the Object hierarchy sucks in some profound way or that similar concepts are implemented differently.

Look in classes called helper and try and see why you need their help. Then change your class structure so you don’t need it.

Handler

Similar to Manager and acceptable in event handle nomenclature, but not for a class. Way too vauge.

Executor

Another meaningless ‘do-something’ word I’ve seen a few times. Typically used to name classes that should be called  something like ThreadPool, MessageDispatcher or BackgroundDownloader.

Manipulator

Usually comes in the form of DataManipulator. Great indication that similar but not identical structures have been used to model the same thing in different places.

Object

This one really confuses me. But I’ve seen EmailMessageObject and the like. ‘Object’ is totally redundant, we know it’s an object. I’ve not seen a class with Class in its name yet but I’m sure I’ll find one.

Array

Every time I’ve seen a MessageArray or similar the class itself has implemented some odd data structure that was definitely not and Array. If it really is an array, you don’t need a class to represent it. And it if your ‘Array’ is doing something odd see if it fits into an existing Collection type there are many to choose from.

Incidentally the one I see most often re-implemented is dequeue which actually does not have a .Net standard library implementation, which is a pity, but LinkedList is semantically very similar.

Data

Data Access Layers abound and having a class called DataAccessLayer is OK, I guess. Buy you only get one! Every other class should specify what data it intends to represent or operate on.

OK mini-rant over.

If you have any other rules of thumb or you think mine a themselves rubbish I’d like to hear.

Advertisements

3 Responses to Garbage Collection

  1. Max says:

    Yeah, don’t forget “Engine”

  2. Hey Max,

    How you keeping dude?

    I think I might have been guilty of the odd ‘Engine’ myself 😛

  3. Darren Oakey says:

    I’m not sure about “helper” – typically, helper classes are written when someone wants to extend underlying classes, but for whatever reasond doesn’t have access to them… For instance, if you want to make a function that deals with strings, you can’t because you can’t modify the underlying string class. I’ve been begging MS for a way of extending other classes, and C# 3.0 will provide that – but until then, Helper classes are the only way to go. Personally, I prefer to just use the suffix “s” instead – ie instead of DateHelper,”Dates” because it looks better… but in reality – there is nowhere else to put that code – and the far far far worse evil would be to duplicate it or spread it amongst other classes. Your Strings.Contains function belongs with the String class, even if the original class designer didn’t think to put it there.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: