DEV Community

Caio Tony
Caio Tony

Posted on • Updated on

How to successfully decouple your frameworks from your business logic to improve testing and maintainability

Hi Folks, Howdy?

have you ever heard about God Objects? never? already? however, let me explain what a hell is this:

In object-oriented programming, a God Object (sometimes also called an Omniscient or All-knowing object) is an object that references a large number of distinct types, has too many unrelated or uncategorized methods, or some combination of both. The God object is an example of an anti-pattern and a code smell. definition from wikipedia

Turn your attention to the bolded sentence, this is the main problem of God objects and also the issue that turns it into anti-pattern/principle cause it notably is opposed to single-responsibility principle. let's look to a example:

import { postModel } from "../models/post";
import { Post } from "../types"
import express from "express";
import { Request, Response } from express
const router = express.Router()


class controller {
  static async handle (request: Request, response: Response): Promise<Post[]> {

   try{
    if (request["query"] == {}) {
      return response.status(400).json({
        statusCode: 400,
        message: "must inform query parameters"
      })
    }
    const { query } = request;
    const posts: Promise<Post[]> = await postModel.find(query).lean();

    return response.status(201).json(posts)
  }catch(err) {
    return response.status(500).json({
      statusCode: 500,
      message: "internal server error"
    })
  }

  }
}

router.get("/posts", controller.handle);

export controller;

Enter fullscreen mode Exit fullscreen mode

Top comments (0)