July 5, 2026

How I Applied Clean Architecture to My Site Without Breaking Anything

How I Applied Clean Architecture to My Site Without Breaking Anything

The Problem: Code That Works, But Is Hard to Grow

When I built my personal site, the first goal was simple: make it work. And it did. But with every new feature I wanted to add, I ran into the same problem — where do I even make the change?

I had a single file over 1,200 lines long that handled almost everything. Business logic was tangled with database calls, and input validation was scattered between the frontend and the server. The code "got the job done," but it had become fragile: one small edit could break something in a distant corner.

I realized the problem wasn't the features. It was the structure.

The Solution: Clean Architecture

Clean Architecture is a simple principle at its core: separate your code into layers, give each layer a single clear responsibility, and let dependencies point inward only — outer layers know about inner ones, never the reverse.

I split the project into four layers:

The Domain layer: the core logic and entities, fully independent, knowing nothing about the database or the interface.

The Use Cases layer: the operations the system performs — publishing content, logging in, saving settings. It calls the core logic and holds no technical details.

The Infrastructure layer: everything that touches the outside world — the database, email, the Telegram bot, storage. Each sits behind a clear interface.

The Presentation layer: the UI and API routes, with no business logic — just a thin shell that receives a request and responds.

The Execution: Four Safe Phases

The most important decision I made wasn't technical, it was methodological: I applied the change in four gradual phases, not all at once.

Phase one: extract a shared use-cases layer to remove duplication between the bot and the interface.

Phase two: split the infrastructure into separate modules (database, email, authentication), while keeping import paths compatible so nothing would break.

Phase three: separate the data repositories from the mapping logic.

Phase four: break apart the giant bot — from 1,200 lines in one file into a small router that delegates to eight specialized modules.

After each phase, I built, tested, and deployed before moving to the next. I never rewrote everything in one go.

The Biggest Lesson

A full refactor of a live project done all at once is a recipe for disaster. If something breaks, it's hard to know which change caused it.

Safe, incremental improvement — one step, test, deploy, then the next — protects you. Each small phase is reversible, and every mistake is isolated.

Conclusion

Clean code isn't an aesthetic luxury. It's the practical difference between a project you can confidently extend a year later, and one you're afraid to touch.

Clean Architecture didn't make my site look any different to a visitor. But it made it — for me as a developer — a system I understand, trust, and build upon. And that, in the end, is what separates code that lasts.