Clean Coding – Consider the given code snippet…


Consider the given code snippet.

class Product {
    private:
    string type;

    Product(string type) {
        this->type = type;
    }

    int getBasePrice() {
        switch (this->type) {
            case food:
                return 10;
            case drinks:
                return 7;
            case books:
                return 3;
            default:
                return 0;
        }
    }

    int getTaxPercent() {
        switch (this->type) {
            case food:
            case drinks:
                return 24;
            case books:
                return 8;
            default:
                return 0;
        }
    }

    string getProductCategory() {
        switch (this->type) {
            case food:
            case drinks:
                return 'Food and Beverages';
            case books:
                return 'Education';
            default:
                return '-';
        }
    }
}

You notice that when introducing a new product type in the code, you’ll have to make changes in three different methods of the class. Which of these design smells does the given snippet have?

Related Posts

Close Bitnami banner
Bitnami