Meal Maker, why getters and setters?

Why do we need to add setters and getters in the Meal Maker exercise? My impression is that they are not necessary to make the code work.
Hope you can clarify.
Many thanks
Link to the project

At the moment, getters and setters do not add much value (in my opinion)

however, using getters and setters are very useful if you would like to expand the project, functionality you could add for which getters and setters are very useful:

  • You can check if new data is valid before setting a property.
  • You can perform an action on the data while you are getting or setting a property.
  • You can control which properties can be set and retrieved.
1 Like

I entirely agree. I created the getters and setters just like the experienced developer in the demo video, finished the project, and got the desired results (ie a random meal). I then commented out all the getters and setters and also got the desired random meal outcome.

Not only are they not necessary, it looks like they aren’t even being used in the code!

most of the exercises don’t do anything particularly interesting with getters and setters,
but they can be useful in specific situations:

  • checking if a value is valid (for what you’re doing) before you change the value of a variable
  • if you want several things to happen when you set a object’s variable to a value, or when you’re getting the value of a variable

You also see getters and setters in other programming languages where objects have variables that you can’t access directly (they are “private” variables)