Search code examples
open-closed-principle

How can I prevent violating the open closed principle when using data from a database


I am fetching json data from a database formatted in the following way

{
    name:{
        type:string,
        value:'test',
    },
    age:{
        type:number,
        value:6,
    },
    hairColor:{
        type:color,
        value:'255,255,255',
    },
    font:{
        type:fontFamily,
        value:'Arial',
    }
}

So in my front-end I would have to check what the type is in some sort of switch statement in order to know what UI element I should render. This violates the open-closed principle becuase when I add a new type to my database I would have to go and modify the switch statement.

How could I deal with this?


Solution

  • Why dont you use default for all other types. It should be in accordance with open-closed principle. For eg:

    switch(type){
          case A:
           return $TRUE
          default:
           return $FALSE
    }