Why use getter and setter methods

Why in classes we are using " _ " symbol before constructor properties and then making getter and setter methods ? I think if we don’t use “_” it would be the same thing .

_ harkens back to languages in which for object-oriented programming (OOP) practice does require one to distinguish explicitly between public and private data members. To understand why public and private members are useful, it’s maybe useful to look to read up on OOP.

The basic gist is that in a bigger code-base, you don’t accidentally want to change things that are going to have unwanted side-effects elsewhere. When you’re doing small examples it’s not as apparent why there’s an urgent need for it, but developers use it because it’s useful.

I think (although someone can correct me if I’m off track) that in functional programming (different paradigm from OOP) public/private is not really a thing because side-effects are already minimized from the start.

Further reading: object oriented - Why do we need private variables? - Software Engineering Stack Exchange

4 Likes