BETTER SOFTWARE DEVELOPMENT
What Does ‘Service’ Class Mean?
I’m sure you’ve never looked at a class called “SomethingService” and right off the bat knew exactly what it was all about.
Truth be told, we’re all confused.
One thing is obvious after years of professional development, wading through tons of code. Developers are terrible at naming things.
As a rookie developer, I know you feel overwhelmed, puzzled or straight-up confused whenever you meet
WhateverManager class in the wild — or better,
Class names are like job titles
Business Developer? Yeah, you’re a salesperson. Social Media Ninja? You probably got a 3-week course in how to post cat pics on Instagram and develops a “sick” SoMe strategy. Software Developer? You might as well be a code monkey. Who knows which responsibilities you’re assigned.
There’s a distinction to make. Job title and job role. We sometimes (often) like to make our titles fancier than the role we’re having.
The same goes for classes.
The service classes
Service is basically a logic dumpster. Any new feature added to your application somehow always ends up implemented in some “service” class.
A service class takes a large number of constructor arguments, making it hugely painful to test. Having to test that new method
GetUserCount()? Sure, it makes total sense we’ll need to mock 7 completely unrelated classes to even instantiate the service class.
GetUserCount() only relies on a single constructor argument in the service class, then move it to its own class. In that way, you now have a very simple class with only one constructor argument.
Sure, it’s not as cool to have
GetUserCount class. But at least you know exactly what it does.
Testing becomes a breeze.
The manager classes
You’ve likely met a
UserManager. It manages users. Whatever that means.
Like service classes, they too are logical dumpsters. But there’s a slight difference. If the Service class is a general waste dumpster, a manager class is like a cardboard dumpster. It’s more specialized.
You’ll face the same issues dealing with unit tests as with the service classes. You’d need to mock a ton before doing the tiniest of operations.
The Utility and Helper classes
“We basically didn’t know where to put this, so here’s a
These are annoying as hell. There’s not a whole lot more to say about this.
So, what should you do?
Simple. Give proper names matching the responsibilities. There’s no shame in naming a class
GetUser and only have a single method. It’s simple. Everyone knows what it does. It’s easy to unit test.
Stop the code bloat and simplify those classes. Like, one method simple.
Nicklas Millard is a software development engineer in one of the fastest-growing banks, building mission-critical financial services infrastructure.
Previously, he was a Big4 Senior Tech Consultant developing software for commercial clients and government institutions.
Connect on LinkedIn
Factory Pattern Without Switch, This Is How It Should Be Done
My approach to creating easily extensible factory classes without logical statements such as switch and if-else
Gain Access to Expert View — Subscribe to DDI Intel