No video

Why you should NOT make everything PUBLIC!

  Рет қаралды 119,799

Code Monkey

Code Monkey

Күн бұрын

🌍 FREE C# Beginner Complete Course! • Learn C# Beginner FREE...
🔴 Watch my Complete FREE Game Dev Course! 🌍 • Learn Unity Beginner/I...
🌍 HumbleBundle MEGAPACK cmonkey.co/hum...
✅ Unity Summer Sale (ends soon) assetstore.uni...
🌍 Get my Complete Courses! ✅ unitycodemonke...
👍 Learn to make awesome games step-by-step from start to finish.
🎮 Get my Steam Games unitycodemonke...
💬 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! ✅ unitycodemonke...
👍 Learn to make awesome games step-by-step from start to finish.
🌍 Get Code Monkey on Steam!
👍 Interactive Tutorials, Complete Games and More!
✅ store.steampow...
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 / unitycodemonkey
🎮 Grab the Game Bundle at unitycodemonke...
📝 Get the Code Monkey Utilities at unitycodemonke...
#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 - / unity3d
Brackeys - / brackeys
Dani - / @danidev
Jabrils - / @jabrils
BlackthornProd - / @blackthornprod
Sykoo - / sykootv
Jason Weimann - / @unity3dcollege
Jonas Tyroller - / @jonastyroller
--------------------------------------------------------------------
- Website: unitycodemonke...
- Twitter: / unitycodemonkey
- Steam: store.steampow...

Пікірлер: 461
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
🌍 FREE C# Beginner Complete Course! kzfaq.info/get/bejne/priVhZmLntivXaM.html 🔴 Watch my Complete FREE Game Dev Course! 🌍 kzfaq.info/get/bejne/d9N3hqh5nLfHdJs.html 🌍 HumbleBundle MEGAPACK cmonkey.co/humblebundle ✅ Unity Summer Sale (ends soon) assetstore.unity.com/?aid=1101l96nj&pubref=notpublic 🌍 Get my Complete Courses! ✅ unitycodemonkey.com/courses 👍 Learn to make awesome games step-by-step from start to finish.
@Sir_Isaac_Newton_
@Sir_Isaac_Newton_ 3 жыл бұрын
I really can't watch the video with its white theme.
@Veyron104
@Veyron104 2 жыл бұрын
Я новичёк: Ненавижу приватные переменные. Целую гору кода писать для того, чтобы достать информацию из этой переменной или её туда записать. Кодеры странные люди: могут продумать весь функционал программы, что на какие классы разбить и какой функционал должен быть у этих классов, но не могут запомнить что player.speed = 10 это изменение скорости персонажа, а не чтение? Уверен, что видео должно называться: Why you should NOT make EVERYTHING poblic! Вам нужно посмотреть фильм "Yes Man". Сначала ты используешь "public" везде, потом тебе говорят что это плохо, и ты везде используешь "private", а потом понимаешь, что там где нужно закрыть переменную, и подписать на изменение этой переменной какое-то событие, то используешь "private", а во всех остальных случаях "public"
@Falesam
@Falesam 3 жыл бұрын
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.
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
The goal is to constantly improve over time so it sounds like you're on the right track!
@--.--
@--.-- 3 жыл бұрын
4 years were thrown into the trash. How lazy you must be so that you don't want to think for at least 3 seconds about whether you need to give the variable public access. I'm not even talking about the SerializeField attribute. If you're not going to work and you make games only as an indie developer, then okay. But anyway you didn't have «code learning», you didn't learn how to code.
@Daniel-dk6xd
@Daniel-dk6xd 3 жыл бұрын
You got to all riled up about the dude leaving public variables, but I worked in a big tech company and you can't imagine how many big programmers (seniors, 15+ years xp) working in huge application projects just leave public variables left and right. It is a bad practice and doubles the workload sure. But it's not a mortal sin. Relax man, you sound stressed.
@aylanj123
@aylanj123 3 жыл бұрын
@@--.-- Dude that was too harsh, relax. Even people with tons of experience sometimes leave public variables thinking it will be needed that way in the future, and just forget about them past the time. It's a bad practice but no one is going to die due to that, just more workload.
@snesmocha
@snesmocha 3 жыл бұрын
@@--.-- Jesus Christ m8, no need to be so harsh. If you didn’t know about it then there’s no need to be so harsh. They aren’t showing an ego and they aren’t Being an ass about it so no need to be an ass
@esosek
@esosek 3 жыл бұрын
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!
@JW-uu9je
@JW-uu9je 2 жыл бұрын
Events are also good for reducing dependencies but it becomes ever more important to have good documentation as you may not receive errors in the console when things are not working properly.
@freezinfire
@freezinfire 3 жыл бұрын
I did wonder about this when I started learning coding, never knew why until now. Thank you for the knowledge.
@ahmadel-baher2779
@ahmadel-baher2779 3 жыл бұрын
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.
@ivopfaffen
@ivopfaffen 3 жыл бұрын
Amazing video! It'd be really cool if you turned this into a series about good coding principles for Unity :)
@coderanger7708
@coderanger7708 3 жыл бұрын
That light mode IDE offends me
@_GhostMiner
@_GhostMiner 3 жыл бұрын
But i guess nobody cares 🤔
@raoufbensalem3417
@raoufbensalem3417 3 жыл бұрын
@@_GhostMiner it hurt my eyes
@raoufbensalem3417
@raoufbensalem3417 3 жыл бұрын
@@anonymous49125you want to be blind ?
@wearwolf4202
@wearwolf4202 3 жыл бұрын
All hail dark mode!
@wearwolf4202
@wearwolf4202 3 жыл бұрын
@@raoufbensalem3417 then don't watch. Not everyone likes dark mode
@KineticCode
@KineticCode 3 жыл бұрын
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.
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
Many thanks! I hope you put those assets to good use!
@brocklawson6099
@brocklawson6099 3 жыл бұрын
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.
@Kortonox
@Kortonox 3 жыл бұрын
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.
@DarthBiomech
@DarthBiomech 9 ай бұрын
You just blew my mind. Like, seriously, thank you. I have so many code in OnSerialize() that was there solely for the purpose of me not accidentally setting incorrect value to the variables...
@glorytoukraine429
@glorytoukraine429 3 жыл бұрын
Code Monkey, thank you for keeping and not removing subtitles that are automatically generated by KZfaq (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.😜
@davedipy04
@davedipy04 3 жыл бұрын
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
@tigransahakyan2840
@tigransahakyan2840 3 жыл бұрын
This HAS TO become a series of clean code principles!
@JW-uu9je
@JW-uu9je 2 жыл бұрын
I recently picked up Code Complete 2 from your video with gamedevtv...I'm really enjoying it! Thank you
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
That's awesome! I hope it helps you just as much as it helped me!
@stephenperry1006
@stephenperry1006 2 жыл бұрын
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.
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
Thanks! Yup the simple things are sometimes the most important
@humadi2001
@humadi2001 3 жыл бұрын
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🌹
@vaflov
@vaflov 3 жыл бұрын
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.
@oldcat1790
@oldcat1790 Жыл бұрын
Well, that's signs of bad coding practices. Reflection for instance is a VERY costly operation that should be used only if you have no other choice, so if you find yourself using it more than a few times then the design of your logic is clearly wrong. Even more so if it used every frame. Likewise, it's better to use protected abstract and virtual functions to allow people to write their custom logic via child class if they need to, because it provides even better flexibility at no cost.
@yavvivvay
@yavvivvay Жыл бұрын
@@oldcat1790 it's all fun and games when you call your code from your code. Using other people code, especially old code, especially in ways it was not made to be used makes it a different story. And yes, it is a smell, but it is a better approach to use at most protected so user can do something that is outside your assumptions. Else you need reflection or other such hacks to get what you want.
@alexgeeves7096
@alexgeeves7096 3 жыл бұрын
Thanks dude! I'm exactly your target audience for this video and I appreciate it a lot
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
I'm glad you found the video and I'm glad you liked it! Thanks!
@DL-zv5xc
@DL-zv5xc 3 жыл бұрын
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!
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
It's a great book, I'm glad you're learning a lot from it!
@thomasremastered6941
@thomasremastered6941 Жыл бұрын
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.
@RobertGuilman
@RobertGuilman 3 жыл бұрын
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
@Lonzy
@Lonzy 2 жыл бұрын
Thank you soo much, I just finished converting all my scripts to Private and Serialized Fields. Feels good having clean code :)
@IngoLingoAnimations
@IngoLingoAnimations 3 жыл бұрын
I like learning the "why" to certain aspects of coding.
@FeedMyKids1
@FeedMyKids1 3 жыл бұрын
Code Complete is the BEST book I've ever read.
@MarushiaDark316
@MarushiaDark316 3 жыл бұрын
I think part of the reason for beginnings to use public for everything is not yet knowing how to make Actions, Events, Accessors, Interfaces, and Constructors work. I would say I'm intermediate level and only just coming to grasp with these myself, but starting to internalize it and using them more and more.
@ruliw8520
@ruliw8520 3 жыл бұрын
After remaking my project from scratch and use private most part of the time and not public as i used to do i had the same conclusion of this video, public variables are like those bikes with training wheels, they're great for complete begginers, but as time goes on and things get more complex, the training wheels most part of the time will only limit what experienced people can do instead of helping
@mehmedcavas3069
@mehmedcavas3069 3 жыл бұрын
I haven't used public variables for almost 2 years. If I have to access the variable I create a property like public float Speed => speed; so they can access it but can't change it. for changing I use methods which is also still a rare case
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
Yup same thing here, I pretty much only use public for singletons and constants. public static GameManager Instance { get; private set; }
@StarkTech47
@StarkTech47 2 жыл бұрын
Thank you for this video, you confirmed what I have thought since a long time now. I would be very interested to see you dive deeper into this topic with getter and setter, or juste using function, buff and debuff ... Very good video for an important thing when it comes to make an ambitious game !
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
If you want to learn more about clean code I can highly recommend the book Code Complete 2 And I just recently released my latest course which is heavily focused on writing good clean code to take your skills to the next level kzfaq.info/get/bejne/h6qiktOb38vIpmQ.html
@willpetillo1189
@willpetillo1189 2 жыл бұрын
This is great advice for beginners moving up to the intermediate level, but I have found that, in moving from the intermediate to advanced level, making things public is almost universally better--though the reasons are nuanced, address long-range issues, and assume some things. The problem with private variables arises when one transitions from writing game logic to tool logic (which is then assembled into game mechanics by juniors or non-programmers). At that point, the logic "only make something public if it has a clear reason to be public" breaks down because you can't possibly know the full range of use-cases in advance and so to lock something down imposes a permanent design limitation based on a lack of imagination. As a specific example, when working with Mecanim, one must maintain a code-based state machine to control characters with animation in addition to the animator state machine and keep both in sync with each other (or work around the system entirely with animator.Play("magicString")...all because someone at Unity couldn't think of a reason to access animation states other than the one currently playing and made that information private. This may sound like an edge case, but over the years, I have lost weeks if not months worth of development time working around tools that have made things private that I needed to access for some reason, and 0.0 seconds debugging problems that arose because something was public that should have been private. Further, in my own programming, I have recently been leaning heavily on a nested composition pattern, where every piece of code has the potential to become a building block for code that operates on a higher level of abstraction, and in that context it is often useful to change settings externally via OnValidate. Another, more nuisance-level problem is what I call "fake encapsulation". For example, this disturbingly-common nonsense: private myVariable; public MyVariable { get => myVariable; set => myVariable = value; } Assuming the above is not to complete an interface, the above pattern adds negative value. In addition to wasting space, it also creates the illusion that myVariable is protected from access from the outside, when in reality it does literally nothing other than require using a capital 'M' to access it. Now, I am making a couple of assumptions that mitigate the benefits of private variables/methods. First, I am assuming one has an IDE with right-click -> find all instances. Second, I am assuming that every...single...time one modifies a variable, especially one in a different script, one does so with a knowledge of the impact of changing that variable and the alternative ways of accomplishing the same goal (as opposed to just looking for something quick that "works"). I take it as an axiom that if new code breaks something, it is ALWAYS the fault of the new, accessing code, and NEVER the fault of the old code not putting up sufficient guard rails. And if the goal is communicating intent, that's what comments are for. By analogy, if a neighbor breaks into my house and steals my computer, the neighbor is to blame for the loss, not me for having insufficient security. Granted, being mindful about how variables are set is certainly good practice. For example, calling a Damage(int value) function instead of directly decreasing health is probably a good idea. This, however, is not for the sake of "protecting" the health state, but for the sake of readability, since damage is the concept being implemented and it just happens to result in decreasing health (possibly among other things).
@fuuuuuuuuuuuuuuuuuuuuuuuuu
@fuuuuuuuuuuuuuuuuuuuuuuuuu Жыл бұрын
Thank you. #vindicated
@oldcat1790
@oldcat1790 Жыл бұрын
Calling a Damage(int value) function is not for the sake of "protecting" the health state, but for the sake of keeping an eye on health changes. During debugging you can easily add debug.log into such function, unlike direct changes.
@willpetillo1189
@willpetillo1189 Жыл бұрын
As I said, treating Damage() as a process that happens to involve decreasing health rather than directly accessing the variable makes sense. And yes, having the potential to easily add debugging logic is one benefit of this process framing. That said, in a context where debugging is the only additional consideration--that is, when it actually makes sense to modify the variable directly because that is a better analogy for what is happening than a process--you could do so with setter logic. In any case, my point stands: whenever anyone actually argues that variables should not be public, the stated reason is pretty much always for the sake of "protecting" state, I'm inclined to take them at their word, and I don't consider this a valid reason.
@RazeVenStudios
@RazeVenStudios Жыл бұрын
Honestly I've struggled so much on this topic. Thank you for the guidance.
@dreadzitodev488
@dreadzitodev488 3 жыл бұрын
Pls more Clean Code practices! I love knowing this things
@migcreatesgames2622
@migcreatesgames2622 3 жыл бұрын
Great explanation process! I do try to avoid public variables and only using for Game Objects or Transform. You should also do an episode on public static variables. I try as much as I can to stay away from static and rather do properties or pass reference.
@migcreatesgames2622
@migcreatesgames2622 3 жыл бұрын
@@EvitoCruor You can still use the public static classes for example I will use them for an inventory class. The issue comes when you are getting an error when changing a public static variable and forget where the variable and change is coming from. Most of my games have 60 or more scripts : / better than doing one long script
@clarkmeyer7211
@clarkmeyer7211 3 жыл бұрын
as a beginner I think learning serializefield is super important because of this exact reason. I find it very helpful when applying this to prefabs where you need a different variable in each situation.
@saito853
@saito853 3 жыл бұрын
if you want to take it to another step, fields should NEVER be public. They should be exposed through properties or methods (just do a quick Google search on Properties vs public fields). In C# you can create a public auto properties like this: public float Speed { get; set; } //The naming convention for Properties is PascalCase However in Unity, public properties will not be exposed in the Inspector, this is because a Property is literally just a layer of abstraction which includes a private backing field and two public methods, you can think of the property I declared above like this: private float speed; public void SetSpeed{float value} => speed = value; public float GetSpeed() => return speed; So in order to expose Properties to the inspector, you have to expose the backing field (which is normally hidden if you use auto properties) by using the [field: SerializeField] attribute, which basically tells the compiler to apply [SerializeField] attribute to the backing field of this property. [field : SerializeField] public float Speed { get; set; }
@sharif47
@sharif47 3 жыл бұрын
And here I was expecting you to talk about why going public (as Unity did some time ago) was a bad idea.
@REOsama
@REOsama Жыл бұрын
This is literally the best explanation
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
I'm glad you liked it, thanks!
@progCan
@progCan Жыл бұрын
very good tutorial, i was always wondering why you make everything serialize field. this explains it very good.
@JaredKuvent
@JaredKuvent 3 жыл бұрын
I'm just starting out with C#, but not unfamiliar with some scripting. Regarding beginner's tutorials teaching to use Public fields: wouldn't it be better to teach the best practice early on? I know it could be a rabbithole of theory before practice, but as a new developer, it can be confusing to learn one way and then discover I should be doing it differently. As I was told when learning piano, "practicing with the wrong technique will only make the learning harder in the long run." However, maybe the important thing is to just jump in, without too much theory, since taking the initial plunge is often the most difficult.
@sebastianp.332
@sebastianp.332 3 жыл бұрын
I have made some games before but I still had some doubts regarding why something should be public or private. This is a great explanation, thank you very much!!!
@AJ213Probably
@AJ213Probably 3 жыл бұрын
One thing I like to do is have readonly accessors for my private fields if other classes need that information. So if we have private float speed; we would also have public float Speed => speed; so that we can read the speed value but not write it. Also => in this case is the short hand for public float Speed { get { return speed; } } You can also do this for functions too if it's a single line
@SteveTheExploiter
@SteveTheExploiter 3 жыл бұрын
Thank you! Finally! So many tutorials just toss about the public variables and it's very disappointing. SerializedField is also good for debugging as you can monitor the variable's value in the inspector in real time.
@GrantCarvalho
@GrantCarvalho 2 жыл бұрын
I enjoyed and learned so much from your videos, Code Monkey! I went ahead and bought your course on Udemy. Thank you!
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
Thanks! I hope you enjoy the course!
@richnaisanze
@richnaisanze 3 жыл бұрын
Pleaseeeee keep posting videos about "Clean Code" in Unity, I'm very interested about clean code, Thank youuu!
@KushagraPratap
@KushagraPratap 3 жыл бұрын
Your videos give me feeling of a school and teachers teaching like in a session, especially the polls, keep it up
@iSTOR
@iSTOR Ай бұрын
C# allows you to define getters and setters that allows to control and access directly to public variables but read and writing will be performed thru getters and setters. That makes obsolete the must-to-be-private members with manually defined getters/setters.
@CodeMonkeyUnity
@CodeMonkeyUnity 29 күн бұрын
Sure you can use properties instead of get/set functions, but you still need SerializseField to expose it in the editor
@iSTOR
@iSTOR 29 күн бұрын
@@CodeMonkeyUnity the point is that using private properties has not advantages over public ones once you can define implicit hidden getters/setters to handle reads/writes to those public properties... no advantages in using privates and all problems and all overhead... I prefer to use private properties only for... really private things that are used in code internals, whose are usually not serializalbe and of course never exposed to the inspector (they are private!!! inspector is the more public-like-thing... making a private property visible for the inspector is like making it super-public).
@alexanderdorenkamp
@alexanderdorenkamp 3 жыл бұрын
Awesome vid! Can you make more clean code videos?
@Skyefaux
@Skyefaux 3 жыл бұрын
I would love a video series on general architecture and design patterns and how they are used in game dev
@fernandog9647
@fernandog9647 2 жыл бұрын
really cool video, I always had the doubt of how to improve the code and not make everything public, I had heard that it was wrong to use public variables and now I understand why, thank you!
@vast634
@vast634 3 жыл бұрын
The ominous "accidental modification". There is a cost in hiding access: it create lots of additional boilerplate code for public getters and setters. The cost is a longer time to scroll to the now longer sourcecode, with the programmer having less overview. Unless the code is ment for others to use, such as in a library or module, adding lots "security" to the code can have a negative effect. It sufficient to make the interface to that module safe.
@sharif47
@sharif47 3 жыл бұрын
I'm having a feeling that the "accidental modification" part doesn't really change much with private variables and public functions. If a script has a variable and a function for changing the variable, then how is it any less troublesome to change it from the function, rather than the variable itself? If an error occurs anywhere, this means either the errors are coming from whichever function is accessing it, which was the exact same problem I was having before... Or the problem is actually in the function I created, which means there's a new boilerplate code that I'll have to search as well. Maybe the new function can also include some code that tells who is calling it, but this code might need to be written off in the end for performance issues. The only place where I see are where the function happens to be both complex and general at the same time. For example, when my player dies, the player gameobject disappears along with some extra stuffs to show the player died. If I code all of it newly in every script that can kill me, then it's a lot of code to search for error correction. But if I do one death function to hadle all of it, I can easily check what caused it. But for something so small as getting and setting values, this looks redundant to me.
@Soloman274
@Soloman274 3 жыл бұрын
More Clean Code principles in Unity context! ❤️
@moheyaldeenalmardiny82
@moheyaldeenalmardiny82 3 жыл бұрын
Amazing video and very important idea I wish we could see a series from this kind of videos
@unofficialSummer
@unofficialSummer 3 жыл бұрын
I saw the thumb and thought that it was gonna be a video about LIFE lol, no problem though, keep it up
@buddyroach
@buddyroach 3 жыл бұрын
btw, that public variable you made called `speed` cant be used by other scripts THAT easily. You have to call the script in order to call it's public variables so Idky people act like its the other way around.
@brandenimmerzeel8182
@brandenimmerzeel8182 3 жыл бұрын
Great video! A similar one could be the implications of singletons as I feel like they are something I used to abuse alot in the early days too (and still do when im lazy.)
@nosh247
@nosh247 3 жыл бұрын
This is nice! I do it by default, even when I didn't knew why, I really don't like when i bump into some experienced programmer that says, we'll fix it later (making unnecessary public variables for testing) which end up being part of the code and left as public...
@itztobii4869
@itztobii4869 3 жыл бұрын
You should never expose a Field public. If you want to change it, use Methods or Properties. The official C# documentation says: Never expose the inner workings of a class.
@Joy-dn8yz
@Joy-dn8yz 3 жыл бұрын
except cases when you need to work through the pointer with some of pointer's object's methods so you make it public. Because if you not, you need to make methods to call methods which change private fields
@henrycgs
@henrycgs 3 жыл бұрын
oh my god, no. that's some dumb, old timey, Java verbose nonsense. this video is a disservice to actually writing clean code. yes, you shouldn't expose the inner workings of a class. which includes the inspector! if something is visible in the inspector, then it should be visible from other classes as well! this is literally what every built-in Unity component does!!
@c1oudsky
@c1oudsky 3 жыл бұрын
@@henrycgs I mean, unity can compile them as just private since you don't need (and not really able) to use the inspector in the build?
@henrycgs
@henrycgs 3 жыл бұрын
@@c1oudsky I don't know what you mean. Compiled code doesn't have encapsulation as far as I know.
@pixeldustinteractive
@pixeldustinteractive 3 жыл бұрын
I think a better way to manage accessibility is with namespacing. Obviously these should be used together for best results
@JW-uu9je
@JW-uu9je 2 жыл бұрын
also what you say from 6:00 - 6:40 is excellent!!!
@jamesmillerjo
@jamesmillerjo 3 жыл бұрын
Nice step-by-step OO lecture
@aaronedvalson104
@aaronedvalson104 3 жыл бұрын
I used to just shrug it off and do it my way, with everything public. Once I got a big enough project, I realized a few of the reasons we shouldn't make everything public. Now, I actually try to anticipate how exposed it'll need to be, and work from private to public.
@r3mus89
@r3mus89 3 жыл бұрын
great video, thank you! I also like that this way the auto completion stays nice and clean.
@Aidan-dc3zr
@Aidan-dc3zr 3 жыл бұрын
I personally prefer to keep things as protected rather than private because it's pretty much the same as private except it makes code written more expandable later on without changing anything.
@philipkaltsounis8097
@philipkaltsounis8097 3 жыл бұрын
You can also make a variable with a public get and private set. That way other classes can check the value of the variable but it cannot be modified
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
That would be a property, not a variable, and yes properties can indeed be used for validation and limiting get/set
@Only4GamersXyz
@Only4GamersXyz 3 жыл бұрын
Hello, Is there any difference between float speed; vs private float speed; should I always use private?
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
Some compilers default to private, others to protected. On a class hiding it might make it private, on an interface it will make it public. So it's always best to be clear and specify exactly what accessibility you want.
@Only4GamersXyz
@Only4GamersXyz 3 жыл бұрын
@@CodeMonkeyUnity Thanks for the reply. What is default in unity?
@brellos7220
@brellos7220 10 ай бұрын
Great video!!! It's really helpful.
@Dritix
@Dritix 3 жыл бұрын
Helpful video, keep it up!
@rezaanataj6978
@rezaanataj6978 3 жыл бұрын
Thank you for your wide verity types of videos. Your Chanel is the best for learning unity tips and tricks I just wanted to know if you have a plan doing a series of videos to cover making textures from beginner to pro as they can be used every where?
@ChillinGames
@ChillinGames 3 жыл бұрын
another tip is if you want a field to be public but you DONT want to add it to the growing mess on the editor, make it internal.
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
For that you can also use the attribute [HideInInspector]
@bonolenovndongo4557
@bonolenovndongo4557 3 жыл бұрын
Your tutorials are amazing. Thank you.
@guridoraccoon6375
@guridoraccoon6375 3 жыл бұрын
Great advice. Thank you so much
@alejandroalzatesanchez
@alejandroalzatesanchez 3 жыл бұрын
The title sounds like privacy informative video That's is .POG.
@lasttrump6015
@lasttrump6015 Жыл бұрын
Interesting but I found making everything public helped me in my coding a lot. I just have a strict rule never to update a public variable in more then one spot and all the code can read it but only updated in one place, so if other things needed to influence it then create new variable for that and still update the main variable in the one place with those adjustments if needed. I also of course use longer and more detailed variable names to reduce conflicts and also a full list of all used variables in a table I can check is not already used elsewhere....not hard and solved a lot of conflict problems that way. The full variable table was good to also sort between what needs to be saved in the game save state and what is not needed. I also have tended towards functional programming which allows local variables that are not global by default anyway (unless I want them to) so also helps a lot in avoiding conflicts.
@arcday4281
@arcday4281 3 жыл бұрын
Useful for beginners ! But following carefully any of your videos related to the code, you can instantly learn how to write clean code )))
@B4NTO
@B4NTO 3 жыл бұрын
Great vid thanks :)
@arkfish
@arkfish 3 жыл бұрын
Exactly. Code that is only used internally by one class can be private, there's no harm in doing so, and it will benefit you in the long-term. Unnecessarily publicizing everything will also make your properties explorer a lot more cluttered, since now if you want to edit a variable you're going to have to search through a lot of other variables. I'd only publicize fields if I need to for debug purposes or to tweak values without recompiling every time I change it
@epsilonray
@epsilonray 3 жыл бұрын
1. what is the difference between a private variable and one with no definition? 2. should you use the hideInInspector atribute when you dont want to modify the variable in the inspector?
@Wobling
@Wobling 3 жыл бұрын
1. variables in C# are private by default so you don't need to explictly state private but you should for readability. 2. HideInInspector is only really useful if you aren't using serialized fields. If you don't want it to be in the inspector just use private. I've never found the need for HideInInpector when using clean code principles.
@GiraPrimal
@GiraPrimal 3 жыл бұрын
@@Wobling A quick note on your 1st point: - This is true in a class or a struct: members are private unless you explicitly state otherwise. - In an interface or a namespace (or even outside of any namespace), members are public by default. As you mentionned, it's a good practice to explicitly write the accessibility level, even if it becomes redundant with the default behavior.
@Wobling
@Wobling 3 жыл бұрын
@@GiraPrimal This is correct, thanks for the addition :)
@scrillagorilla4524
@scrillagorilla4524 10 ай бұрын
love your videos bro. some day soon when i get a job in the field i'll donate. thank you
@CodeMonkeyUnity
@CodeMonkeyUnity 10 ай бұрын
Best of luck in your learning journey!
@Drillgon
@Drillgon 3 жыл бұрын
I thought you meant open source by public at first and it took me a whole minute to realize that "public" meant the access modifier.
@misterrhombus
@misterrhombus 2 жыл бұрын
How would you go about changing a private variable from another script? Like the SetSpeed() you mentioned? Won't public functions make your code less scaleable (I could be misunderstanding something)?
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
Yes exactly, "less scalable" can also be described as "less accessible" which is exactly the point, you want to limit access as much as possible to limit how many possible things can go wrong. It's much easier to find references of all SetSpeed(); function calls than all read/write direct access to the variable itself.
@404waffles
@404waffles 3 жыл бұрын
im not even in gamedev and this is pretty useful
@FM_GOBi
@FM_GOBi 3 жыл бұрын
Hey Code Monkey, have you written unit tests for your games? Do you have plans on covering that as a tut perhaps?
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
I don't normally do unit tests for my games since I have to work insanely fast in order to find success with those games, but I am constantly trying to write code that could be testable. So I'm constantly thinking about how to minimize dependencies and make every script as standalone as possible. Unity does have a bunch of Unit Testing tools which I've never used but I'd love to research them when I have the time.
@grazeen
@grazeen 3 жыл бұрын
Hi, I am following your videos for a while now. I have a friend who is deaf and watching your videos, i sometimes explain him what are you talking about and what are you saying(auto subtitles are messy). So if you open subtitle adding feature i will be happy to help adding subtitles. Thanks in advance. Keep up good work :)
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
KZfaq disabled community subtitles a while ago, it's no longer something that can be done, there's only the auto generated ones.
@nilsleonhardtdev
@nilsleonhardtdev 3 жыл бұрын
Very good tip, imma use it from now on!
@BreadAckerman
@BreadAckerman Жыл бұрын
Now i have knowledge. Bye gotta go and change every variable to sterelized private
@xwestlightx
@xwestlightx Жыл бұрын
i justt started learning with Unity. after this video im wondering why i would ever use the public version now instead of a Serializedfield? should i avoid this completly or are there some circumstances where the public version still makes more sense to use? thanks for the great videos btw.! im learning alot from you!
@miz0n3-
@miz0n3- 3 жыл бұрын
Just asking : How did you started your game development career? I mean what kind of game engine you used and programming language as well. Just want to know, to have a basic idea. Iam currently having a hard time in using unity. Specially at coding but I love to code and want to do so. Your channel helps a lot to fix them. Thanks for your support through videos. ❤️
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
I covered my game dev journey here kzfaq.info/get/bejne/ZuCjlrOXxLuVho0.html Basically I started making IRC scripts, then moved onto Flash and finally when I wanted to make PC games I found Unity and I've been using it ever since.
@jean-michel.houbre
@jean-michel.houbre 3 жыл бұрын
Well explained. Why did you change the recommended book? It seems to me that in your video on how to become a developer, you recommend "Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin Series)" ?
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
Both are great and both cover the same topic. Code Complete is a lot more thorough at 900 pages whereas Clean Code is much more concise. So to get a base understanding of clean code principles, maybe start with Clean Code first. To get a thorough understanding of every specific element that makes good code, go with Code Complete. Or really the best of all just get both!
@kitsunedotexe3587
@kitsunedotexe3587 3 жыл бұрын
beginner here, thank you!
@gta5onlineshorts
@gta5onlineshorts 3 жыл бұрын
what's the difference between modifying public variable from other function and modifying private variable with its public function from other function
@odo432
@odo432 3 жыл бұрын
So many tutorials use Public variables and it's something that has always annoyed me. Unless you require external access, just make everything private. And a lot of the time if you require external access you can make use of get/set and have much greater control over access (eg make getting the variable public but setting it private etc). And if you need access to private variables via Unity then use [Serializefield] attribute.
@michaelberthaud
@michaelberthaud 3 жыл бұрын
I think the real problem is not that field is public but the way those systems are constructed to interact with one each other is inherently hectic. making the variables private doesn't solve nor improve how the system would interact with each other. If anything making things blindly private has the potential to add more complexity to the system since you're adding layers of abstraction to your data fields. Now should you have a bug involving that data field, instead of just looking at who accessing the field directly, you gotta sifting through a bunch of different methods to find the problem.
@No_Ghosts
@No_Ghosts Жыл бұрын
yes! a private var with a public accessor function is more difficult to debug and scales worse than a simple public var.
@bladonski
@bladonski 3 жыл бұрын
Never seen your channel before and thought you were gonna be talking against open sourcing applications lol
@lovegeneration8464
@lovegeneration8464 3 жыл бұрын
Make more videos like this please.
@sadpancake6563
@sadpancake6563 3 жыл бұрын
You should not use public fields at all. Use methods and properties (properties are actually methods) to change them (encapsulation)
@lennyseed9255
@lennyseed9255 3 жыл бұрын
The problem that I face while keeping as much as possible private is that I just dont understand on how to reference something private in one script from another script. It sometimes not work for me even if I copy directly from someone else's scripts
@reactga
@reactga 3 жыл бұрын
Is there any adventage of serialisefield over protected? Edit: I realised this should not even be a question since they work mostly differently
@marcosborregales1569
@marcosborregales1569 3 жыл бұрын
they are two things. SerializedField makes it both appear in the editor as well as "save" (serialize) the value. Protected is a type of Private that is used when using Inheritance. When a class inherits from another, the derived class can access/see/read protected values, not private.
@diligencehumility6971
@diligencehumility6971 3 жыл бұрын
Serialized field, means the value of the field will be serialized. (For C# in general. For Unity, it "means" you can modify the value in the editor, even it's private) Protected is like public and private. Protected means it can only be accessed by child classes (and itself)
@_GhostMiner
@_GhostMiner 3 жыл бұрын
*advantage
@reactga
@reactga 3 жыл бұрын
Thanks for the replies, but for a simpler explanation, I did a quick search (which I should have done before obviously) and from what I understand of post, the difference is, while they both show up in the inspector, serialisefield variables can be modified from within editor while protected variables can't be modified, it uses the value set in code or it's default value if none is set.
@marcosborregales1569
@marcosborregales1569 3 жыл бұрын
@@reactga actually not correct, if you add the SerialiazedField attribute to a protected field, it does indeed show up in the inspector. I am doing that right this second :)
@ihateflies5605
@ihateflies5605 3 жыл бұрын
Damn yes yes yes I started doing this a while back and I maybe use a public variable like once a week
@waguno
@waguno 3 жыл бұрын
A must watch vídeo
@blablabla1196
@blablabla1196 2 жыл бұрын
Great video! Although in my experience I think its a good idea to first start out making variables public. Because the reality is, is that you will not be sure what you want to be private or public just yet. And then when you are 100% sure on which ones need to be private, change them to private. So why not do it in reverse? Start private? Then set public if needed? Because then you don't have to set Serialize Field EVERY variable. It's a better workflow if you are experimenting on a game, to just code free spirited at first and then clean your code as you go. Just my 2 cents.
@Lps-bx7ck
@Lps-bx7ck Жыл бұрын
2 questions I have about this: 1 - Would this mean having all variables private and setting them trough public methods? 2 - Using this logic, can I still use {public get; private set;}? I say this because having all variables private would make hard to get them on other scripts, so I can use my second question to get them, or use a public method to do so?
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
Yes exactly and yes you can use properties as accessors. Personally I prefer get() set() functions but properties can work as well, the point is to be able to add some extra logic so you're not accessing the variable directly.
How to Fix NullReferenceException in C#! (4 Step Process)
16:10
Code Monkey
Рет қаралды 83 М.
Steam EXPERT teaches you Game Marketing for SUCCESS!
52:22
Code Monkey
Рет қаралды 60 М.
Meet the one boy from the Ronaldo edit in India
00:30
Younes Zarou
Рет қаралды 19 МЛН
English or Spanish 🤣
00:16
GL Show
Рет қаралды 7 МЛН
Before VS during the CONCERT 🔥 "Aliby" | Andra Gogan
00:13
Andra Gogan
Рет қаралды 10 МЛН
7 Days Stranded In A Cave
17:59
MrBeast
Рет қаралды 95 МЛН
What are Interfaces? (C# Basics)
13:54
Code Monkey
Рет қаралды 206 М.
The Most Legendary Programmers Of All Time
11:49
Aaron Jack
Рет қаралды 552 М.
Dear Game Developers, Stop Messing This Up!
22:19
Jonas Tyroller
Рет қаралды 707 М.
What are Events? (C# Basics)
15:05
Code Monkey
Рет қаралды 387 М.
What are Delegates? (C# Basics, Lambda, Action, Func)
18:39
Code Monkey
Рет қаралды 290 М.
How to ACTUALLY get into Gamedev
14:01
DaFluffyPotato
Рет қаралды 718 М.
Blueprints vs. C++: How They Fit Together and Why You Should Use Both
47:14
20 Advanced Coding Tips For Big Unity Projects
22:23
Tesseract
Рет қаралды 179 М.
Meet the one boy from the Ronaldo edit in India
00:30
Younes Zarou
Рет қаралды 19 МЛН