As I ponder over designing a class with a member variable of type object containing a fixed number of fields, the question arises: should I opt for a single setter function or multiple setters to modify these fields?
To illustrate this dilemma clearly, I have created a simple organizational management structure class in two different ways:
Multiple Setter Functions
class Management { constructor() { this.numberOfManagers = 100; this.salaryDetails = { regionalManagerSalary: 80000, stateManagerSalary: 110000, executiveManagerSalary: 200000 }; } setRegionalManagerSalary(salary) { this.salaryDetails.regionalManagerSalary = salary; } setStateManagerSalary(salary) { this.salaryDetails.stateManagerSalary = salary; } setExecutiveManagerSalary(salary) { this.salaryDetails.executiveManagerSalary = salary; } } const management = new Management(); management.setRegionalManagerSalary(100000); management.setStateManagerSalary(120000); management.setExecutiveManagerSalary(210000);
One Setter Function
class Management { constructor() { this.numberOfManagers = 100; this.salaryDetails = { regionalManagerSalary: 80000, stateManagerSalary: 110000, executiveManagerSalary: 200000 }; } setManagerSalary(typeOfManagerSalary, salary) { this.salaryDetails[typeOfManagerSalary] = salary; } } const management = new Management(); management.setManagerSalary('regionalManagerSalary', 100000); management.setManagerSalary('stateManagerSalary', 120000); management.setManagerSalary('executiveManagerSalary', 210000);
So, which implementation do you think would be more effective: the first one with multiple setters or the second one with a single setter function?