DEV Community

Cover image for Ditch Your Helpers and Utilities Classes Before They Ruin Your Code
Cesar Aguirre
Cesar Aguirre

Posted on • Originally published at canro91.github.io

Ditch Your Helpers and Utilities Classes Before They Ruin Your Code

I originally posted this post on my blog a long time ago in a galaxy far, far away.


Recently, I needed to track down how an old Visual Basic WebForms app stored a value from the database into the Session object.

But I didn't know I was in trouble until I looked for the file in the GitHub repository and it said "(Sorry about that, but we can't show files that are this big right now.)"

The file had 69,359 lines. Its name? GlobalFunctions.vb. You can guess what was in there just by looking at that name, right? It was a clear symptom of a deeper problem.

A "dead" language isn't always the problem

The problem isn't that it was a WebForms app in Visual Basic.

We can write crappy code in any programming language.

It was that huge "throw everything under the carpet" kind-of file.

"Helpers" and "Utility" classes, full of unrelated static methods are one of the conventions I've decided to ban.

A better name is part of the solution

If you're tempted to write anything named with "Helpers," "Utilities," or worse, "GlobalFunctions," please stop and consider how to split that class into smaller, more focused classes with clearer names.

Smaller, well-named classes ease cognitive load, making them easier to work with.

Or even better, if you're practicing DDD, before defaulting to a "Utility" class, ask yourself: does this belong inside an entity or value object instead?

Writing readable code, by ditching Utility classes, is just one of the skills every coder should master. But it takes more than typing symbols to become a senior coder.

That's why I wrote Street-Smart Coding: 30 Ways to Get Better at Coding. It's the roadmap I wish I had when I was starting out.

Grab your copy of Street-Smart Coding here

Top comments (5)

Collapse
 
drewknab profile image
Drew Knab

That sounds remarkably like a WebForms code base I've previously worked on.

Collapse
 
canro91 profile image
Cesar Aguirre

It's like the framework itself encouraged bad behavior...dunno. I've never found a WebForms app in a good shape :/

Collapse
 
baltasarq profile image
Baltasar García Perez-Schofield

I must admit that I've used utility classes myself. I didn't understand that a utility class could be a problem, until you mentioned that 69 K lines. Unbelievable.

Collapse
 
canro91 profile image
Cesar Aguirre

They become a problem when we treat them as "throw everything under the carpet" kind of files.

Collapse
 
farhandigital profile image
Farhan Digital

I was confused the whole time because I thought you were against helpers/utility in general