Home » Avoid… » Switch statements » Lines of code: switch statements vs. polymorphism

Lines of code: switch statements vs. polymorphism

This is a comparison of lines of code used for switch statements-based solution and polymorphic solution from my “Avoid Switch Statements” article:

Lines overall

Let’s see how many lines of code are required for different solutions. All the empty lines are removed, so that the number of lines will be more precise.

Using switch statements:   Using polymorphism:
    public enum EmployeeType
    {
        ENGINEER,
        SALESMAN,
        MANAGER,
        RESELLER
    }
    public class Employee
    {
        private int _monthlySalary;
        private int _commission;
        private int _bonus;
        private EmployeeType Type;
        public Employee(int monthlySalary, int commission, int bonus, EmployeeType type)
        {
            _monthlySalary = monthlySalary;
            _commission = commission;
            _bonus = bonus;
            Type = type;
        }
        public int PayAmount()
        {
            switch (Type)
            {
                case EmployeeType.ENGINEER:
                    return _monthlySalary;
                case EmployeeType.SALESMAN:
                    return _monthlySalary + _commission;
                case EmployeeType.MANAGER:
                    return _monthlySalary + _bonus;
                case EmployeeType.RESELLER:
                    return _commission + _bonus;
                default:
                    throw new Exception("Incorrect Employee");
            }
        }
    }
    public class TravelExpenseCalculator
    {
        public decimal Calculate(EmployeeType employeeType, int kilometersTravelled)
        {
            switch (employeeType)
            {
                case EmployeeType.ENGINEER:
                    return kilometersTravelled * 0.2m;
                case EmployeeType.SALESMAN:
                {
                    if (kilometersTravelled < 30)
                        return 0;
                    return (kilometersTravelled - 30) * 0.25m;
                }
                case EmployeeType.MANAGER:
                    return kilometersTravelled * 0.3m;
                case EmployeeType.RESELLER:
                    return kilometersTravelled * 0.25m;
                default:
                    throw new Exception("Incorrect Employee");
            }
        }
    }
 
    public class Engineer : IEmployee
    {
        private int _monthlySalary;
        public Engineer(int monthlySalary)
        {
            _monthlySalary = monthlySalary;
        }
        public int PayAmount()
        {
            return _monthlySalary;
        }
        public decimal CalculateTravelExpense(int kilometersTravelled)
        {
            return kilometersTravelled * 0.2m;
        }
    }
    public class Salesman : IEmployee
    {
        private int _monthlySalary;
        private int _commission;
        public Salesman(int monthlySalary, int commission)
        {
            _monthlySalary = monthlySalary;
            _commission = commission;
        }
        public int PayAmount()
        {
            return _monthlySalary + _commission;
        }
        public decimal CalculateTravelExpense(int kilometersTravelled)
        {
            if (kilometersTravelled < 30)
                return 0;
            return (kilometersTravelled - 30) * 0.25m;
        }
    }
    public class Manager : IEmployee
    {
        private int _monthlySalary;
        private int _bonus;
        public Manager(int monthlySalary, int bonus)
        {
            _monthlySalary = monthlySalary;
            _bonus = bonus;
        }
        public int PayAmount()
        {
            return _monthlySalary + _bonus;
        }
        public decimal CalculateTravelExpense(int kilometersTravelled)
        {
            return kilometersTravelled * 0.3m;
        }
    }
    public class Reseller : IEmployee
    {
        private int _commission;
        private readonly int _bonus;
        public Reseller(int commission, int bonus)
        {
            _commission = commission;
            _bonus = bonus;
        }
        public int PayAmount()
        {
            return _commission + _bonus;
        }
        public decimal CalculateTravelExpense(int kilometersTravelled)
        {
            return kilometersTravelled * 0.25m;
        }
    }
    public interface IEmployee
    {
        int PayAmount();
        decimal CalculateTravelExpense(int kilometersTravelled);
    }

That’s 60 vs. 77, which is around 20% fewer lines of code for solution with switch statements. The first impression is not surprising. However, nearly all of these 17 extra lines of code contain only curly brackets. Let’s now have a closer look to how many lines with real implementation code are there. Let’s remove all the signatures, class names, private fields, brackets, etc and keep only the implementation logic, like assignments, operations, ifs, etc.

Lines with implementation

Using switch statements:   Using polymorphism:
            _monthlySalary = monthlySalary;
            _commission = commission;
            _bonus = bonus;
            Type = type;
            switch (Type)
                case EmployeeType.ENGINEER:
                    return _monthlySalary;
                case EmployeeType.SALESMAN:
                    return _monthlySalary + _commission;
                case EmployeeType.MANAGER:
                    return _monthlySalary + _bonus;
                case EmployeeType.RESELLER:
                    return _commission + _bonus;
                default:
                    throw new Exception("Incorrect Employee");
            switch (employeeType)
                case EmployeeType.ENGINEER:
                    return kilometersTravelled * 0.2m;
                case EmployeeType.SALESMAN:
                    if (kilometersTravelled < 30)
                        return 0;
                    return (kilometersTravelled - 30) * 0.25m;
                case EmployeeType.MANAGER:
                    return kilometersTravelled * 0.3m;
                case EmployeeType.RESELLER:
                    return kilometersTravelled * 0.25m;
                default:
                    throw new Exception("Incorrect Employee");
 
            _monthlySalary = monthlySalary;
            return _monthlySalary;
            return kilometersTravelled * 0.2m;
            _monthlySalary = monthlySalary;
            _commission = commission;
            return _monthlySalary + _commission;
            if (kilometersTravelled < 30)
                return 0;
            return (kilometersTravelled - 30) * 0.25m;
            _monthlySalary = monthlySalary;
            _bonus = bonus;
            return _monthlySalary + _bonus;
            return kilometersTravelled * 0.3m;
            _commission = commission;
            _bonus = bonus;
            return _commission + _bonus;
            return kilometersTravelled * 0.25m;

That’ 28 vs. 17 which is a totally different picture. Having more classes may increase a total number of lines of code, but significantly reduces the number of code lines with logic.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: