DEV Community

Kristijan Kanalaš
Kristijan Kanalaš

Posted on • Edited on

Why you shouldn't cascade functionality

Don't do it! I'm serious, it will come back and bite you in the ass. đŸ˜†

So here is what I did

I was working on a microservice that should extract images that have faces in them from PDF files. I used Symfony and as such I made a few services that were responsible for their part of the job. For example, ImageExtractionService or FaceDetectionService.
I was pretty proud of the finished project, using some open source services to detect faces in pictures and choosing what program to use to extract images from PDF files, that I forgot to think ahead about code reusability and overall code quality.

The problem

I cascaded the functionality between the services. It looked something like this:

//ImageExtractionService
$this->faceDetactionService->detect()

//FaceDetectionService
function detect() {
    //some code
    $this->imageService->process()
}

//ImageService
function process() {
    //some code
    $this->cleanupService->cleanUp()
}
Enter fullscreen mode Exit fullscreen mode

Chaining it like this you end up making unwanted connections, for example if I was only interested in what FaceDetectionService "saw" I couldn't obtain that data because it doesn't return that data instead it immediately tries to process the image.

How did it come to bite me in the ass?

Well, few days after the feature went to production I noticed a need for a service debug option. But because my code is chained and needs a link to download a PDF and needs a link where to upload the images I just couldn't make a local debug unless I truly separated the services.

So... Don't do it! Don't chain the functionality of separate services.

If you'd like to support me writing future posts feel free to buy me a cup of coffee.

Top comments (0)