Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 23, 2021 02:32 pm GMT

Why Switch Statement is Bad

Why Switch Statement Is Bad

First of all Switch Statement is not bad but sometimes violates Clean Code Principles
So Switch statement should be used very carefully.

Why Switch Statement is Sometimes Bad

  • Violates Open-Closed Principle S(O)LIDWhen adding a new Functionality with a new requirements so i will violate the Open-Closed Principle
  • Maintenance
    By time with a new Requirements it will be very hard to maintain it

  • Bad Code Smell & Bad OOP Style
    containing lots of redundant codes and the code going to be messy with time

Old Example

this Example Violates Open-Closed when we want to add new Functionality at this Function
also we have lots of redundant codes like Order Msg Data

function orderData(STATUS: string): Order {  const result = new Order();  switch (STATUS) {    case ORDER_STATUS.CHECKOUT:      result.msg = 'Welcome USER';      result.action = `${ORDER_STATUS.CHECKOUT} Action`;    case ORDER_STATUS.PAYMENT:      result.msg = 'Welcome USER';      result.action = `${ORDER_STATUS.PAYMENT} Action`;    case ORDER_STATUS.DELIVER:      result.msg = 'Welcome';      result.action = `${ORDER_STATUS.DELIVER} Action`;    default:      break;  }  return result;}

So Using switch on a type is very bad OOP Style how can we refactor this code ?

The best solution of this is Using ( Polymorphism + Strategy Pattern )

The Below Code is a Solution with Polymorphism & Strategy Pattern

1- Initialize IOrder Interface & Other Payment Processors which have different business logic

interface IOrder {  getOrderData(): OrderData;}class Checkout implements IOrder {  getOrderData(): OrderData {    return new OrderData('Welcome USER', `${ORDER_STATUS.CHECKOUT} Action`);  }}class Payment implements IOrder {  getOrderData(): OrderData {    return new OrderData('Welcome USER', `${ORDER_STATUS.PAYMENT} Action`);  }}class Deliver implements IOrder {  getOrderData(): OrderData {    return new OrderData('Welcome', `${ORDER_STATUS.DELIVER} Action`);  }}

2- Add Strategy Design Pattern Which Holds All Payment Processors Objects

class OrderStrategy {  public CHECKOUT: IOrder;  public PAYMENT: IOrder;  public DELIVER: IOrder;  constructor() {    this.CHECKOUT = new Checkout();    this.PAYMENT = new Payment();    this.DELIVER = new Deliver();  }}

3- Now Our getOrderData function clean and Ready to use

function getOrderData(STATUS: ORDER_STATUS): OrderData {  const orderStrategy = new OrderStrategy();  return orderStrategy[STATUS].getOrderData();}

Conclusion

Switch Statement is not bad at all but sometimes it violates OOP, also Breaks Open-Closed Principle

and it's going to be very hard to maintain and and refactor our Code in the future is going to be like a rock in our back
and the best solution to handle
these staff is to use Polymorphism and Strategy Pattern


Original Link: https://dev.to/eslamelkholy/why-switch-statement-is-bad-3hfi

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To