+ 1
Is it good practice to declare variables under public in a class? Or should I put them under private and create get functions.
Is it good practice to declare variables under public in a class? Or should I put them under private and create get functions and call the get functions in the main file
1 ответ
+ 2
really, depends on what you want. If you are coding in an OOP manner, the most common thing is to have getters and setters and have the member variable itself private. That has the benefit that you can add sanity checks to the setters and can make sure it only gets set to sane values and so on. However, there is a small drawback an that is overhead in development time as well as a runtime overhead. So in some embedded cases where every cpu cicle counts, you might want to avoid it and have it public.
I personally don't see too big of a problem with having member variables public in most cases