DEV Community

Cover image for Utils classes bury the lead
Jerome Thibaud
Jerome Thibaud

Posted on

Utils classes bury the lead

Happy to be here!

In my recent article, I relate a discussion I've had many times over: Utils classes are 'bad'. "Bad how" you ask? Like crossing the streams? I believe it's about information or the lack thereof. I discuss the problems with creating utility classes and I offer solutions for improving code organization and usability.

Think about your code as a product and other developers as your users.

The issues with 'Utils'

The name 'Utils' provides no valuable information, making it difficult for developers to understand the purpose of the code.

Lack of discoverability can lead to duplication, as developers might miss existing methods and create new ones.

Utility methods are often provided through static methods, which can cause challenges in testability and flexibility.

A few suggestions to make it better

To address these issues, I suggest a few things:

Consider the problem domain and choose informative names for your code, placing it near relevant domain types.

Leverage design patterns and language features to increase usability.

Organize code in a predictable way and providing information at each level of the source code hierarchy.

Ask questions about the intent, context, relationships, and problem domain to determine the best name and location for your code.

Use design patterns like the decorator pattern to add functionality to existing Types.

Avoid static methods to support testability and flexibility.

Utilize language features like extension methods and metaprogramming, especially when working with external libraries.

Conclusion

By following these guidelines, you can create more maintainable and discoverable code, ultimately saving time and effort in the long run.

You can read the full story on my site

Thank you for reading, I hope this was helpful.

Top comments (0)