Why you should NOT make everything PUBLIC!

preview_player
Показать описание
👍 Learn to make awesome games step-by-step from start to finish.

💬 As you're starting out you might learn to set things as public in order to expose them in the Editor.
In this video I tell you why you should NOT do that!
The reason is to write good clean code that minimizes code complexity. The less accessible your variables are the better.
If you have an error with a variable that is set to public, you don't know where the error comes from, it might be in that class or any other class in your entire code base since every class can modify that field.
However when you set it to private you make sure that any issues that occur have to be happening from within that class, that really helps on keeping a smaller mental model of what your code is doing.
And if you want to expose a variable in the editor, simply use the attribute [SerializeField]
That way you keep your code nice and clean but still keep it editable in the Editor.

👍 Learn to make awesome games step-by-step from start to finish.

🌍 Get Code Monkey on Steam!
👍 Interactive Tutorials, Complete Games and More!

If you have any questions post them in the comments and I'll do my best to answer them.

See you next time!

#csharp #unity #programming #unitytutorial #gamedev

--------------------------------------------------------------------
Hello and Welcome!
I'm your Code Monkey and here you will learn everything about Game Development in Unity using C#.

I've been developing games for several years with 8 published games on Steam and now I'm sharing my knowledge to help you on your own game development journey.

I do Unity Tutorials on just about every topic, Unity Tutorials for Beginners and Unity Tutorials for Advanced users.

--------------------------------------------------------------------
- Other great Unity channels:
--------------------------------------------------------------------

Рекомендации по теме
Комментарии
Автор

👍 Learn to make awesome games step-by-step from start to finish.

CodeMonkeyUnity
Автор

In my 4 years of code learning, the public variables become 20% more private each year. I am still lazy about making all private even in cases that I know it should be. But I will get there. Thank you so much for the great videos.

Falesam
Автор

The best example I've come across as to why you should make fields private and use functions to access them is to think of it like a device such as a TV remote.

Do you need to know how a remote works in order to use it? Nope. As long as you have the buttons needed to perform its function, that's all that matters. In fact, if late at night a bunch of garden gnomes sneak into your house, and completely change the inner-workings of the remote and then leave - if the next time you use it and it still functions the same, you won't notice the difference.

In a similar manner, the public entities of a class are those "buttons", and other classes should use those without having to "know" how they are defined as long as the expected result is achieved. You should make it in a way such that you can change the way your class works without affecting code in other classes.

ahmadel-baher
Автор

Hey Monkey,
this video should naturally continue with using a property for public reading but private writing and also you should check out the benefits of using scriptable objects as variables because it avoids dependencies and unnecessary errors. I admire what you do for the community! Cheers!

esosek
Автор

I did wonder about this when I started learning coding, never knew why until now. Thank you for the knowledge.

freezinfire
Автор

Amazing video! It'd be really cool if you turned this into a series about good coding principles for Unity :)

ivopfaffen
Автор

I just started learning C# for Unity specifically (about a week or so in). The series I've been watching has been great so far but I questioned this the second it was introduced. I absolutely asked myself, "Why would you make anything private ever?" Glad I got my answer, how serendipitous.

brocklawson
Автор

What I also like about SerielizedField is that you can put further atributes to it, like Range() or Min(). That way you can clamp the value you can give in the editor.

Kortonox
Автор

Code Monkey, thank you for keeping and not removing subtitles that are automatically generated by YouTube (even if the speech does not always match what is written in the subtitles). This allows me to translate them into my language (russian) and watch your videos.😜

glorytoukraine
Автор

This is quite a contentious topic.
Making everything private by default is what is thought in schools and is a generally accepted good practice.
Personally however, I find that non-public access is more trouble than it's worth. Every field, method, class and whatever I write is always public.
There are several reasons for this.
First: The language allows you to access private fields anyway, it's called reflection.
Second: You have to provide access to all your fields anyway (through methods/properties). You may think that you don't have to but if other people end up using your code then someone sometime is going to want to access that private field.
An example for unity: extending the animator. If you try this you will find that a lot of the fields are non-public, forcing you to use reflection.

vaflov
Автор

Got the megapack, very glad I could find a way to support your channel. You make much better tutorials than anyone else I've seen, and I direct aspiring Unity developers to you whenever I can. A big thanks for all the informative videos you've made, I will always be a silent supporter of your channel.

KineticCode
Автор

This HAS TO become a series of clean code principles!

tigransahakyan
Автор

I dont use unity but to just keep learning about everything makes me happy. I would love to see you make more videos about clean code. Very interesting and helpful

davedipy
Автор

There is 2 things that I do to make a cleaner code : as you said in the video, I avoid as much as possible the public field, and when I make a public variable or a public function, I also add a comment to clarify the other classes/objects that use this variable/function.

thomasremastered
Автор

Man you are speaking wisdoms in every video you make. Also when you said that the only limitation is the developer experience not Unity, that powerful words convinced me not to distract myself switching between Platforms. Thank you man🌹

humadi
Автор

I recently picked up Code Complete 2 from your video with gamedevtv...I'm really enjoying it! Thank you

JW-uuje
Автор

Thank you soo much, I just finished converting all my scripts to Private and Serialized Fields. Feels good having clean code :)

Lonzy
Автор

I watched this video shortly after it came out, and after watching your video from yesterday (Difference between Transform & GameObject), I decided to check this out again since you mentioned it. I was curious if everything you said in this video had stuck, and I'm glad to say that it has!
I realise this is quite basic compared to most other concepts, but even a reminder on "simple" things goes a long way sometimes.

You're honestly a great teacher, Code Monkey. Thank you for all the amazing content and courses you've made. <3

Keep up the incredible work.

stephenperry
Автор

Thanks for the book recommendation! I saw this video while I was planning to learn unity, saw the book and decided to wait on watching the rest of the video till I read it. I just reached 6.2, where it talks about this, and so far, this book has taught me a lot! Thanks!

DL-zvxc
Автор

Thanks, as a self taught coder many of this basic concept of OOP alludes me (and i just follow them blindly because of "good practice"), and yes, you're the first one that explains the concept clearly

RobertGuilman