JavaScript - RandomMealGenerator - Why Setters and Getters?

Hello everyone,

I’m currently working on the Random Meal Generator code from the Full Stack Developer Course. In the project walkthrough we’re advised to create setter and getter methods for every one of the dishes. Nevertheless, the code works just fine without them as well. I think they could have a useful functionality, but I fail to understand which one. Could someone explain why we would need setter- and getter-methods in that code?
My code here, I have the getter and setter methods but as a comment and the code works just fine :slight_smile:
Many thanks in advance!!!

const menu = { _courses:{ appetizers: [], mains: [], desserts: []}, /* get appetizers(){}, set appetizers(appetizerIn){}, get mains(){}, set mains (mainsIn){}, get desserts (){}, set desserts (dessertsIn){}, */ get courses(){ return { appetizers:this.appetizers, mains: this.mains, desserts: this.desserts, } }, addDishToCourse(courseName,dishName,dishPrice){ const dish ={ name: dishName, price: dishPrice }; this._courses[courseName].push(dish) }, getRandomDishFromCourse(courseName){ const dishes = this._courses[courseName]; const randomIndex = Math.floor(Math.random() * dishes.length); return dishes[randomIndex] }, generateRandomMeal(){ const appetizer = this.getRandomDishFromCourse('appetizers'); const main = this.getRandomDishFromCourse('mains'); const dessert = this.getRandomDishFromCourse('desserts'); const totalPrice = appetizer.price + main.price + dessert.price; return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}. The total price is $${totalPrice}.` } } menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25); menu.addDishToCourse('appetizers', 'Feldsalat and Stuff', 3.00); menu.addDishToCourse('appetizers', 'Crack sandwich', 0.35); menu.addDishToCourse('mains', 'Mushroom Wellington', 6.50); menu.addDishToCourse('mains', 'Pizza', 11.30); menu.addDishToCourse('mains', 'Lasagna', 5.60); menu.addDishToCourse('desserts', 'Berry Crumble', 3.50); menu.addDishToCourse('desserts', 'Brownies', 1.50); menu.addDishToCourse('desserts', 'Space Cookies', 4.20); const meal = menu.generateRandomMeal() console.log(meal)

Getters and setters are often linked to the object-oriented programming (OOP) way of thinking. In particular, they relate to a class data members privacy concerns.

Privacy in js can’t be strictly enforced like it can in some other languages (which is why this lesson uses the convention of prepending names with a _ to designate members as private). One aim of this is to ensure that the usage of the class you create falls within the your desired range.

For example, your class might have a data member that’s an integer that only works if it’s within a certain range. The user of your class for some reason overlooks this and tries to set it out of range. A good setter method would throw some kind of error and/or prevent the action from happening, whereas direct access could be disastrous (since it can be set to something out of range).

There’s more nuance to this, pros and cons and whatnot. But if you want to learn OOP they’re definitely something to think about. java - Why use getters and setters/accessors? - Stack Overflow

2 Likes

Thanks for your answer! Now I understand better :slight_smile: