When Backend Engineers Write Frontend Code: A Tale of Refactoring
Sergio Bru

When Backend Engineers Write Frontend Code: A Tale of Refactoring

Last week, I found myself staring at a tangled web of JavaScript in our quiz application. As a backend engineer comfortable in the world of Spring Boot and Thymeleaf, I initially wanted to ignore the frontend mess and focus on "real engineering" on the server side.

But the code wouldn't let me sleep at night.


Original JS
Original snippet

This code worked, but at what cost? Future maintenance would be painful as new question types emerged, and debugging would be a nightmare with concerns scattered throughout the codebase.

So I embraced the challenge. Even as a backend engineer, I knew that maintainable frontend code follows the same principles we cherish in backend development. I decided to apply the Strategy pattern - a design pattern I frequently use in backend code - to solve this frontend problem.

The result? A clean implementation where different question types are rendered by specialized strategies:


Article content
Factory, ...yeahh

Each renderer strategy encapsulates its own rendering logic and event handling, making the code modular and extensible. The core QuizManager simply delegates to the appropriate strategy:

Article content
Simple and clear

The lesson? Good engineering principles and design patterns transcend the frontend/backend divide. The Strategy pattern provided the same benefits in JavaScript that it does in Java: flexible, maintainable, and extensible code.

Next time you're tempted to write "just enough JavaScript to make it work," remember that your future self (or colleague) will thank you for applying proper design patterns—regardless of which layer of the application you're working in.

#SoftwareEngineering #CleanCode #JavaScript #DesignPatterns #StrategyPattern #RefactoringJourney

To view or add a comment, sign in

More articles by Sergey Brusentsov

Explore content categories