Why you should NOT make everything PUBLIC!

117,061
0
Published 2021-08-02
🌍 HumbleBundle MEGAPACK cmonkey.co/humblebundle
✅ Unity Summer Sale (ends soon) assetstore.unity.com/?aid=1101l96nj&pubref=notpubl…
🌍 Get my Complete Courses! ✅ unitycodemonkey.com/courses
👍 Learn to make awesome games step-by-step from start to finish.
🎮 Get my Steam Games unitycodemonkey.com/gamebundle

💬 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.

🌍 Get my Complete Courses! ✅ unitycodemonkey.com/courses
👍 Learn to make awesome games step-by-step from start to finish.

🌍 Get Code Monkey on Steam!
👍 Interactive Tutorials, Complete Games and More!
store.steampowered.com/app/1294220/

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

🔔 Subscribe for more Unity Tutorials    / @codemonkeyunity  

See you next time!

📍 Support on Patreon www.patreon.com/unitycodemonkey
🤖 Join the Community Discord discord.gg/eHjUVrm
🎮 Grab the Game Bundle at unitycodemonkey.com/gameBundle.php
📝 Get the Code Monkey Utilities at unitycodemonkey.com/utils.php

#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.

You can see my games at www.endlessloopstudios.com
--------------------------------------------------------------------
- Other great Unity channels:
Unity - youtube.com/user/Unity3D
Brackeys - youtube.com/user/Brackeys
Dani -    / @danidev  
Jabrils -    / @jabrils  
BlackthornProd -    / @blackthornprod  
Sykoo - youtube.com/user/SykooTV
Jason Weimann -    / @unity3dcollege  
Jonas Tyroller -    / @jonastyroller  
--------------------------------------------------------------------

- Website: unitycodemonkey.com/
- Twitter: twitter.com/UnityCodeMonkey
- Steam: store.steampowered.com/developer/EndlessLoopStudio…

All Comments (21)
  • @Falesam
    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.
  • @freezinfire
    I did wonder about this when I started learning coding, never knew why until now. Thank you for the knowledge.
  • 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.
  • @esosek
    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!
  • @brocklawson6099
    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.
  • @ivopfaffen
    Amazing video! It'd be really cool if you turned this into a series about good coding principles for Unity :)
  • @KineticCode
    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.
  • @davedipy04
    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
  • @humadi2001
    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🌹
  • 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.
  • 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.😜
  • @Kortonox
    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.
  • Amazing video and very important idea I wish we could see a series from this kind of videos
  • 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
  • @RazeVenStudios
    Honestly I've struggled so much on this topic. Thank you for the guidance.
  • @Lonzy
    Thank you soo much, I just finished converting all my scripts to Private and Serialized Fields. Feels good having clean code :)