Your Controllers Should Be Thin As Possible
A controller's job should really just be to:
- receive the HTTP request
- extract data from it (body, URL parameters, headers)
- call the appropriate service to do the actual work
- take the service's response and format it as HTTP (status code, JSON body, etc)
Thats it. A controller should not contain complex logic about how things work, they should just coordinate between the HTTP world and your business logic.
Your actual business logic can be in the service layer. For example:
In a controller: "get the user ID from the URL, call the service to create an order, return 201 status"
In a service: "check if the user has valid payment info, verify items are in stock, calculate total with tax and discounts, create the order record, send confirmation email, update inventory"
Why separate them?
- reusability: you might need to create an order from your API, a scheduled job, or whatever. If the logic is in a service, all three can use the same code. If it's buried in a controller, you'd have to duplicate it.
- testing: testing services is much easier because they're not tangled up with HTTP stuff.
- clarity: when you open a controller, you immediately see what endpoints exist and what they do at a high level. The messy details live elsewhere.
- changes don't cascade: If your business rules change but your API endpoints stay the same, you only touch services. If you add a new endpoint that uses existing logic, you only touch controllers/routes.
I don't care what AI does, the version of you who puts in the effort and hard work will always outperform and be happier than the version of you who does not.
what people think AI does to software engineer: kills it as a career
what AI really does: every job starts being done by a software engineer. recruiting, law, sales, finance, accounting
learn to code
I completely agree that those who believe the current generation of AI models will replace software engineers are not solving hard engineering problems. People often conflate coding with software engineering. One is writing, while the other is thinking.
Got promoted ๐ to Project Lead at @To_GoMobility - A Mobility Platform For Events & Event Attendees!
In 1 year & 7 months, weโve:
โ Launched our MVP, Web, & Mobile Apps.
โ Built 5 key event partnerships in 5 Cities In Nigeria.
โ Partnered with diverse automobile providers!
Do you know what it takes to work at a startup?
A 32-hour work week, a 4-day work week, unlimited paid time off... stop dreaming. Check my post on Linkedin for the full gist.
https://t.co/G7Rd9RzNfe