public or private ?

Greetings,

someone told me long times ago that usually, programs try to use private variables when it is possible, in order to “increase security” and avoid data modification. Is it also the case in Unity ? Should I try to use the maximum of private variables, and use function to retrieve and modify their content ?

Generally speaking, you’ll want to make your fields private unless you need them publicly exposed. Often, people will use properties (getters/setters) so that the class retains some control and some level of abstraction to that class’ fields.

Unity is no different but it’s up to you how you want to use them. I would say it’s more important to employ these abstractions when you have multiple developers working on the same project, but it can be worthwhile for a single programmer to keep the API light and easy to manage.

i’m trying to avoid public/static variables when possible. unity makes it easier for us to use public variables than most programming languages, but i think it is more convinient to debug if you know exactly in which script you need to begin searching for errors if something goes wrong

Public variables facilitate sloppy relationships between objects, so I try to minimize them.

It must be a pain to have a program only composed of private variables ? What should I do ? Scripting so it is the easiest method for me, or trying to privatize most of the stuff ? Or it do not “really” matter ? I am alone on my own project…

I suggest you try to use private variables, and just in case you need to share information between classes, you could do them public or implement properties (which are, in fact, set get functions).

Advantages of using private variables: you are avoiding unwanted changes.

Public variables might make your developing processes easier and faster but tends to make code maintenance and debugging harder. Choose your side.

Look for Coupling in OO books. Coupling (computer programming) - Wikipedia

I remember hours lost in Debug, scratching my head. I will go for the private :wink:

Thanks you for your answers (and the reading :))

Something I’ve never quite understood, why does using get and set more likely to help debugging code?
Regardless if you want to track down where a variable gets changed in the code outside the class you still have to either search for the variable name or search for a get/set function. What am I missing here?

I have a tiny idea but I won’t say it, because I may say a blunder :slight_smile:

About functions, something popped into my mind : should I make them private too ?

this look like a long never ending debate as i can see on some coding forum , between the partisans of all properties + private encapsulation things etc…, some other will say properties will occur to have quiet a bit of thing happening under the hood etc…etc…etc…

coding world seem to be far form the ISO norm yet :lol:

in the end i guess that this depends on how complex your project is going to be and what you think fit best your purpose …

@AkilaeTribe

You shouldn’t make functions private - they usually are used from outside the class.

But in case you have an internal function ( used exclusively with your class ) you make that private.

Acknowledged ! Thank you :wink:

You are welcome :slight_smile: