Sometimes a class isn’t worth it.

Only some things deserve to be a class.

If you have a class with a few methods but no data elements, you may be better off implementing it as a method in another class. After all, without data, what does it really represent?

Occasionally you’ll see these things, such as a Calculator class, that only consists of complex logic for performing some calculations. But, of course, this begs the question, calculations on what? And as soon as you figure that out, you may wonder how that thing is modeled and whether these calculations should live closer to that object.

Programmers often name these classes after verbs.

In another example, take a class named “Shipper”. Its job is to ship something. Ship what? An order. Ah, it’s an OrderShipper. Or wait, maybe ship() should be a method inside the Order class. So my_order = new Order(”pair of shoes”); my_order.ship(”123 Mockingbird Lane”); and so on.

Leave a Reply

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

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

Facebook photo

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

Connecting to %s

%d bloggers like this: