Stop with All the Classes, Already
 

­

People are overusing classes, and those classes are a burden on future maintenance.

­
­

Classless Can Be Classy

­

Dave Thomas

­
­

Give a typical OO developer an empty editor buffer, ask them to code, and it is a safe bet that the first five letters they type will be class.

­
­

We've all been brainwashed to believe that, in object-oriented systems, the fundamental—atomic—unit of design is the class.

­
­

It isn't.

­
­

It is called object-orientation for a reason. The essence of OO is the object; classes are simply ways of generating those objects.

 

This is the first of a series of mini-tirades against the abuse of classes in modern development.

­

Rule: No State? Not a Class!

 

I keep bumping into code that looks like this:

Somewhere else in the code, our intrepid developer will write:

Why would they write this? What they're trying to do is to call a method with a parameter. Why obscure that behind a class definition and an unnecessary constructor call?

 

Ah, but this code is clearly too embarrassing for our developer friend. So, instead, they write:

This is then called using

See, they say, now my class has state. Sometimes, to make them feel even better about this abomination, they'll hide behind a fancy name such as Service Object.
 

Bullshit. This is even worse than the first example. The code is more complex and, even worse, it creates the wrong impression for future maintainers. The fact we have a constructor that takes parameters might lead them to believe that they're dealing with an actual class, and to make assumptions that its objects might be long-lived.

 

A Naked Method Is a Function

 

A method that has no meaningful persistent state between calls is not a method. It's a function. It has no business sitting inside a class. Put it in a namespace: a module or a package or whatever your language du jour uses.

 

Some folks will complain that they need the service-object pattern because it lets them turn a function into a value: they create an instance of the class, then pass that instance to some other piece of code that then calls create(). By having multiple different classes, each with a create function, their code is now polymorphic.

 

This is an abuse of classes, because it fails to communicate intent. It you really want to do this, just use closures and/or pass lambdas or function references around. (If your language doesn't support closures, lambdas, or function references, perhaps it's time to enter the 21st century.)

 

In the next newsletter we'll dig deeper into the misuse of classes. (Hint: Laravel and Rails: we're looking at you….)

­

Maybe your friends and colleagues would be interested in these articles. They can sign up at pragprog.com/newsletter/ .

­

Photo by Dollar Gill on Unsplash

New Titles

­

• Vector Search • 
• Rails 8 • 
• OCAML • 
• Simplicity • 
• A/B Testing •

­

Click below to see the full list.

­
Only at PragProg.com
­

This email was sent to {{ contact.EMAIL }}  

You received this email because you are registered with Pragmatic Programmers, LLC
 
Replies to this email will be sent to an unmonitored email account.
 

Unsubscribe here

Pragmatic Programmers, LLC
PO Box 271356
Flower Mound TX 75027

­
­

©  2024 Pragmatic Programmers, LLC