Refactor Helper/Utils Classes

Yaroslav Pylaev
2 min readAug 27, 2022

If I want to create a class with a list of consts and static methods to solve some routine tasks, how should I name it — MyClassUtils or MyClassHelper? I would prefer -Helper. And what is the help about? My case is to share some code between unrelated classes: easy to call a constant MyClassHelper.DUMMY_CONSTANT or validate a object with special logic MyClassHelper.isDummyObjectReallyDummy(dummyObject). Great solution!

But this discussion about the name is wrong. Creation MyClassHelper (even -Utils) is wrong. Because of SOLID e.g.

S — different specific business logic for a type (or for a domen) lives together in the class (sometimes too different).

O — such class exists in a static context so it can’t use the benefits of OOP and we have to change its methods to add some more functions instead of overriding them.

D — in a static context class methods needs all dependencies before the program runs.

I’ve even seen private static method for static helper method — it’s like helper for helper.

That’s why it’s better to take a step back and ask another question — where to insert new business logic for a data type?

So the core idea is to encapsulate the logic in a class with the data it operates on. If we speak about data type I practice Wrapper classes — Type -> TypeWrapper.

Benefits of the following structure are

  1. Business logic encapsulation
  2. Clear hierarchy of types via inheritance.
  3. Less voluminous partials deploys.

Besides, some constants will move to Wrappers. But if a constant is really needed to be shared, the trade off is to use a class name template like MyTypeConstantsStore, because -Helper or even-Utils don’t show the class purpose in this case.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response