Why you should NOT make everything PUBLIC!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome i'm your code monkey and in this video we're going to look at why you should not make all of your variables public if you want to expose some variable in the editor you really should keep it private and instead add the serialize field attribute in order to expose it in order to understand why that is the better method you need a basic understanding of clean code principles if you prefer a more guided path with step-by-step lectures then check out my complete courses learn how to make a builder defender game using c-sharp just like i make my own steam games or learn how to make games internally using visual scripting or learn all about unity with the ultimate overview course which contains over 13 lectures each covering a different tool or feature of the engine to help you make better games faster i'm always available in the courses q a section answering your questions every single day so check out all the courses with the link in the description also if you need some tdr check out the humble bundle link in the description with a massive amount at a super deep discount now first of all if you're not familiar with the term clean code i would highly recommend the book code complete once you read it you will increase your skills as a programmer tenfold it is definitely the one thing that i have read with regards to programming that had the biggest positive impact on my code quality a really important part of writing clean code is minimizing access as much as possible so every field every variable or class really should be as limited as possible while allowing it to achieve its purpose this is one of the best methods you have for managing complexity and ensuring your code is still easy to understand even as it grows when working with unity the editor and the fact that you can expose certain fields is extremely useful you can test out various values and easily iterate upon your game that's one huge benefit now when you want to expose a film the editor you can simply make it pollen so for example public float speed so you just do that and in period shows in the editor so you can now again very easily just modify this even modify this one runtime so very good for iteration however doing it this way means that this field is now accessible to every other part of our code so every other class in our code base can now read and modify this field now this can cause a lot of nasty issues and make your home codebase a complete mess with tons of scripts accessing and modifying tons of other scripts so perhaps you accidentally modify this field from another class and the whole thing breaks because it wasn't supposed to be modified or maybe just a bunch of scripts are reading from this field but you want those scripts to get a different value than what's exactly stored here or you just simply want to know when the field is read or modified now if you make everything public and you read and write from anywhere then very quickly your game will become a mess of unmanaged spaghetti code for example you have a player class with a public speed field and then the ui class is reading directly from that field at the same time the upgrade class sometimes modifies that maybe the enemy class also directly changes it to slow the player down to the specific attack and maybe there's multiple enemy types all of them with a different amount of slowdown now you find a bug where the speed is incorrect in order to find the cause you now have to search every single one of these classes since the bug could be anywhere maybe the enemy is setting the player speed when it shouldn't or maybe the upgrade logic is incorrect and it's applying a slowdown instead of a speed up it could be on any of these classes or any other class in your code base because any of those could possibly modify the field and since they are accessing the film directly it's very tricky to identify where and when the variable is changing the point is you now have to keep all of this logic of your entire code base in your brain as you try to search for the cause of the problem so ideally your code should be as limited as possible now if this field is meant to be used solely inside this class and nothing else then really this should be private doing so means no other class anywhere else in the code base can read and write this variable which is exactly what we want for example if i define another class here if i've got a second class with a reference to my first one and i try to access the speed variable there you go it's inaccessible due to its protection level whereas if this was public then i could modify that one from anywhere else in my code so that's what we're trying to prevent making it private then we simply cannot do what we're doing here writing clean code is all about managing complexity you want to minimize as much code as you need to keep in your brain at all times and when you make something as private when you see private you know for a fact that no other piece of code outside of this class can interact with this field so again that is really good for helping us manage complexity that means that if there's an error anywhere related to this class in this variable you know for a fact that the error has to be somewhere inside of this class since no other class can access that field but as soon as you make it private it removes one of the biggest benefits in unity which is to expose it in the editor like for example you have a player character and you want the speed variable to be exposed in the editor so you can easily play around and find the perfect speed value what that means is that in this case you want to keep the variable private so your code stays nice and clean but you also want it to be exposed in the editor and only in the editor so the solution to that is to keep it private and simply add the attribute serialize field and now the variable is still private so i still cannot access it from a second class so nope i still can't do this however now it does show up over here in the editor so i can now very easily iterate upon this value in order to find the perfect one and again the whole point is that this is not private so that still means that this field can only be accessed and modified by the code inside of this class and again that really helps you manage complexity so if there's any error whatsoever involved in this field you know for a fact that the error has to be somewhere inside of this class because you know guarantee that no other class anywhere else in the code base can modify this field so you can now safely iterate upon this value in the editor while knowing that all the code is still nice and clean with minimal complexity back to the previous example if you still wanted to modify the speed of the player you would instead make a public function to change it and now you could easily analog to see when this function was being called and who was calling it you could add some validation to make sure the field wasn't set to an invalid value and you could fire off an event inside that function for when the speed changes which the ui would then listen to instead of reading the field directly on every frame pushing this even further you would probably make the enemy apply some sort of slow down state instead of directly setting the speed the main point is the film variable is now directly inaccessible from outside of this class so any bugs would have to exist inside of this class and you could safely ignore the rest of your code base while you're focused on fixing this one bug sometimes i see people say something like well why don't i just make every single field public but that is really the opposite of what you should always be thinking you should always make your fields private by default and only make them public if you have a very good reason why it needs to be public so the question should be why should this not be private as your code becomes bigger and more complex keeping this simple rule in mind will help you immensely now i want to make one note here which is if you've seen the official tutorials made by unity you might have noticed that they do use the public parameter now the reason for that is not because the people at unity don't know better everyone that i've met from unity is very capable and very good at their job now the reason for that is simply for simplicity when teaching some complete beginners it is much easier to simply teach people to just make something public when they want to edit something in the editor rather than having to teach them clean code principles and attributes so as you are starting out keeping it simple takes precedence over writing some good clean code you can always learn that later so my hope with this video is that it gets recommended to beginners just after they learn the absolute basics so if it's your first time writing code then using public won't avoid any unnecessary complexity in the learning process however as you get more capable and you'll learn more and more then you should start to learn how to write better code rather than just writing code that works so if that's the stage that you're at right now then i hope that this video has shown you the benefits of writing clean code and why you should not abuse public again if you're looking for a more guided path with step-by-step lectures then check out my complete courses alright hope that's useful check out these videos to learn some more thanks to these awesome patreon supporters for making these videos possible thank you for watching and i'll see you next time
Info
Channel: Code Monkey
Views: 70,518
Rating: undefined out of 5
Keywords: unity clean code, c# clean code, unity serializefield, unity public or private, code monkey, c# serializefield private, c# public private, clean code practices c#, serializefield, clean code, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity, game design, game development, game dev, game development unity, programming, coding, c#, code, software development, learn to code, learn programming, unity tutorials
Id: pD27YuJG3L8
Channel Id: undefined
Length: 8min 26sec (506 seconds)
Published: Mon Aug 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.