DEV Community

loading...

Not noticing new objects

vasily profile image Vasily Polovnyov ・1 min read

Not noticing new objects or resources other than those that already exist in the domain is a common mistake. Because of it, we start to spread the entire behavior of an application across three or four models — God classes.

Let's say we have a blog service where administrators approve posts. Usually we tend to add approve and disapprove actions to the PostsController:

PATCH /posts/1/approve
DELETE /posts/2/disapprove
Enter fullscreen mode Exit fullscreen mode

PostsController becomes huge and unmanageable. A "refactor the PostsController" task is added to the tracker.

But if you look closely and ask yourself "should the PostsController receive approve or disapprove? If not, then who?", you may notice a new object and resource Approval:

POST /posts/1/approval
DELETE /posts/2/approval
Enter fullscreen mode Exit fullscreen mode

PostsController remains the same, ApprovalContoller turns out to be small and focused on a single task.

tl;dr Don't try to fit everything into existing objects: models, controllers. Look for new objects and resources

Discussion (0)

Forem Open with the Forem app