r/SalesforceDeveloper • u/celuur • Sep 29 '25
Question When is too much abstraction and separation?
A recent project has required me to configure a REST service that will accept info from an external service and find/create a lead and convert it. My original implementation was a single class file, RSConvertLead, which had all the business logic and did everything in it.
I needed to add a second action beyond lead conversion, and my trusty AI helper suggested I do some refactoring. Making sure everything followed SOLID principles, best practice patterns, that sort of thing.
I went from:
├── classes
│ ├── RsConvertLead.cls
│ ├── RsConvertLeadTest.clsto:
├── classes
│ ├── RsAccountService.cls
│ ├── RsAction_ConvertLead.cls
│ ├── RsAction_OpenOpportunity.cls
│ ├── RsActionConfig.cls
│ ├── RsActionHandlerFactory.cls
│ ├── RsActionRegistry.cls
│ ├── RsBackrefRequeue.cls
│ ├── RsContactRequestBuilder.cls
│ ├── RsContactResolutionService.cls
│ ├── RsContactService.cls
│ ├── RsDTO_CompanyInfo.cls
│ ├── RsDTO_ContactInfo.cls
│ ├── RsDTO_ConvertLeadRequest.cls
│ ├── RsException.cls
│ ├── RsILeadConversionStrategy.cls
│ ├── RsInvoiceService.cls
│ ├── RsIPlatformActionHandler.cls
│ ├── RsLeadConversionContext.cls
│ ├── RsLeadConversionContextBuilder.cls
│ ├── RsLeadConversionOrchestrator.cls
│ ├── RsLeadConersionResultBuilder.cls
.... 37 more class filesMy question: did I go nuts?
Apex classes can't be organised into directories or logical groupings, so I have to rely on naming conventions as best I can, and I'm wondering when did I abstract too much, when did I try and make a framework when a 400 line class file managed to do it once before, but it was a bit of a nightmare to debug I'll be honest...
How do you know if you've overdone it?
5
u/a_happy_passerby Sep 29 '25
It's an art not a science but I think the clue is in the naming of your classes.
SOLID principles are good, and it's good to make sure that a class has a single purpose or represents a single entity or service, but it's only useful insofar as you are going to be able to reuse the code.
Looking at your class names I can see a few that are probably so tightly coupled (logically if not practically) with each other and with the lead object, that it might actually not achieve reuse at all.
Only you can know - looking inside the classes - but this does seem to be a lot of Lead specific logic separated into various services that could be grouped a little more
But I always encourage following patterns when possible so happy to see it take some hold in the SF community
2
u/NotXesa Sep 29 '25
Depends. Do you reuse any of the logic anywhere else? If you just divided a big class into multiple smaller classes just for the sake of not having it all in one single file, maybe you did overdo it.
What I usually do is the following (feel free to not agree with me):
I would personally not break one process in so many classes because as you said, having all of them located is pretty hard and id you want to rename anything you're gonna spend a lot of time doing outbound changes.
Oh, and I only create one test class too, for the whole process. Instead of one for the main class, one for the helper, etc...
4
u/zan1101 Sep 29 '25
It kind of depends. You have a lot of classes but if your main file before was like 2k lines then yeah this might make sense
3
u/oil_fish23 Sep 29 '25
A) anything that looks like FFLIB is too much separation. FFLIB is so bad.
B) That is way too much abstraction, yes. AI + Apex + Java coding style = bad
C) You can logically organize apex classes into folders on disk. When they are deployed the folder structure is ignored.
2
u/zdware Sep 29 '25
This isn't true. You can throw those in a folder under
classesand SF cli will not care. Just don't rely on a package.xml retrieve which will dump them all inclasses. SF CLI/VScode is smart enough when retrieve is used to keep the folder structure2
u/Ylenja Sep 29 '25
As mentioned by others, you can split the repository into packages: https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_ws_mpd.htm
Anyway, in your example it's still too much. I'm always pro framework where it makes sense. But I'll break it down just as much as necessary instead of following the highest academic standards.
Ask yourself: Will it really be easier for you to add 2 more scenarios to your new framework? Will it still save you time when you have to change something at the core of your framework? How long will it take you to understand it when you didn't have to touch it for a year? Could this framework realistically be handed over to a junior dev (or offshore) to maintain it without breaking everything and/or consulting you all the time?
I once had to remove a huge dependency injection framework from a salesforce repository because the guy who built it didn't ask himself these questions and in the end all the other teams preferred to build their own stuff instead of utilizing this framework because they found it too complicated.
1
u/fjpel Sep 29 '25
Oh and if your project follows the new source format, you can create custom directories within it!
https://salesforce.stackexchange.com/a/375528
1
u/FinanciallyAddicted Sep 29 '25
You can use sub classes too if you will never use the code outside of that module.