How to Write High Quality Code that doesn't fall apart. — Transcript
Full transcript
- 0:00Hello and welcome. I'm your code monkey.
- 0:02All right, so welcome to my talk which
- 0:03is titled how to write game code that
- 0:05doesn't fall apart. This is going to be
- 0:07all about clean code. So what is it and
- 0:09why it's something so important? So this
- 0:12is my goal with this song. So my goal is
- 0:14for you to learn how to write high
- 0:16quality clean code so you can make some
- 0:17proper professional games as opposed to
- 0:19just disposable prototypes. That's an
- 0:21issue that a lot of people have,
- 0:22especially a lot of beginners. They can
- 0:24make some prototypes and sure they write
- 0:26whatever kind of code to get it to work.
- 0:27But if you want to be a prop
- 0:29professional developer, if you want to
- 0:30make some actual proper games, if so,
- 0:32you need to take some good practices. So
- 0:34you end up building some actual games as
- 0:36opposed to some disposable prototypes.
- 0:38So to achieve this, we're going to go
- 0:40through a bunch of things in this talk.
- 0:42So first of all, we're going to cover
- 0:43what is and what is knuckling code.
- 0:45Starting off with some nice definition
- 0:46so we know exactly what we're talking
- 0:48about. Then we're going to cover why
- 0:50does all of this matter if the code just
- 0:52works. This is a common objection that I
- 0:54get whenever I talk about the importance
- 0:56of writing clean code. So I won't cover
- 0:58that. I won't cover how just the code
- 1:00working is not enough. Making it proper
- 1:02is the way that it should be done. There
- 1:04we go. Talked about one of the most
- 1:05important things. So if you take just
- 1:07one thing out of this talk, I hope you
- 1:08take this naming rules. They are pretty
- 1:10simple to implement and this one change
- 1:12alone will massively increase the
- 1:14quality of your code. After that we are
- 1:16going to talk about how decoupling is a
- 1:19superpower. Decoupling is basically the
- 1:21process by taking your systems and
- 1:22making them as separated as possible. So
- 1:24while you're working on one thing, you
- 1:25just focus on that one thing and nothing
- 1:27else. After that, we're going to talk
- 1:30about the single responsibility
- 1:31principle. This is another one that is
- 1:33greatly going to improve the quality of
- 1:35your code. It's how each thing, each
- 1:37class, each function should do one thing
- 1:38and one thing only. After that, we're
- 1:41going to cover the process of
- 1:42information hiding. This is meant to
- 1:44help you basically manage the mental
- 1:46load you have to keep in your head. So,
- 1:48as you're working on some kind of game,
- 1:49you don't have to keep a massive amount
- 1:51of code in your head as you're working
- 1:52on it. If you minimize how much you are
- 1:54actually exposing, if you hide as much
- 1:56information as possible, that is going
- 1:58to help you out quite a bit. Then, we're
- 2:00going to cover one of the easiest ways
- 2:02basically to make debugging the easier
- 2:04possible. We're going to cover the
- 2:05process of how one source of truth, so
- 2:07each piece of that in your game should
- 2:09exist in one place and one place only as
- 2:11opposed to having copies all over the
- 2:12place, which like I said, that will help
- 2:14with debugging by quite a bit. After
- 2:16that, we're going to cover a slightly
- 2:18more controversial topic on
- 2:19self-documenting code. So, this all
- 2:21about comments. Should you write
- 2:23comments? Should you not write comments?
- 2:24When to use them, when not to use them.
- 2:26So, here I won't cover that topic. Then
- 2:29a fun one. So, topic about magic
- 2:31numbers. We're going to cover what
- 2:32exactly they are and why you should not
- 2:34use them and what you should use
- 2:36instead. Then we're going to talk about
- 2:38topic of strings as identifiers, which
- 2:40is something you should also not do. You
- 2:42should not use strings to identify
- 2:43anything. So again I will talk about why
- 2:45you shouldn't use them. What are the
- 2:47main problems with it and what are the
- 2:48alternatives that I recommend. Then on
- 2:51topic of composition over inheritance.
- 2:53So when beginners first start learning
- 2:54about object-oriented programming
- 2:56usually they tend to overuse inheritance
- 2:58way too much. So over here I want to
- 3:00give you basically a better alternative
- 3:02focusing on composition instead of
- 3:03inheritance. After that comes also a
- 3:06very important thing which is
- 3:07refactoring. So basically everything
- 3:09we're going to talk uh everything we're
- 3:11going to cover in this entire talk is
- 3:12not meant to be something that you run
- 3:14just once but rather it's meant to be a
- 3:15constant iterative process. So over here
- 3:17the final topic is all about that all
- 3:19about how refactoring is a constant
- 3:20process. So we should be applying all of
- 3:22these guidelines. We should be applying
- 3:24them not once but over and over again.
- 3:27Now this is meant to be a very practic.
- 3:29It is not meant to be just nonsense
- 3:32theory that is just meant for academics
- 3:34or something like that. No, this is
- 3:35meant to be very very practical. So
- 3:37after you watch this video, I want you
- 3:38to take all of this and immediately
- 3:40apply them to your code and you'll see
- 3:41how the code quality will be much much
- 3:43better. So before we get to some
- 3:46specifics, just a quick example so we
- 3:48know what we're talking about. So a
- 3:49quick example of bad code versus clean
- 3:50code. So over here here we have example
- 3:53of some pretty terrible code. So it's a
- 3:55pretty small piece of code. So just by
- 3:56looking at it, you can probably guess
- 3:58what exactly this is doing. But still
- 4:00takes you quite a bit of time trying to
- 4:01understand, okay, what do all these
- 4:03variables represent? What does the class
- 4:05represent? what are these functions
- 4:06doing? Technically, like I said, it's a
- 4:08very small thing. So, if you think about
- 4:09it for a few minutes, you can figure out
- 4:11what this is doing. But still, it
- 4:13requires quite a lot of mental effort
- 4:14trying to figure that out. Whereas over
- 4:16here, here we have the exact same code,
- 4:18but now written with some proper clean
- 4:20code practices. And we can see very
- 4:21instantly, okay, that one takes you a
- 4:24few minutes to understand. Whereas this
- 4:25one over here, very instantly
- 4:26understandable. You just read the names
- 4:27and you can instantly understand, okay,
- 4:29this is a health system. We've got a
- 4:31health field. We've got functions to
- 4:32damage, to heal, and so on. So, yep,
- 4:34just at a glance, we can see how both of
- 4:36these, that one is very difficult to
- 4:38understand, whereas this one is very
- 4:39easy to understand. That is the entire
- 4:41point behind writing clean code. Also, I
- 4:43should point out how over here, the
- 4:45compiler doesn't care. These two are
- 4:47basically exactly the same thing as far
- 4:48as the compiler is concerned. The
- 4:49compiler really just converts both of
- 4:51these into binary and then just executes
- 4:52it. So, everything we're going to talk
- 4:54about, all these clean code guidelines,
- 4:56they're really meant for your benefit as
- 4:58a programmer. The compiler doesn't care.
- 4:59The code runs just the same. So this is
- 5:01all meant to help you as a programmer
- 5:03being able to work easier and with fewer
- 5:05issues. Also since I point out the
- 5:08massive amount difference that it takes
- 5:10between understanding that version and
- 5:11this version which is what I was saying
- 5:13a while ago difference between making a
- 5:15disposable prototype versus making a
- 5:16proper game. So if you're working on
- 5:18some kind of game for a short amount of
- 5:19time doesn't matter too much but imagine
- 5:21so that one takes you 10 minutes to
- 5:22understand whereas this one takes you a
- 5:24few seconds. Imagine that spread across
- 5:26making a proper project that takes 6
- 5:28months to one year and you can see the
- 5:30massive time savings that you get if you
- 5:32understand your code in a few seconds
- 5:33versus several minutes. Now, before I
- 5:36give you some specific advice, just who
- 5:38I am so you know where I'm coming from
- 5:40and why I can give you this advice.
- 5:42First of all, my name is Hugo. I love
- 5:43programming. I've been writing code for
- 5:45over 25 years. I started off a very long
- 5:47time ago back when I was 10 years old. I
- 5:49started writing merk scripts which I'm
- 5:51guessing nowadays most people don't know
- 5:52what is IRC. That was basically the chat
- 5:54rooms back in the day. Whereas nowadays
- 5:56you've got like WhatsApp groups and
- 5:57something like that. That was kind of
- 5:58the similar one. And that one I could
- 6:01basically customize a program in order
- 6:02to make some chat bots. So I made some
- 6:04nice trivia games, some hangman
- 6:05[clears throat] games. There was no
- 6:06visuals. It was really all just
- 6:07textbased. But still I could make some
- 6:09bots and talk with the bots. So that is
- 6:10how I first learned how to write
- 6:12programming.
- 6:14Then in 2008, that is when I first
- 6:16discovered Flash. And before that, I was
- 6:18used to playing games just like every
- 6:19kid my age, playing web- based games.
- 6:21just go in school, go on the computer,
- 6:23start playing some flash games. That was
- 6:24fun. But this is when I realized, okay,
- 6:26I can actually not just play games, but
- 6:28I can actually make those games. So that
- 6:29is when I discovered Flash was the
- 6:31program that people were using to build
- 6:33those games. That is when I started
- 6:35doing that. Started making a bunch of
- 6:36flash games, publishing them on
- 6:38Newgrounds, congregate, all those
- 6:39things. And eventually over time, I also
- 6:42learned that you could put some ads on
- 6:44those games, which meant those games
- 6:45could actually be a source of income. So
- 6:46that is when I started thinking about
- 6:48game development as sort of a potential
- 6:49career. So I made that for about four
- 6:51years. I made an insane amount of flash
- 6:53games. And eventually in 2012, that's
- 6:55when I decided, okay, I like web games.
- 6:57But more and more what I really like
- 6:59playing are really just PC games. So I
- 7:01would like to make those kinds of games.
- 7:02And flash can't really quite do it. So
- 7:05that is when I started looking, okay,
- 7:06how can I make some PC games? And that
- 7:08is when I went looking and I found
- 7:10Unity. I think back then GDAU did not
- 7:12exist. Unreal was paid. So Unity was the
- 7:15only option. It was completely free. So
- 7:17I downloaded it, started using it. And
- 7:19since then, I have published nine games
- 7:21on Steam. Here's just an example of some
- 7:23of my games. Now, my games have never
- 7:25been a massive hit. I don't have any
- 7:26kind of game that most people would
- 7:28recognize, but still, my games have
- 7:30managed to allow me to make a living as
- 7:32a solo indie game dev for over a decade.
- 7:34And all that time, I've made about 1.1
- 7:36million in gross revenue. So, the games
- 7:38have done quite decently well. And then
- 7:41in 2018, that's when I started my
- 7:42YouTube channel. That's when I figured
- 7:43out, okay, I'm already I've already
- 7:45spent all this time making games. I have
- 7:47quite a bit of knowledge and I know a
- 7:49lot of people would learn how to would
- 7:50love to learn how to make games. So why
- 7:52don't I try doing that? Why don't I
- 7:54start making some tutorials trying to
- 7:56teach the knowledge that I have gained
- 7:57using my own games teaching very
- 7:59practical knowledge. So teaching things
- 8:00that I actually learned whilst making my
- 8:02actual Steam games and basically that's
- 8:04what I started doing. Started making
- 8:05some nice YouTube videos. And since then
- 8:07I have made over 800 videos something
- 8:10like that. This is my most popular video
- 8:123.7 million views a complete 10-hour
- 8:14free course. It is really nice. So yep,
- 8:16this has been my journey and throughout
- 8:17all this time basically I learned the
- 8:19importance of clean code and that is why
- 8:21it is something so important which is
- 8:22actually topic on this slide right here
- 8:25why clean code is so important to me
- 8:26because it is something very important
- 8:27to me and the reason is simply basically
- 8:30I want to help you avoid the mistakes
- 8:31that I went through that's entire
- 8:32purpose behind this talk basically I did
- 8:35not always write high quality clean code
- 8:37like everybody else I started off by
- 8:38writing some pretty nasty code I started
- 8:40writing code that just worked that was
- 8:42the only purpose as long as it works and
- 8:44it's fine but Of course that led to
- 8:46quite a lot of problems. So I suffered
- 8:48quite a lot of frustration due to bad
- 8:49names, confusing logic, all of those
- 8:51things. So basically I suffered through
- 8:53it quite a lot. Whereas if I had focused
- 8:55on these guidelines right from the
- 8:56start, my code quality would have been
- 8:57higher and I would have avoided quite a
- 8:59lot of frustration. So basically that's
- 9:01my goal with here. Basically trying to
- 9:03teach you all these concepts so you can
- 9:04avoid all the mistakes and all the
- 9:06frustration that I went through. Now on
- 9:09topic of clean code that is actually a
- 9:11very nebulous topic. It is something
- 9:13that everyone seems to have their own
- 9:15definition. If you go on Google, you'll
- 9:16find like a dozen different definitions.
- 9:18So, I want to give you my specific
- 9:20definition just so you know exactly the
- 9:21context for everything we're talking
- 9:23about in this talk. And my definition is
- 9:25quite simple. It is just this clean code
- 9:27is code that is easy to understand and
- 9:29easy to modify. That's it. Very simple,
- 9:31very basic, very straightforward. That
- 9:33is it. If your code is easy to
- 9:35understand and if you can easily modify
- 9:36it, if so, then I call that clean code.
- 9:39So, that's it. That's basically my
- 9:40definition. Whereas with some dirty code
- 9:42like we saw a while ago, we have to
- 9:44stare and we have to try to guess what
- 9:45is the code doing. Whereas with clean
- 9:47code, you can really just look at it and
- 9:48just by reading it, you should be able
- 9:50to easily understand it. This is my
- 9:52definition quite simple. And as to why
- 9:54this is important, it is one of the
- 9:56reasons is because this is because you
- 9:58actually spend a lot more time reading
- 9:59code than you do writing code. I believe
- 10:01the actual number is something like 10
- 10:03times. So for every one minute you spend
- 10:05writing code, you spend 10 minutes
- 10:06reading that kind of code. So that means
- 10:08that it does pay to actually pay
- 10:10attention to how you're writing your
- 10:11code so that it is easy to read because
- 10:14that is what you're going to spend the
- 10:15most amount of time doing.
- 10:18Ultimately the entire purpose behind all
- 10:19this this is the goal to help you make
- 10:21more complex more interesting better
- 10:22games and importantly actually finish
- 10:24them. So all of you I'm sure just like
- 10:26me just like everybody else you have a
- 10:28mountain of unfinished prototypes and a
- 10:30lot of the reasons why those prototypes
- 10:31end up being unfinished is because
- 10:33you're trying to build it but it gets
- 10:34more and more complex. it becomes harder
- 10:36and harder to add things and eventually
- 10:37the whole thing comes crumbling down. So
- 10:40that is why clean code is important
- 10:42because not only it makes things easier
- 10:44to work with but it helps you actually
- 10:45finish those so you can actually end up
- 10:47with a bunch more finished project as
- 10:49opposed to just a ton of prototypes.
- 10:52Now it is important to know what is
- 10:53clean code and also what is not clean
- 10:55code. So on what is clean code? So clean
- 10:58code is all about minimizing complexity
- 10:59and mental load. So as you're working on
- 11:01some kind of codebase, you don't have to
- 11:03keep in mind a massive amount of code.
- 11:05You can just think about the one thing
- 11:06that you are working on and that's it.
- 11:08And if you write your code properly, if
- 11:10you organize things properly, if so, you
- 11:12can do that. Whilst working on one piece
- 11:13of code, you just focus on that. So it's
- 11:15really all about trying to minimize
- 11:16complexity as much as possible and
- 11:18trying to minimize the mental load that
- 11:20you have to keep in your head. Clinko is
- 11:22also about reducing future debugging
- 11:24pain. So it's all about trying to be
- 11:26kind to your future self because bugs
- 11:28will guarantee to happen that that
- 11:30there's no way around that. Bugs will
- 11:32happen but if you write your code
- 11:33properly the debugging process will be
- 11:35much much easier as opposed to if you
- 11:36write things quick and dirty debugging
- 11:38process will be a nightmare. So yep
- 11:40clean code is all about reducing that
- 11:42future debugging pain.
- 11:44Then clean code is also about making
- 11:46intent obvious. So again just by reading
- 11:48it you should be able to easily
- 11:49understand okay what exactly this is
- 11:50doing. You shouldn't have to think long
- 11:52and hard. Okay trying to understand what
- 11:53is this function doing? What does this
- 11:54class represent? No, just by reading it,
- 11:56you should be able to understand. So the
- 11:58intent should be very, very obvious.
- 12:01Clink [snorts] code is also about making
- 12:02the project easier to expand. So again,
- 12:04if you're making a tiny prototype,
- 12:05doesn't really matter too much. But if
- 12:07you're trying to make a six to oneear
- 12:08game, if so, you're going to be
- 12:09expanding and adding more and more
- 12:11features onto it. And if you write quick
- 12:12and dirty code, then it's going to be
- 12:14very hard to do that. But if you write
- 12:15some proper clean code, then adding more
- 12:17and more features should be a breeze.
- 12:20And clean code is also about helping
- 12:21feature you understand PESU. So, in a
- 12:23little bit I have a slide all about
- 12:25this, how even if you're working just by
- 12:27yourself, it still makes sense to pay
- 12:29attention to the quality of your code
- 12:30because even if you're working by
- 12:31yourself, you're still always at least
- 12:33two people. There's present you and
- 12:35there's future you and those might as
- 12:37well be very different people. So,
- 12:38writing good clean code is basically
- 12:40making it easier on future you so that
- 12:42they can easily understand past you.
- 12:45Now, what is clean code not about? So,
- 12:47clean code is not about performance.
- 12:49Those are two very much separate topics.
- 12:51They can sometimes overlap sometimes but
- 12:53for the most part they are separate
- 12:55things. So you focus on clean code
- 12:57writing things properly and
- 12:58alternatively you can focus on
- 12:59performance making things fast and
- 13:01sometimes they do overlap but they are
- 13:03not related. So the whole thing about
- 13:05saying clean code is fast or slow that's
- 13:07very much basically comparing apples to
- 13:09oranges those are very much separate
- 13:11topics.
- 13:13Clean code is also not about impressing
- 13:15other programmers. So like I said this
- 13:17talk this is all very practical stuff
- 13:18that you should be able to use as soon
- 13:20as you finish watching this video. So it
- 13:22is not just arbitrary things just to
- 13:24make it good for some kind of teacher to
- 13:26impress someone. Nope. It's not it. It's
- 13:28very practical meant to help you as a
- 13:30programmer. Clean code is also not about
- 13:32following some arbitrary rules. So it's
- 13:34not about looking good on some kind of
- 13:36CV something like that. Nope. Like I
- 13:38said it's all very practical very much
- 13:40to help you write some awesome code.
- 13:42Clean code is also not about a specific
- 13:44book. Basically if you go on Google and
- 13:46you search for clean code you'll find
- 13:47there's one book literally with that
- 13:48title and there are some interesting
- 13:50ideas in that book and some not so
- 13:52interesting ones. So over here basically
- 13:53I am not talking about that book
- 13:55specifically. I'm talking about more of
- 13:56the highle concept of what exactly is
- 13:59link code which like I said already gave
- 14:01you my definition code that is easy to
- 14:03understand and easy to modify.
- 14:05And link code is also not unbreakable
- 14:08rules that you must follow. So
- 14:09everything that we're going to cover in
- 14:10this song, these are very much just
- 14:11guidelines and it's up to you and it's
- 14:13up to you to know when to apply these
- 14:15guidelines and when to not apply them.
- 14:16Again, keep in mind always at Northstar.
- 14:18It is always meant to help you. So these
- 14:20are things you always keep in mind, but
- 14:22sometimes breaking these guidelines
- 14:24might actually help. Now, here's the
- 14:26most common objection that I see
- 14:28whenever I try to encourage people to
- 14:29write higher quality code. They
- 14:30basically tell me, "So if the code
- 14:32works, why does any of this matter?" And
- 14:35it's an interesting argument, but the
- 14:37thing is this argument only works
- 14:38basically if you're making some very
- 14:40very tiny games. If you're making
- 14:41something like a Flappy Bird where an
- 14:43experienced developer kind of like
- 14:44myself can build in let's say like two
- 14:46hours, that's a very simple game. So
- 14:48with something that simple, even if you
- 14:49write some very terrible code, you are
- 14:51not really going to live in that
- 14:52codebase for any length of time. So it
- 14:55doesn't really matter the quality of the
- 14:56code. But as soon as you're trying to
- 14:58make something more complex, if you
- 14:59don't have a nice solid foundation, if
- 15:01so, then the whole thing is really just
- 15:02going to come crashing down. You have to
- 15:05write some proper clean code. You need
- 15:07some proper practices so you can
- 15:08actually build something more
- 15:09interesting on top of it. And that is
- 15:11basically what clean code does. If you
- 15:12focus on clean code, then your
- 15:14foundation will be very solid and you
- 15:16can easily add things on top and things
- 15:17will still stay solid. So essentially,
- 15:20sure your code works right now, but
- 15:22won't still work after you add 20 new
- 15:23features. If you write some very dirty
- 15:25code, the more things you write, the
- 15:27more the whole thing is going to come
- 15:28crashing down. But if you have a proper
- 15:30solid foundation, you will be able to
- 15:31keep adding things on top of it and it
- 15:33will still work. Messy code is just
- 15:35going to increase your debugging pain.
- 15:37Whereas clean code, again, that won't
- 15:38actually help you take your game to the
- 15:40finish line. Now, here's another common
- 15:43objection that I see when I talk about
- 15:44this. Someone saying, "I work alone. No
- 15:46one else needs to understand my code but
- 15:48me and I know what I wrote." So,
- 15:50sometimes people use this to not worry
- 15:51about naming rules, those sort of
- 15:52things. But like I was saying a while
- 15:54ago, you are never really working by
- 15:56yourself. Even if you are totally by
- 15:57yourself, even if so, you are always at
- 15:59least two people. That is you from the
- 16:02present who has all the context from all
- 16:03the code that you are writing. But then
- 16:05there's you in the future and that one
- 16:07does not have all that context. So you
- 16:09of the present and you of the future
- 16:10might as well be two completely
- 16:12different people. In the future, you
- 16:14will not have all the content you have
- 16:15right now. So things that make sense
- 16:17right now, they will not make sense in
- 16:18the future. Like let's say for example
- 16:20you're making some kind of code and you
- 16:22define some kind of function and you
- 16:23decide to name it DST just because for
- 16:25some reason for you right now that name
- 16:27DST seems to make sense. So you write it
- 16:29like that but then you come back to it
- 16:31one month later and you no longer have
- 16:32all that context and now you are left
- 16:34wondering what exactly did I mean by
- 16:36DSC? Was it distance destination some
- 16:39kind of distribution something there is
- 16:40no way of knowing because you named it
- 16:42DSC you named it something that made
- 16:44sense at the time but now in the future
- 16:46without all that context now it does not
- 16:48make any sense. So even if you are
- 16:49working technically by yourself it is
- 16:51never really just one person it is
- 16:52always multiple developers working
- 16:54across time. So essentially focus on
- 16:57writing clean code and do not make
- 16:58things harder on your future self for no
- 17:01reason. Again remember you spend a lot
- 17:03more time reading code than you do
- 17:04writing the code. So that should be your
- 17:06focus writing code that is going to be
- 17:08easier to read when eventually you do
- 17:10have to read them. Now on this topic
- 17:12there's a funny joke related to this
- 17:14which is a nice way to sort of have a a
- 17:16lens from which to see all the things
- 17:18we're talking about here which is
- 17:20basically write code as if the person
- 17:21who will maintain it after you is a
- 17:23serial killer who knows where you live.
- 17:24So try to think it like that. Try to
- 17:26write code right now so that in the
- 17:28future that person who might be a serial
- 17:30killer, you don't want to make them
- 17:31angry. So write some very good code so
- 17:33that that person is happy with your code
- 17:35as opposed to being very unhappy and
- 17:37trying to do something very bad to you.
- 17:40Okay? So this is what we're going to
- 17:42learn how to go from the left to the
- 17:43right side. But like I said, remember
- 17:45how there's nuance to everything. These
- 17:47aren't just guidelines. These are not
- 17:48unbreakable rules. So as I start giving
- 17:51you some very practical advice, always
- 17:52keep this in mind. I encourage you to
- 17:54follow all these guidelines. Especially
- 17:55if you are a beginner, you should opt
- 17:57for following them more than for
- 17:59breaking them. But sometimes not
- 18:00following these guidelines might
- 18:01actually lead to better code. So that
- 18:03should always be your guiding star.
- 18:04Always thinking, does this make the code
- 18:06easier to understand and easier to
- 18:07modify? If so, go ahead and apply the
- 18:09guideline. But as always, these are not
- 18:11unbreakable rules. Sometimes it is
- 18:13better to break them. Okay, starting off
- 18:15with like I said the most important
- 18:16thing. If you take just one thing away
- 18:18from this talk, I hope you take this. If
- 18:20you just focus on this, your code
- 18:21quality will massively improve. naming
- 18:23rules. These by themselves they are the
- 18:25easiest clean code improvement you can
- 18:26make. It is very easy and will massively
- 18:28increase the quality of your code. And
- 18:30the reason is because naming naming is
- 18:32actually architecture. It is not just a
- 18:34cosmetic thing. When you give a variable
- 18:36or a function some kind of name, it is
- 18:38not just a visual thing. No, it is
- 18:39something very core to your entire
- 18:41codebase. Very core to how the code
- 18:43actually works. Naming is very much a
- 18:45crucial part of your entire codebase. So
- 18:47you need to focus on writing good names
- 18:49so you have a nice solid foundation upon
- 18:51to which [snorts] good names they
- 18:53essentially make it clear what the
- 18:54variable or class represents or what the
- 18:56function does. That's it very very
- 18:58simple but if you just follow this the
- 19:00code quality will go up by quite a bit.
- 19:02So for example here's a bad example. So
- 19:05let's say we got some kind of variable
- 19:06float s. Now what exactly does that
- 19:08represent? Just by looking at it you
- 19:10have no idea. Is it some kind of speed,
- 19:12movement speed, rotation speed? Is it
- 19:14some kind of shield amount? Something
- 19:16like that. I don't know. What about D?
- 19:18Is it some kind of distance? Some kind
- 19:20of destination? No way of knowing. What
- 19:22about a function named do stuff? What is
- 19:23that doing? Literally, you cannot tell
- 19:25because that is the most generic name
- 19:27ever. Do stuff. Stuff can be literally
- 19:29anything. So, just by looking at the
- 19:30name of the function, you cannot tell
- 19:32what it is doing. So, if you use these
- 19:34kinds of names, you have to waste a ton
- 19:36of mental energy trying to understand
- 19:38what does this represent? What is this
- 19:39thing doing? Whereas if you use some
- 19:42proper names then you can just see oh
- 19:43okay so that's movement speed that's the
- 19:45reach start distance that function is
- 19:47updating the health bar. So if you write
- 19:49some proper names then just by
- 19:50themselves the code will instantly be
- 19:52easier to understand. You can just read
- 19:54and you instantly know what does this
- 19:56represent? What is this doing? There's
- 19:58basically no guessing required like this
- 20:00things are much much easier to follow.
- 20:03So on this here's some very specific
- 20:05advice. So first of all be descriptive
- 20:07be verbose. So I very much encourage you
- 20:10if a function or if a field is meant to
- 20:12represent the health amount then name it
- 20:14health amount. That's it. Use whatever
- 20:17is the most descriptive name you can
- 20:18come up with that represents that thing
- 20:20or what that function does. Try to come
- 20:22up with the best name possible. And like
- 20:24I said be verbose. So if the absolute
- 20:26best name that you can come up with is
- 20:28let's say 20 characters long. If so then
- 20:30just use that name. Don't try to shorten
- 20:32it. Don't try to like eliminate vowels
- 20:34or come up with abbreviations. No, don't
- 20:36do that. There's basically no limitation
- 20:38to the memory size for your variable
- 20:40names. So just use whatever variable
- 20:42names make most sense. Doesn't matter if
- 20:44it's 20 characters. If that's the best
- 20:46name you can come up with, then go ahead
- 20:47and use that name. Just make sure it is
- 20:49very descriptive. Just by reading the
- 20:51name, you should be able to understand
- 20:52what does this represent or what is this
- 20:54doing. Also avoid abbreviations and
- 20:57acronyms. Again, goes back to the same
- 20:59thing how present you and future you are
- 21:01different people. So some kind of
- 21:03abbreviation, some kind of acronym might
- 21:05make sense to you right now, but one
- 21:06month from now, chances are it won't
- 21:08make sense and you will have no idea
- 21:09what does that acronym mean. So try to
- 21:11avoid those because it is very, very
- 21:13easy to forget what do they mean. Also,
- 21:16don't try to be clever. I know some
- 21:18people, especially some beginners, they
- 21:19find it fun how you can name it
- 21:21literally anything. So they start naming
- 21:23it like inside jokes or sometimes using
- 21:25some curse words. And sure, again,
- 21:27that's something that [snorts] might be
- 21:28fun to you right now. It's quite funny,
- 21:31but one month from now will not be as
- 21:33funny because you won't understand.
- 21:34Okay, I named this some kind of joke.
- 21:36But what does this mean? I don't know
- 21:38what it means. So don't be clever.
- 21:40[clears throat]
- 21:41Also, do not use single letter variable
- 21:43names. So basically names like we saw
- 21:45like ds. What exactly does that mean?
- 21:47There's no way of knowing. There's not
- 21:48enough information in the variable to
- 21:50represent what exactly it's
- 21:52representing. So don't use those. The
- 21:54obvious exception of course are for loop
- 21:56iterators. So if you're making some kind
- 21:57of for loop sure using I, J, K, L, all
- 22:00of those usually those are easy to
- 22:02understand. So for that exception, you
- 22:04can use those. But everywhere else, do
- 22:06not use single letter variables.
- 22:10Then also remember how you don't get
- 22:11bonus points for comeback code. Like I
- 22:13was saying, there's really no
- 22:14limitations to memory nowadays.
- 22:15Computers are insanely good. Maybe if
- 22:17you were back in the 80s, you had to
- 22:19actually limit what name you gave a
- 22:20variable because that actually mattered.
- 22:22But nowadays, memory as far as it's
- 22:24concerned with regards to naming rules,
- 22:26regards to size of names, it's basically
- 22:28infinite. So, like I was saying, if the
- 22:30best name you can come up with is 20
- 22:32characters long, then do it. Don't try
- 22:34to shorten it just for the sake of
- 22:35compact code because you really don't
- 22:37get bonus points. But you do get bonus
- 22:39points for code that another developer,
- 22:41including yourself, can read instantly.
- 22:43So that should very much be your focus
- 22:45trying to come up with names that
- 22:46someone without any context of the
- 22:48codebase can just look at it and see oh
- 22:50okay I know exactly what this is doing.
- 22:53So these are the practical rules but
- 22:55also very important to remember how
- 22:57names are very much not set in stone.
- 22:59You can easily rename things. So you
- 23:01should be constantly trying to think
- 23:02okay what is the best name that I can
- 23:04come up with in this scenario. But if in
- 23:06the future you come up with a better
- 23:07name. If so just take a few seconds just
- 23:09rename it. Nowadays ids are very good.
- 23:12Here's an example in Visual Studio.
- 23:13Visual Studio you just press CtrlR and
- 23:16you can easily rename things and it will
- 23:17automatically rename every single
- 23:19instance of that symbol. So there's
- 23:21really no reason not to rename things.
- 23:23So when you define some kind of
- 23:24variable, some kind of function, think
- 23:25of what is the best name possible. But
- 23:27then in the future, always keep trying
- 23:29to think, okay, is there a better name
- 23:30for this thing? If so, spend two seconds
- 23:33rename it and give it the better name.
- 23:36Then in terms of how you actually write
- 23:38your code, the code style, that one is
- 23:40also something that is quite important.
- 23:42But the most important thing is really
- 23:43just this consistency. You can basically
- 23:45pick whatever code style you want. Go
- 23:46with uppercase, lowerase, Pascal, camel
- 23:49case, none of that matters. Those are
- 23:50really just visual things. So the
- 23:52specific code sound that you follow.
- 23:54That one does not matter. What matters
- 23:55is this. You need to be consistent. So
- 23:57define some kind of code sound that you
- 23:59want to follow and then really just
- 24:00follow it 100% of the time. You don't
- 24:02want to have one file following this
- 24:04specific type of code cell and different
- 24:06file following a completely different
- 24:07code style. No, you want things to
- 24:09match. You want to be able to open any
- 24:11kind of file in your codebase and be
- 24:13able to easily understand because they
- 24:15all follow the same code style. As an
- 24:16example, here is my own personal code.
- 24:19What I've developed over my 20 years of
- 24:21writing code. So for my classes, I
- 24:23define them in Pascal case. For
- 24:25constants, uppercase snake case. For my
- 24:28properties and events, PESL case. Then
- 24:30for fields, I've got cheml case. for
- 24:32parameters I've got chem as well. So,
- 24:34yep, this is the code sound that I
- 24:35follow and I can basically open up any
- 24:37script from the past 10 years that I've
- 24:38written and it will be following this
- 24:40code sound. So, I can very easily
- 24:41understand, I can very easily read it.
- 24:43But like I said, there's nothing special
- 24:45about this specific code sound. This is
- 24:47just the one that I came up with myself.
- 24:49But that doesn't mean that you should
- 24:50use this sound. Like I said, most
- 24:52important thing is consistency. So, come
- 24:54up with whatever code sound makes most
- 24:55sense to you and really just make sure
- 24:57you follow it 100% of the time.
- 25:00Next on the topic of decoupling and how
- 25:02it is a superpower. So decoupling is
- 25:05really awesome and decoupling basically
- 25:08just means keeping classes as separated
- 25:10as possible and doing so it reduces
- 25:11mental load. So if you have like two
- 25:13classes you don't want them to be
- 25:15constantly talking to one another. If so
- 25:16they are going to be tightly coupled.
- 25:18Instead you want to separate them as
- 25:19much as possible. If two classes can
- 25:21work independently, you should separate
- 25:23them as much as possible. Because if you
- 25:25make everything coupled, if everything
- 25:27talks to another, so you've got some
- 25:28kind of giant class that talks to the AI
- 25:30system, to the physics engine, the
- 25:31rendering pipeline, the animation,
- 25:32whatever it is. If so, then as you're
- 25:34working on that one class, you're
- 25:36constantly having to keep in your head,
- 25:37okay, how is this change that I'm doing
- 25:39to this class, how is this affecting all
- 25:41of these other different classes? You
- 25:43have to keep all of that massive amount
- 25:45of information in your brain as you're
- 25:46working on just one specific class. So,
- 25:48this is quite terrible. this going to
- 25:50cause you quite a lot of mental load
- 25:52whereas your goal should be to reduce
- 25:54mental load. So in turn you should keep
- 25:56things as decoupled as possible. If you
- 25:58want if you can separate the animation
- 26:00system from let's say the physicism
- 26:01entirely. That way if you're working on
- 26:03the animation that's it. You just work
- 26:04on that one. You don't have to worry
- 26:06about the physicism because you know for
- 26:08a fact they are completely decoupled
- 26:10from one another. Or if you're working
- 26:12on some kind of player movement let's
- 26:14say your entire code base is like a
- 26:15thousand classes long. But if the player
- 26:17movement is 100% decoupled from
- 26:18everything else, then it doesn't really
- 26:20matter. Your codebase could be 100
- 26:21classes long, a thousand classes long,
- 26:23it doesn't matter because if that one is
- 26:24decoupled whilst you're working on that
- 26:26one, you know for a fact it is not
- 26:27touching a dozen other classes whilst
- 26:29you're working on that. You just focus
- 26:31on that and that's it. So the mental
- 26:32load you have to keep inside your head
- 26:34is very very small which helps you out
- 26:36quite a bit. And if you want a very
- 26:39practical example on how to decouple
- 26:40things, just this decoupling action is
- 26:43just to separate the logic from the
- 26:44visuals. So let's say you've got some
- 26:46kind of player movement script and that
- 26:47one should just handle movement. So that
- 26:49one should take some kind of transform
- 26:50and just move it around and then you
- 26:51have a separate some kind of player
- 26:53visual script and that basically just
- 26:54listens to the logic on movement and
- 26:56then calls some kind of animation. So
- 26:58the logic script that one does not know
- 27:00anything about any kind of visual, any
- 27:01kind of animation, any kind of mesh,
- 27:03nope, does not know any of that. It just
- 27:05knows it has a position and just moves
- 27:07that kind of position. And then you've
- 27:08got the visual script. that one just
- 27:10listens to what is changing on the logic
- 27:11script and that is the one that is
- 27:12actually playing sound effects, playing
- 27:14animations, those kinds of things. So
- 27:16try to keep those separated the logic
- 27:18and visuals try to keep those as
- 27:20separated as possible and that alone
- 27:21will help the increase the code quality
- 27:23by quite a bit. Now in coupling just one
- 27:26last thing which is you always have to
- 27:28have some coupling. So for example in
- 27:30this scenario the logic is completely
- 27:31decoupled from the visuals but the
- 27:33visuals need to know what the logic is
- 27:34doing. So the visual is tightly coupled
- 27:36to the logic. So some coupling is always
- 27:38guaranteed to happen. You can't really
- 27:40make a game or any kind of software. If
- 27:42you have classes that do not talk to
- 27:43each other at all, no, they do have to
- 27:45talk to each other somewhat. So the
- 27:47entire purpose is trying to minimize as
- 27:48much as possible, trying to separate
- 27:50things as much as you can and doing so
- 27:52again reduce your mental load which
- 27:54makes your job as a programmer much much
- 27:56easier.
- 27:58Then on topic of the single
- 27:59responsibility principle, this one is
- 28:01really nice and really easy to
- 28:03understand. It is basically one class,
- 28:05one function should do one thing and one
- 28:06thing only. That is it. Super basic,
- 28:09super simple. But if you just follow
- 28:11this, your code quality will massively
- 28:12increase. And this one, some of you
- 28:15might know it is actually part of the
- 28:16solid acronym. So it's got all these
- 28:18various things. So if you want a little
- 28:20bit more homework, you can actually go
- 28:21and research all these other principles.
- 28:23They're also nice, but I think the most
- 28:25important one by far is the first one.
- 28:27So that is the one that we're going to
- 28:28be focusing on. And on that one, like I
- 28:31said, one class should do one thing and
- 28:32one thing only. So, here's a bad
- 28:34example. We've got some kind of player
- 28:36movement class. And sure, it is moving
- 28:37the player, but then it's also playing
- 28:39footstep sounds. It's also starting some
- 28:41kind of running animation and updating
- 28:43UI. That is not correct. It says that
- 28:45it's player movement, but it's moving
- 28:47the player and playing footstep sounds,
- 28:48animation, so on. It's doing multiple
- 28:50things at once. That is not correct. So,
- 28:52this one is violating the single
- 28:54responsibility principle. Instead, this
- 28:56should be nicely sped up. You should
- 28:57have different classes. So this one
- 28:59class just handles player movement. This
- 29:01one class just player sounds. This one
- 29:02class just player visuals. You should
- 29:04keep things separated. So each thing
- 29:06does one thing and one thing only. The
- 29:09same thing applies to functions as well.
- 29:11So here's an example. You've got some
- 29:12kind of function named move player and
- 29:14it is moving the player. But then it's
- 29:15also playing some kind of footstep
- 29:17sounds. That is not correct. You should
- 29:18not be doing this. You should not be
- 29:20doing both things. Both moving the
- 29:21player and playing footstep sounds.
- 29:23Nope. You should focus on one thing and
- 29:25one thing only. And actually in this
- 29:26specific case for functions you can
- 29:28really just remember the naming rules
- 29:30always remember. So try to come up with
- 29:33the best the most descriptive name you
- 29:35can come up with the name that actually
- 29:36represents what the function is doing
- 29:38which in this case this name move player
- 29:40that is not the correct name. It says
- 29:42move player but the function is clearly
- 29:43moving the player but then also playing
- 29:45footstep sounds. So the name is not
- 29:47correct. The better name for this
- 29:48function should be move player and play
- 29:50footstep sounds because that is what the
- 29:52function is doing which in itself that
- 29:54name that is actually a very very useful
- 29:57heristic. Basically if the best name you
- 29:59can come up with includes the word and
- 30:01if so you're probably doing too much.
- 30:03Sure if you if the best name is move
- 30:05player and play footstep sounds that is
- 30:07a very nice guide. Okay, I should
- 30:09probably split this up. There should be
- 30:10one function to move the player and one
- 30:12function to play sound effects. Those
- 30:14should be their own thing. So, yep, if
- 30:16the best name includes the word and,
- 30:18there is a very clear guide, not always,
- 30:20but most times that you probably should
- 30:22split this up. But again, like I said,
- 30:24just a quick reminder, remember
- 30:26everything we're talking about here,
- 30:27these are all guidelines. These are not
- 30:28unbreakable rules. So, sometimes it
- 30:31doesn't make sense. For example, for
- 30:32this one, for the single responsibility,
- 30:34sometimes you have a function that is
- 30:35like 20 lines long, and technically it's
- 30:37doing more than one thing. But if you
- 30:38break that one function into five
- 30:40functions that each are like two lines
- 30:42long, if so, it might actually increase
- 30:44complexity as opposed to minimizing it.
- 30:46So that is why you should focus on that
- 30:47guideline always every time. The go the
- 30:50entire goal behind all of this is trying
- 30:51to minimize complexity. And these
- 30:53guidelines, they should help you achieve
- 30:55that. But if there's some scenario where
- 30:56they don't where they actually make the
- 30:58code more complex, if so, then don't
- 30:59follow them in that specific scenario.
- 31:02Next on the topic of information hiding.
- 31:05This is another interesting one which is
- 31:07remember how clean code is all about
- 31:08minimizing complexity and mental load
- 31:10management. So you should try to
- 31:12minimize how much you have to keep in
- 31:13your head. So in this case let's look at
- 31:15a bad example. Here we've got a bunch of
- 31:18fields and all of them are marked as
- 31:19public. Now public means that any class
- 31:21anywhere in your entire codebase can
- 31:23both read and write to it. This is
- 31:25pretty terrible because if something bad
- 31:27happens, you don't know what is going
- 31:29on, where exactly that bug came from. So
- 31:32if you have a codebase that is a
- 31:33thousand class long, then in theory that
- 31:35is a thousand class a thousand places
- 31:38where anything could be modifying any of
- 31:39those fields. So if there's some kind of
- 31:41bug where the movement speed is set to
- 31:43zero, how exactly do you know where that
- 31:45came from? How do you debug it? It is
- 31:47going to be very very difficult because
- 31:48this because technically if it is
- 31:50public, there's a thousand places that
- 31:52could be modifying that. So this means
- 31:54that as you're working on this codebase,
- 31:56you're constantly having to think about
- 31:57the entire codebase because technically
- 31:59anything could be modifying that one
- 32:01field.
- 32:02So instead, you should default to make
- 32:04things private. Private means that only
- 32:06that specific class can access that
- 32:08specific field. So if you've got some
- 32:09kind of bug, you know for a fact it has
- 32:11to exist in that one class. It cannot
- 32:12exist outside of it because it is
- 32:14physically impossible for something
- 32:15outside to modify it. You should try to
- 32:17limit access as much as possible and
- 32:19basically only expose what you
- 32:21absolutely need to. So in this case, if
- 32:23it is private, it doesn't really matter
- 32:25how many class you have in your entire
- 32:26codebase. Could be 10, could be a
- 32:28thousand, could be 10,000. Doesn't
- 32:29really matter because if it is private,
- 32:31you know for a fact, guaranteed if you
- 32:33got some kind of bug on your movement
- 32:34speed, it has to exist right here in
- 32:36this one class. Now, just a quick Unity
- 32:39tip, which is if you mark it as private,
- 32:41it does not show up in the editor. So if
- 32:42you do want to follow some good clean
- 32:44code guidelines and you also want to
- 32:46expose something in the editor, if so,
- 32:48then just do that. Make it private and
- 32:49add the serialized film attribute. That
- 32:51way it will be private meaning other
- 32:53classes cannot modify that field but
- 32:56YouTube itself unity itself will be able
- 32:58to showcase that field in the inspector
- 33:00so you can easily edit it. Now like I
- 33:03said limitex as much as possible and
- 33:04only expose what you absolutely need to.
- 33:06So if you do need to expose this let's
- 33:08say you've got some kind of UI function
- 33:09that needs to showcase the movement
- 33:11speed that means UI does need to know
- 33:13about it. You can't hide it from the UI
- 33:15and if so really just make some simple
- 33:17get functions. Now technically if you
- 33:19want you can also use properties.
- 33:20They're basically the same thing. Just
- 33:21me personally, I prefer to use
- 33:22functions. So just like this, if I've
- 33:24got some kind of UI that needs to know
- 33:26movement speed, I really just expose a
- 33:27function get move speed and the UI
- 33:29actually reads the move speed. Then if I
- 33:31need something to actually set movement
- 33:34speed, then I expose the function to do
- 33:35that. So yep, just like this. This helps
- 33:37out quite a bit. Now, of course, you
- 33:39might be asking, okay, so we made them
- 33:41private, so we don't have access. But
- 33:43now, if we add these functions, don't we
- 33:44end up with the same thing? And yes and
- 33:46no. So first of all, like I said, only
- 33:48expose things if you absolutely need
- 33:50them to. So if you don't need anything
- 33:52to get some kind of variable, then don't
- 33:54expose it. Not at all. You should only
- 33:56make these two functions. These are
- 33:57optional. Only make them if you
- 33:59absolutely need them. If something else
- 34:00needs to get that function, needs to
- 34:02actually read that value. If so, make
- 34:04the get function. If something else
- 34:05really needs to set it, if so, make a
- 34:07set function. But only make these if you
- 34:09need them. And even if you do need them,
- 34:11if you do end up making these these,
- 34:13this is still a better approach just
- 34:14because with this it is possible to add
- 34:16access controls, validation, logging,
- 34:18events, those kinds of things. So you
- 34:20can add some kind of function, some kind
- 34:22of log over here when you set the
- 34:23movement speed. So if you have some kind
- 34:25of bug where the movement speed is set
- 34:26to zero, at least you can follow the
- 34:28log, you can follow the stack trace and
- 34:29try to see where is that bug came from.
- 34:32Whereas if you make a public field, you
- 34:33really have no way of attaching extra
- 34:35logic to that. If something modifies it,
- 34:37that's it. So over here you can ask
- 34:39validation X controls and a bunch more.
- 34:41So yeah, even if you make these two
- 34:43functions, these are still better than
- 34:44making it public. But like I said, these
- 34:46are optional. You should only expose
- 34:48these if you absolutely need to. Again,
- 34:51remember the entire purpose behind
- 34:52everything we're talking about here is
- 34:54just this. The entire goal is to
- 34:55minimize how much code you have to keep
- 34:57in your head as you're working. So if
- 34:58you try to minimize something, if you
- 35:00make something private, then you know
- 35:01for a fact if there's a bug in there, it
- 35:03has to exist in this one class. It
- 35:05cannot exist outside of it. So as you're
- 35:07working on this one thing as you're
- 35:08solving this kind of bug, you know for a
- 35:10fact you don't have to think about every
- 35:11other class that exists because only
- 35:13this one can actually modify.
- 35:15Then another very important topic on the
- 35:17one source of truth. This is one of the
- 35:19best ways to eliminate bugs. So one
- 35:22source of truth is basically how your
- 35:23data should have one single source of
- 35:25truth as opposed to existing in multiple
- 35:27places all over the place. So here's a
- 35:30bad example. So let's say we got some
- 35:32kind of player health function that has
- 35:33a current health field. But then the
- 35:35health bar, that one also stores a
- 35:36current health. The safe system also has
- 35:38it. The animator has some kind of is
- 35:40that parameter and the UI is saying
- 35:41dead. So you've got all of this data
- 35:43copied on multiple different places. And
- 35:45if you have this, so who exactly are you
- 35:47going to ask to know if the player's
- 35:49dead? All of them have that kind of
- 35:51data. So which one do you ask? There's
- 35:52no way of knowing. And importantly, what
- 35:54if you have this? What if data goes out
- 35:56of sync? What if the player health says
- 35:58the current health is zero, but the end
- 36:00meta says is dead is set to false. If
- 36:02this is the case, is the player actually
- 36:04dead or alive? And the answer is there's
- 36:06no answer. You can't really know because
- 36:07you have multiple data on multiple
- 36:10places that are all meant to represent
- 36:11the same thing. But if they go out of
- 36:13sync, you don't know because you don't
- 36:14have one source of truth. So this whole
- 36:16thing is a debugging nightmare. If you
- 36:18have some kind of bug, you never know
- 36:19where the bug is coming from because
- 36:21you've got multiple data all over the
- 36:22place. So instead, you should do
- 36:25something like this. You've got the
- 36:26player health. That is the one that
- 36:27actually stores the current health. That
- 36:29is the one source of truth. That is the
- 36:30only one that matters. And everywhere
- 36:32else like the health smart safe system,
- 36:34those just access the player health and
- 36:35they actually read the current health.
- 36:37So they don't store any data themselves.
- 36:39They really just read the actual source
- 36:41of truth, the actual data that actually
- 36:43makes sense. That way if you got some
- 36:44kind of bug on the current house, if so,
- 36:46you know for a fact it has to exist in
- 36:48this one place because everywhere else,
- 36:50all these other ones are just reading
- 36:51from that one. So if there's a bug, it
- 36:53has to be on that one. So, yep. This
- 36:55right here makes it super easy to debug.
- 36:58Then on a potentially slightly more
- 37:00controversial topic here, we have
- 37:02self-documented code. So, basically all
- 37:04about comments. Should you use them?
- 37:05Should you not use them? Essentially,
- 37:07comments, they should be rare and it
- 37:09should explain the why and not the what.
- 37:11For the most part, the goal should be
- 37:13the intent should be clear in the code
- 37:15itself. So, just by reading the code,
- 37:17you should be able to understand what
- 37:18this code is doing, what this variable
- 37:20represents. Just by name itself, it
- 37:22should be understandable. You don't want
- 37:24to use comments trying to explain
- 37:25something. Here's a bad example. So
- 37:27don't write some useless comments. So
- 37:30right here you've got some kind of code
- 37:31adding score by 10 and then you add in
- 37:33the comment at score by 10. This is
- 37:35completely useless. You can understand
- 37:37that just by reading the code itself. So
- 37:39this comment right here is just visual
- 37:41noise. It is just occupying your brain
- 37:42for no benefit whatsoever. So do not
- 37:45write these kinds of comments.
- 37:47Also don't write outdated incorrect
- 37:50comments. So in this case, let's say we
- 37:51started off adding 10 to the score, but
- 37:53then we refactored it. We made it just
- 37:55five, but we forgot to update the
- 37:57comment. So now, not only is the comment
- 37:59adding visual noise, it is actually just
- 38:00an outright lie. And if you believe the
- 38:02comment, it is going to take you in the
- 38:04wrong direction. So in this case, it's a
- 38:06very bad use case for comments. Remember
- 38:09the actual code is the one source of
- 38:10truth that is one that actually matters.
- 38:13Also, do not use comments to explain
- 38:15some bad names. For example, you name
- 38:17some kind of variable D and you are
- 38:19trying to explain, okay, D is a distance
- 38:21something. Again, this is a terrible use
- 38:23case. So in this really remember the
- 38:25naming rules. So in [snorts] this case,
- 38:27instead of a comment explaining it,
- 38:28really just give it a better name. Pick
- 38:30up distance. That is it. You just read
- 38:32the name and it is easily
- 38:33understandable. No need to write a
- 38:35useless comment.
- 38:37Also, not use comments to explain some
- 38:39complex confusing logic. So over here
- 38:41we've got some complex conditions.
- 38:43checking for distance, checking if a
- 38:45keys press and some kind of boolean and
- 38:47we using a comment to try to explain
- 38:48that. Don't do that over here. Instead
- 38:50of using a comment, really just put all
- 38:52that logic in a separate function and
- 38:54then give that function a very good name
- 38:56and that way it is easy to understand.
- 38:58You can just see, oh, I'm asking can I
- 39:00pick up this item and if so, I do pick
- 39:02up the item. That way, no comments are
- 39:04necessary. You really just read it and
- 39:06that's it.
- 39:08Then in terms of dos, do use comments
- 39:10that explain design decisions. So, for
- 39:12example, let's say we're working on some
- 39:13kind of game and most levels in the game
- 39:15you do use path finding. But then, let's
- 39:17say you've got some specific game, some
- 39:19specific level where it doesn't actually
- 39:21have any obstacles and you intentionally
- 39:23don't want to use path finding in that
- 39:24level. If so, then that's actually a
- 39:26great use case for a comment trying to
- 39:27explain why you made that decision
- 39:29because again, try to think so one month
- 39:31from now, you come back to this codebase
- 39:32and you'll look at this code and you
- 39:34might think, oh, this is wrong. I should
- 39:36be using pathfinding. Why am I not using
- 39:38pathfinding? So to avoid that confusion,
- 39:40it does make sense to write a comment
- 39:42that actually explains why you made that
- 39:44one specific design decisions.
- 39:46Also, do you use comments to explain
- 39:48some potential bugs? Let's say you're
- 39:50working on some kind of camera system
- 39:51and you want to make sure that the
- 39:53camera actually updates after the
- 39:54player, otherwise it causes some kind of
- 39:56bug. So that is another use case where
- 39:58it does make sense to write the comment
- 40:00because if you don't have that comment
- 40:01again, future is going to come back to
- 40:03this and is going to see the late update
- 40:04and might think, "Oh, why did I put this
- 40:06on late update? that seems to be a
- 40:08mistake. Let me replace this. Put this
- 40:10on update and doing so you would cause
- 40:11that bug. So to avoid that, go ahead
- 40:14explain a comment trying to explain why
- 40:16exactly use this specific thing and how
- 40:18it was a very conscious choice and not
- 40:20by accident. Also do use comments for
- 40:23class overview or documentation. So any
- 40:25kind of thing where you want to explain
- 40:26it a little bit more. Again like I said
- 40:28the name should be as readable as
- 40:30understandable as possible. But
- 40:32sometimes class can represent some more
- 40:34let's say ethereal concepts and if so
- 40:36using a comment can help with that. And
- 40:38of course for documentation that also
- 40:40makes sense. [snorts] For the most part
- 40:43bad comments they explain code that
- 40:44really should just have been clearer.
- 40:46Whereas good comments they explain
- 40:47context that the code cannot express by
- 40:49itself. For the most part the code
- 40:51should be the thing that you should
- 40:52focus on and comments should just only
- 40:54be used for things that the code cannot
- 40:56express. So the rule is not never write
- 40:58comments. No, instead you should focus
- 41:00on making the code self-documenting and
- 41:02only use comments so add context that
- 41:03the code cannot express clearly. But
- 41:05that should be your main focus. Trying
- 41:07to make sure that just code itself
- 41:08explains everything. And in most cases,
- 41:10in 90% cases, you can do that. Just
- 41:12reading the code should be
- 41:13understandable. Comment should only add
- 41:15context that the code cannot express by
- 41:17itself.
- 41:19Then on the topic of magic numbers,
- 41:21which is a very fun name for something
- 41:23very simple, which is simply numbers
- 41:25where their meaning is unclear. So over
- 41:27here we've got some kind of code moving
- 41:29some kind of transform. It's moving by a
- 41:31move direction and then multiplied by
- 41:33five and 1.2. What exactly do these
- 41:35numbers represent? And the answer is
- 41:37there is no way of knowing. Just looking
- 41:38at this, you can't quite tell what is a
- 41:40five, what is a 1.2? Can I change them?
- 41:42Can I remove them? I have no idea. So
- 41:45these are numbers where their meaning is
- 41:46unclear. So you should not be using
- 41:48these. Instead, use named variables.
- 41:51That way you can easily name them. Okay,
- 41:53so that's movement speed. That's a
- 41:54sprint multiplier. That way things are
- 41:56much easier to understand. I can just
- 41:58read the code and I understand what it's
- 41:59doing as opposed to up there where I
- 42:01have no idea what those numbers
- 42:03represent.
- 42:05These are also numbers where basically
- 42:07you can see kind of like this on some
- 42:09kind of if you're testing if it's under
- 42:103F. What is 3F? Again, no way of
- 42:13knowing. So don't use magic numbers.
- 42:15Instead, give it a proper variable and
- 42:17it would make sense. Oh, okay. So if I'm
- 42:18closer than the attack distance, I'm
- 42:20going to attack the player. Or another
- 42:22example for function parameters. So
- 42:24you've got some kind of function and has
- 42:26multiple parameters technically you can
- 42:28use intellisense in order to see okay
- 42:29what is the second parameter what is the
- 42:31third one what are those meant to
- 42:32represent so in this case technically if
- 42:34you want you can rely on intellisense
- 42:36but again trying to minimize your mental
- 42:38load really just use some local
- 42:40variables that we can define okay that's
- 42:42a minimum zoom maximum zoom so just by
- 42:44reading it is much easier remember the
- 42:47entire purpose the entire goal behind
- 42:48all this is really making it easier to
- 42:51understand so you don't have to waste
- 42:52mental energy so any kind thing that is
- 42:54hidden meaning. Make sure you use some
- 42:56kind of variable where you can give it a
- 42:58proper name and just by reading it you
- 42:59understand what it is doing. Next, do
- 43:02not use strings as identifiers. So the
- 43:05reason is because it is very easy to
- 43:07make mistakes and the compiler simply
- 43:09cannot save you. So here's a quick
- 43:11example. Look at all these strings. Can
- 43:13you tell what exactly they are? How
- 43:14exactly they are different? And at a
- 43:16glance you really can't because they are
- 43:18very very similar. But one of them is
- 43:20player 10. But then that one instead of
- 43:22a zero, that one actually has an
- 43:23uppercase O. This one over here instead
- 43:25of a one actually has a lowercase L. So
- 43:28visually they look very similar. But the
- 43:29reality is all of these are completely
- 43:31separate. All these are completely
- 43:33different things. And importantly also
- 43:35the compiler cannot save you because all
- 43:36of these these are all valid. This is
- 43:38all valid C# code. Even though they are
- 43:40all different, this is all 100% valid.
- 43:43So if you use strings, the compiler
- 43:45cannot help you. you are only going to
- 43:46find those errors during actual runtime
- 43:49and not whilst you're actually writing
- 43:50the code. Whereas you should always
- 43:53prefer using some nice strong types
- 43:55because if you do if you name some kind
- 43:57of variable player 10 but then replace
- 43:59the zero with an O then the compiler
- 44:01won't tell you okay this looks similar
- 44:02but very clearly you wrote this a
- 44:04different way. Did you mean to do that?
- 44:06No, probably not. So if you use some
- 44:07strong types the compiler can help you
- 44:09even though visually it still looks
- 44:11quite different.
- 44:13Now on this here's a very real example
- 44:14that happened to me. So a few years ago
- 44:16like four years ago I was making a
- 44:18spaceship construction system something
- 44:20that is quite interesting and over there
- 44:21I was basically on a live stream
- 44:23building something and I was doing the
- 44:24same thing that I always do using
- 44:26transform.find in order to find some
- 44:28kind of chomp objects. Back then I
- 44:29already knew not to use strings as
- 44:31identifiers and this was basically my
- 44:32one and only exception. I just used find
- 44:35in order to find the chomp objects. So
- 44:36over here I wrote some similar code. So
- 44:38transform.find find the S button and
- 44:40then trying to get the S button making
- 44:42sure okay that is the actual name for
- 44:44the game object. So looking at that at a
- 44:46glance, it seems correct. Everything
- 44:47seems to be very much like it should
- 44:49work. But of course, once I started
- 44:51running the code, there go a dreaded no
- 44:53reference exception. Everything breaks.
- 44:55And yep, this was quite tricky. So then
- 44:57I went and I double checked. Okay, is
- 45:00the code actually saying S button? So I
- 45:02I erased it. I wrote it again. Yes, it
- 45:05is named S button. What about the
- 45:06object? Is it named S button? Yes, it is
- 45:08clearly named S button. So looking at
- 45:10this, it looks like it should work. Why
- 45:12is this not working? Why is this
- 45:13throwing a no reference exception? So it
- 45:15drove me crazy for like 10 20 minutes
- 45:17trying to understand okay the code is
- 45:19correct why is this not throwing an
- 45:20error and the answer is finally after a
- 45:23while I discovered this. So that game
- 45:25object looks like it is named S button
- 45:27but it's actually S button with a space
- 45:29and of course as far as strings are
- 45:31concerned S button and S button space
- 45:33those might as well be completely
- 45:35different things those don't have
- 45:36anything to do with one another. So very
- 45:38correctly the code was saying okay
- 45:40there's nothing named S button because
- 45:41the only thing that exists is S button
- 45:43with a space. So yep I went insane for
- 45:45like 10 20 minutes just because this
- 45:47just because I was using strings to
- 45:48identify a child object. So this
- 45:50terrible you don't want this to happen
- 45:52again. You don't want to suffer all of
- 45:54that all of that nonsense all of that
- 45:57complexity in your own brain. You want
- 45:58to keep things as similar to yourself as
- 46:00possible. So yep, basically after this
- 46:03happened, I decided okay, even for
- 46:04transform.find, I will never ever use
- 46:06that again just to avoid using strings
- 46:08100% of the time. And since then, I have
- 46:11not used them just to try to avoid all
- 46:13of this mistake. Now, also don't use
- 46:16tags since they are based on strings.
- 46:18This is something that you might see in
- 46:19a lot of especially Unity beginner
- 46:21tutorials. They use tags to identify
- 46:23objects, identify things. Don't use them
- 46:25for the same reason because tags are
- 46:27based on strings. So you should not use
- 46:29them. Instead, you should use some kind
- 46:30of tag component. So, if you're working
- 46:32with some kind of game object and you
- 46:33want to know if it's a certain specific
- 46:35type, if so, then just check does it
- 46:37have this component? If so, then it is
- 46:38of this type. If not, then it isn't.
- 46:42Now, if you absolutely must use strings,
- 46:44because sometimes you do have to use
- 46:45them. For example, when working with the
- 46:47Unity animated system, if you're working
- 46:49with parameters, you have to use
- 46:51strings. There's no way around it. The
- 46:52the API really only works with strings.
- 46:54or if you're working with some kind of
- 46:56web API, chances are you're probably
- 46:58going to have to use strings. So if
- 47:00that's the case, if you absolutely must
- 47:01use them, if so, then use a constant
- 47:03instead. That way, you define a constant
- 47:04just once in your entire codebase. And
- 47:07then everywhere else, you just use that
- 47:08constant, you don't use the string. That
- 47:10way, if you make a mistake, at least you
- 47:12can only make the mistake in one place.
- 47:14Everywhere else, if you're using the
- 47:15constant and you make a mistake on the
- 47:16constant name, the compiler will help
- 47:18you. It won't tell you, oh, you wrote is
- 47:20walking incorrectly. You probably didn't
- 47:22mean that. So in this case at least you
- 47:24try to keep yourself a little bit safe.
- 47:26So for the most part try to avoid using
- 47:28strings to identify anything instead.
- 47:30But if you do need to use them if so
- 47:32then use a constant. Essentially
- 47:34[snorts] strings should only ever be
- 47:36used for text. So if you want to tell
- 47:37the player hello if so then go ahead use
- 47:40a string but for identifying anything do
- 47:42not use strings.
- 47:44Next, on the topic of composition over
- 47:47inheritance, which like mentioned
- 47:48beginning, this quite interesting
- 47:50because beginners when they start
- 47:51learning object-oriented programming,
- 47:53they'll learn about inheritance and they
- 47:54tend to overuse it way too much. Whereas
- 47:57the reality is inheritance technically
- 47:59isn't always bad. It can be good in some
- 48:01scenarios, but composition is usually
- 48:03the better option. Inheritance is good,
- 48:06but only in very small, very specific
- 48:08use cases. For example, we've got this,
- 48:10we've got some kind of door object, some
- 48:12kind of chest object. So these does make
- 48:14sense. Both a door and a chest should be
- 48:16unlockable and they should be openable.
- 48:18So in this case this makes sense. You
- 48:20make a base class and all these objects
- 48:21are meant to inherit all of that logic.
- 48:23So in this case it makes sense. But then
- 48:26let's say you want to implement some
- 48:27kind of terminal object and all of a
- 48:29sudden this does not make sense because
- 48:30terminal you can unlock it. So you can
- 48:32type on it but you can't really open a
- 48:35terminal. That does not make sense. So
- 48:37in this case if you do this you end up
- 48:39with the door and chest. Those make
- 48:41sense. Those do implement this logic but
- 48:43terminal is all of a sudden implementing
- 48:45some logic that does not make sense to
- 48:46it open. So you are going to have some
- 48:49kind of function that is probably going
- 48:50to be open that is probably going to be
- 48:52empty because it does not make sense
- 48:53within this context. That is a problem
- 48:55of inheritance. You end up inheriting
- 48:57way more logic than you actually
- 48:59intended. On this topic there's also a
- 49:02funny joke which is you want the banana
- 49:04but what you got was a gorilla holding
- 49:05the banana and the entire jungle. So
- 49:07let's say you've got some kind of banana
- 49:08but the banana inherits from grabbable
- 49:10object and the gorilla interacts with
- 49:12grab object but then the gorilla also
- 49:14inherits from animal and the animal
- 49:16inherits from like organism which the
- 49:18entire jungle inherits from. So yeah
- 49:20basically you want just one tiny thing
- 49:22but because of a very complex very messy
- 49:24inheritance structure because that you
- 49:26end up getting way more than you wanted.
- 49:29So for the most part you should prefer
- 49:31composition usually with interfaces. So
- 49:34in this case you can define two
- 49:35interfaces one unlocks one opens and
- 49:38then you can say okay so the door this
- 49:40one is meant to be unlockable and
- 49:41openable. Same thing for chest but
- 49:43terminal that one only makes sense to be
- 49:45unlockable does not make sense to be
- 49:46openable. So that way you basically
- 49:48compose your objects based on different
- 49:50pieces of behavior and you only add the
- 49:51behavior that you actually need to. And
- 49:54it's a very practical use case which is
- 49:56this. So Unity itself already uses a
- 49:58composition pattern with Unity. you've
- 50:00got the main game object and then you
- 50:02basically just attach components onto
- 50:03it. So if you want a very practical use
- 50:05case of composition over inheritance
- 50:07then Unity itself is a great example.
- 50:11Now in all of this again like I said
- 50:13there's nuance to this. So inheritance
- 50:15isn't always bad. There are some
- 50:16scenarios where inheritance does make
- 50:18sense like we saw for very tiny
- 50:20scenarios where you do want to share all
- 50:22this logic with multiple classes. If so,
- 50:25inheritance might make sense, but I
- 50:26would say in 95% of use cases, chances
- 50:29are a composition pattern is going to be
- 50:31easier.
- 50:33Then finally, the topic of refactoring.
- 50:35So this is another very important thing
- 50:37because this refactoring is a constant
- 50:39process of improving the code quality
- 50:40and this something that you should be
- 50:42doing constantly over and over again. So
- 50:44you try to come up with some kind of
- 50:45game mechanics, some kind of system.
- 50:47First you make it work, then you make it
- 50:49clean and then you're constantly
- 50:50refactoring. You're constantly thinking,
- 50:52is there a better way that I can do
- 50:53this? And if so, then take the time to
- 50:55do it. Now, in this, there's also
- 50:57something that a lot of beginners
- 50:58usually tell me when they see my videos,
- 50:59which is, "Oh, the code quality that I
- 51:02write is pretty terrible when I compare
- 51:03with your videos." And the answer is
- 51:05because they are comparing their first
- 51:06draft with my six or seven drafts.
- 51:08Basically, what you see in a video is
- 51:09after I've done a ton of research, after
- 51:11I've written and rewritten and
- 51:13refactored a ton of code. So, what you
- 51:15see is very much the final version. So,
- 51:17don't compare it to your first draft
- 51:18because your first draft, that one will
- 51:20never be perfect. That is perfectly
- 51:22normal. That is okay. That will not
- 51:24happen. Essentially with the first
- 51:26version, you first understand and
- 51:27actually solve the problem. And once you
- 51:29solve the problem once, then you have
- 51:31quite a bit more understanding on the
- 51:32problem. So you can figure out better
- 51:33ways for solving that same problem. And
- 51:35that is exactly what you do on the
- 51:36second version, you take the solution
- 51:38you came up with and with your better
- 51:40knowledge, with your newer acquired
- 51:41knowledge with that you can basically
- 51:43improve and come up with a better
- 51:44solution. And then afterwards, like I
- 51:47said, constant subsequent versions you
- 51:49are just constantly improving that same
- 51:51problem. So again it's a very constant
- 51:53process something that happens over and
- 51:55over again. Now this some pregnant tips
- 51:58what are some possible refactoring
- 51:59steps. First of all renaming variables.
- 52:02So again like I said you should come up
- 52:03with the best name you can come up with
- 52:05but then after you come up with name
- 52:06constantly try to think is there a
- 52:08better name for this thing. If so take
- 52:09the time to rename it. Refactoring can
- 52:12also mean extracting functions. So if
- 52:14you got some kind of function that you
- 52:15think is doing way too many things. If
- 52:17so, then you probably want to split it
- 52:19into multiple functions and each
- 52:20function do one specific thing.
- 52:22Refactoring can also mean splitting a
- 52:24class. So if you define a class that is
- 52:25meant to represent, let's say, the
- 52:27player movement, but then you end up
- 52:28doing more than just a player movement.
- 52:30If so, that might be worthwhile. Split
- 52:32it into multiple classes. But at the
- 52:34same time, if you split it too much,
- 52:35refactoring might just mean combining
- 52:37some oversplit code. Sometimes you can
- 52:39go way too insane and split things all
- 52:41over the place and you realize, okay, if
- 52:43some of these things were together, it
- 52:44would actually make the code simpler. If
- 52:46so, refactoring can also mean just
- 52:48combining oversplit code. Refactoring is
- 52:51also all about removing that code. So
- 52:53game development is a very iterative
- 52:55process. You're constantly trying out
- 52:56new things trying to see what works. So
- 52:58let's say you figure out, okay, I'm
- 52:59going to make this kind of mechanic, try
- 53:00to see if it works. So you build that
- 53:02mechanic, but then you realize, uh, this
- 53:04doesn't quite work right. Let's go with
- 53:06another mechanic. Let's go in another
- 53:07direction. And once you do that, that's
- 53:08perfectly normal. That is the entire
- 53:10purpose behind game development. But
- 53:11once you do that, definitely go back and
- 53:13try to get rid of that dead code. You
- 53:15don't want it to keeping around because
- 53:17then you don't know okay why is this not
- 53:18being used anywhere is it intentional is
- 53:20it not no so for refactoring make sure
- 53:23any dead code you try to remove it as
- 53:24much as possible
- 53:26or assembly refactoring is just
- 53:28simplifying an over complicated
- 53:29abstraction. So if you come up with some
- 53:31kind of pattern and you have some kind
- 53:33of object that inherits from like a
- 53:36bunch of different base classes and
- 53:37implements a bunch of interface and a
- 53:39bunch of things sometimes you can go way
- 53:41too complex with all the kind of levels
- 53:43of abstraction. So sometimes simplifying
- 53:46that might be the better thing. So
- 53:47constantly trying to think okay is there
- 53:49a better way for me to solve this
- 53:50problem something that is simpler that
- 53:52is easier to understand if so go ahead
- 53:54refactor that. But the important thing
- 53:56is this it's a constant never- ending
- 53:58process. You are constantly refactoring
- 53:59things. you're constantly thinking okay
- 54:01what exactly am I doing here what could
- 54:03I do better constantly try to think that
- 54:05and constantly try to improve the
- 54:06quality of your code
- 54:09now that let's do a quick simple live
- 54:11refactor just so we can see how to apply
- 54:13all these guidelines in nice practical
- 54:16manner okay so here we've got some code
- 54:18it's supposed to be some kind of keypad
- 54:20or system so like the gift here is
- 54:22showing so basically we want to have
- 54:23some kind of keypad on some kind of door
- 54:25then we want to input some kind of code
- 54:27and if we get the right code it should
- 54:28open door And basically that's how it
- 54:30works. So input one, two, three, four
- 54:32and then the door basically opens. We
- 54:34can walk through just like that. So very
- 54:35simple keypad system.
- 54:39So here we've got that and we've got
- 54:41some code and okay it is doing exactly
- 54:43that. But this is some pretty bad code.
- 54:46We can see for the start how we can
- 54:48already find quite a lot of mistakes. So
- 54:50we can see everything is public. We can
- 54:52see the names are pretty bad. We can see
- 54:53everything both the logic, the
- 54:55animations, the audio it's all in just
- 54:57one place. So that is not correct. So
- 54:58instead, let's start off by fixing this.
- 55:00First of all, like I said, the most
- 55:01important thing are naming rules. So we
- 55:03should give them some proper names. So
- 55:05for here, instead of code, let's put it
- 55:07like correct code. And again, I'm using
- 55:09the ID in order to actually rename and
- 55:11renames every instance of that. Then for
- 55:13the current, this is actually the
- 55:15current code. So let's give it some
- 55:17proper name. Then for txt, again, don't
- 55:20use abbreviations, don't use
- 55:21dimminatives. So over here UI text mesh
- 55:26which again we're going to refactor this
- 55:28in a little bit. Then if you add the
- 55:29animator instead of animator like that
- 55:32then the audio source audio source and
- 55:35then boom open instead of that is open
- 55:38just like this and okay again just like
- 55:40this with some proper names and let's
- 55:41also add some proper formatting just
- 55:43like this it is already quite a bit more
- 55:45understandable you can just look at the
- 55:46names of the things and you can easily
- 55:47understand what exactly it is doing.
- 55:49Next let us not make things public. Let
- 55:52us hide as much information as possible.
- 55:54So this should be private. So let's put
- 55:56this one private. All of these as
- 55:59private like this. And the function
- 56:01itself is meant to be called from
- 56:02outside. So that one does make sense to
- 56:04be public. Now if we make it private, we
- 56:06cannot assign it in the editor. Let's
- 56:08say for the correct code, we actually
- 56:09intentionally did want to do it in the
- 56:12editor. If so, let's add a serialized
- 56:14field attribute. That way it won't show
- 56:16up in the editor, but it won't be
- 56:17private, so it's not accessible by other
- 56:19classes. Next up, let's solve some magic
- 56:21numbers. So, for example, over here
- 56:24we've got a four. What does this four
- 56:26represent? Now, looking at it for a
- 56:27little bit, I can see, okay, so this is
- 56:29meant to be the correct code. That is
- 56:30meant to be the actual length of the
- 56:32code. But again, we shouldn't have to
- 56:34try to guess that. It should be very,
- 56:35very clear. So, over here, we can either
- 56:37use an int for the correct code length.
- 56:40We can use that and set it to four. We
- 56:42can do this or in this case since that
- 56:44one is actually meant to match up what
- 56:45we have over here on the correct code
- 56:47function because that we should really
- 56:49just give it a even better name. So
- 56:52instead of using a local variable for
- 56:54that we can just use the correct code by
- 56:56itself use.length and that way we have
- 56:59just one source of truth. So we make
- 57:00sure that even if we modify the correct
- 57:02code if we put it five characters
- 57:04everything still works. Okay. So next up
- 57:07like I said do not use strings to
- 57:09identify things. over here for the
- 57:11boolean for this parameter. Like I said,
- 57:13you can't really avoid this. You have to
- 57:15use this. So that really sucks, but that
- 57:17is the way it works. So if we have to
- 57:19use strings, if so, let's use a proper
- 57:22constant. So a private const. Let's make
- 57:25a con string
- 57:27and name it open and equals open. And
- 57:30that way we just need to make sure that
- 57:32this one over here is correct. And if
- 57:33this one is correct, then everything is
- 57:35going to work. So down here we just use
- 57:37a constant as opposed to using a string.
- 57:40Okay. So this is already looking quite a
- 57:42bit better. But of course we are still
- 57:44doing way too many things here. We are
- 57:45handling both the logic. We're handling
- 57:47the animation, the UI, the audio and so
- 57:48on. That is not correct. So let us split
- 57:51those up by quite a bit. And over here
- 57:52by the way I'm using everything in just
- 57:54one class just because to make it easier
- 57:56over here for this video presentation.
- 57:59Whereas obviously in Unity I would
- 58:00create separate files and actually
- 58:02attach them. put over here. So let's say
- 58:03a public class and let's say one for the
- 58:07keypad door UI and I want this class to
- 58:10basically just handle the UI and nothing
- 58:11else. So the UI text mesh that one
- 58:12should not be here. That one should
- 58:14instead be right here. And of course you
- 58:16need to know okay how exactly do I make
- 58:18this work? How exactly do I connect this
- 58:20logic? Do I know when this one is
- 58:22actually modifying? And the answer to
- 58:24that to keeping things decoupled is to
- 58:25use some nice events. So over here we
- 58:27can find a public event using event
- 58:30handler just because that's the standard
- 58:32C# thing
- 58:34and we can use on current code changed.
- 58:38So we can do this kind of event and then
- 58:42we are going to invoke this event. So
- 58:44over here whenever the code changes we
- 58:46invoke that. So on current change we do
- 58:49invoke and pass in this and event args.
- 58:55So we close that out and then down here
- 58:57when the code is reset we do the same
- 58:59thing. So then down here we just need to
- 59:01listen that event which means we need
- 59:02the reference to this. Now in this case
- 59:04like I said imagining this was a Unity
- 59:05game. So I would have a kind of
- 59:08serialized field. So serialize field
- 59:10private of keypad door for the keypad
- 59:14door. So I've got this one right here.
- 59:17And then on private void start on start
- 59:22here I can basically just go onto that
- 59:23one and hook on to on code change. I
- 59:26hook onto that event. Then on this one
- 59:28now I can go on the UI text mesh and set
- 59:30that text and set it to what that one
- 59:33currently has. So again information
- 59:35hiding. So we want to keep the current
- 59:37code as actual private and only expose
- 59:39it if we need to which in this case we
- 59:40do need to. So let's make a public void
- 59:44get current code. Actually, it's not
- 59:46void, but actually returning a string.
- 59:49So, on this one and this one, we do
- 59:51return that. So, return the current
- 59:53code. Return that. And then down here,
- 59:56we can actually do that. So, we can go
- 59:57on the keypad door and get the current
- 1:00:00code and two string on this one. And
- 1:00:02yep, there you go. So, now up here, we
- 1:00:04don't use UI text mesh. We don't use any
- 1:00:07of this. Nope. We just fire off events.
- 1:00:09And then we've got the UI class. That
- 1:00:10one is listening to those events and
- 1:00:11actually applying those changes. Now,
- 1:00:13importantly is the keypad door. This one
- 1:00:15over here is completely decoupled from
- 1:00:16the UI. So I could get rid of this class
- 1:00:19entirely and the code would still
- 1:00:20compile because everything is nicely
- 1:00:22decoupled.
- 1:00:23Okay. So next we've got basically the
- 1:00:25same thing for the audio source and for
- 1:00:26the animator. So over here let's do
- 1:00:28those. So public class the keypad door
- 1:00:32animations.
- 1:00:34So we've got this one right here. And
- 1:00:36over here same thing we're going to have
- 1:00:37a reference to the keypad door. We're
- 1:00:40going to have that one. And on private
- 1:00:41void start we're basically going to
- 1:00:43listen to that event except over here we
- 1:00:46need to make another event. So let's see
- 1:00:47when is the animator meant to change.
- 1:00:49It's basically when the door is open
- 1:00:51which is when the correct code is
- 1:00:52inputed. So public event handler let's
- 1:00:56make on
- 1:00:58correct code ins certain. So we do this
- 1:01:02and then over here we're going to invoke
- 1:01:04this event. So invoke
- 1:01:08with this and event args. empty. So we
- 1:01:11have that one. And for the animator set
- 1:01:12B, we're basically just going to do that
- 1:01:14down here. So on start, let's go on the
- 1:01:17keypad door on the correct code insert.
- 1:01:21Let's attach this one right here. And on
- 1:01:23this one, that is when we are actually
- 1:01:25going to set the binding, which means
- 1:01:27down here is when we need to have this
- 1:01:29constant right here. And we also need a
- 1:01:32reference for the animator right here
- 1:01:34like this. And also make it a serialized
- 1:01:37field so we can set it in the editor.
- 1:01:40So there I go. So that handles the
- 1:01:41animations. And finally for the audio
- 1:01:43source, that one is quite simple. So
- 1:01:45public class keypad door audio. And on
- 1:01:50this one, we're going to do very much
- 1:01:51the same thing. So again, let's first
- 1:01:53grab a reference. So we grab this one
- 1:01:56right here. And then on private void
- 1:01:59start on start, we're going to hook onto
- 1:02:01that one. So keypad door. when the
- 1:02:04correct code is inserted. When that
- 1:02:06happens, then we're going to want to do
- 1:02:08the same thing that we're doing up here.
- 1:02:10So, audio source.play just like this, we
- 1:02:13play it. And then over here, we get that
- 1:02:15audio source. And we're also going to
- 1:02:17expose it. So, over here, also make it a
- 1:02:22serialize
- 1:02:24field like this. And yep, there you go.
- 1:02:28All right. So, yep. So, I think that's
- 1:02:29about it. Now technically we could
- 1:02:30improve this maybe a little bit more but
- 1:02:32for the most part this quite good. We
- 1:02:34could use like struct objects for the
- 1:02:35correct code and a bunch more but for
- 1:02:37the most part this is quite nice. So we
- 1:02:39can see the code that we started with.
- 1:02:41We basically cleaned up the names. We
- 1:02:42cleaned up the access modifiers and we
- 1:02:45made things nicely decoupled. So you've
- 1:02:47got multiple classes as opposed to one.
- 1:02:49And each class does one thing and one
- 1:02:50thing only. So for example if we got
- 1:02:52some kind of bug on the audio we know it
- 1:02:54has to be on this class because these
- 1:02:56other ones are completely decoupled. So
- 1:02:58here we have a much better piece of code
- 1:03:00that if you just looked at this code, if
- 1:03:02you had no context of this project, just
- 1:03:04looking at this code, you can instantly
- 1:03:06see okay this one is handling the logic,
- 1:03:07this one handling the visuals, the
- 1:03:08animations and so on and you can easily
- 1:03:11just by reading the names you can
- 1:03:12understand. Okay, so that is what this
- 1:03:14is doing. So this is much much easier to
- 1:03:16follow. Now if you want to see another
- 1:03:17detailed refactoring tutorial, you can
- 1:03:19go ahead and watch this video. By the
- 1:03:20way, there's a link at the end of the
- 1:03:22slides, but yep, this one 30-minute
- 1:03:24video where I did basically the same
- 1:03:25thing that I covered here. basically
- 1:03:26going through that cleaning up the names
- 1:03:27access modifiers and all those things.
- 1:03:30All right. So a quick recap of
- 1:03:31everything that we covered in this talk.
- 1:03:33So first of all definition what is clean
- 1:03:34code? That is code that is easy to
- 1:03:36understand and easy to modify. So for
- 1:03:38that first we'll learn about that
- 1:03:40definition. What is and importantly what
- 1:03:41is not clean code. Both are very
- 1:03:43important. Then we learned why does all
- 1:03:45of this matter if the code just works.
- 1:03:47So hopefully throughout this entire talk
- 1:03:48eventually convince you that just
- 1:03:50working is not enough if you want to be
- 1:03:51a proper professional. if you want to
- 1:03:53make some proper games. If so, you need
- 1:03:54to write some proper clean code so you
- 1:03:56can actually finish your projects. Then
- 1:03:58we covered naming rules. So basically
- 1:04:00the most important part of this talk. If
- 1:04:01you just follow this one thing, I
- 1:04:02guarantee your code quality will
- 1:04:04massively improve. Then we covered how
- 1:04:06decoupling is a superpower. Trying to
- 1:04:08keep things as separated as possible.
- 1:04:10That helps out your brain quite a bit.
- 1:04:11So you can work on the code base without
- 1:04:13having to keep a massive amount of code
- 1:04:15inside of your head. Then we cover the
- 1:04:17single responsibility principle. how
- 1:04:19each class each function should do one
- 1:04:21thing and one thing only. This is
- 1:04:22another guideline that is relatively
- 1:04:24easy to follow and if you just follow
- 1:04:25this your code quality will increase
- 1:04:27massively. After that we covered
- 1:04:29information hiding. So always about
- 1:04:31trying to minimize your mental load,
- 1:04:32trying to minimize how much code you
- 1:04:34have to keep in your head and if you
- 1:04:35hide things as much as possible. If so
- 1:04:37then helps out by quite a lot. Next, one
- 1:04:40of the better debugging tips, one sort
- 1:04:42of truth. Every piece of that in your
- 1:04:44game should exist in one place and one
- 1:04:46place only. So if you focus on that it
- 1:04:48won't solve so many possible bugs. Then
- 1:04:51we covered some documenting code. So how
- 1:04:53comments they should be used very
- 1:04:55sparingly. For the most part the code
- 1:04:56itself should be very clear what exactly
- 1:04:58it represents what exile it's doing and
- 1:05:00comments should explain the why and not
- 1:05:02the what. Then we covered how magic
- 1:05:04numbers and how you should not use them.
- 1:05:06And instead of using magic numbers you
- 1:05:08should use some proper name variables.
- 1:05:09That way the meaning is much more clear.
- 1:05:12Then we also covered strings and
- 1:05:13identifiers and why you should not use
- 1:05:15them. It is very easy to make mistakes.
- 1:05:17You can write strings that look very
- 1:05:18very similar but they are very different
- 1:05:20and you are only going to find that
- 1:05:22difference once you try to run the code.
- 1:05:23You are going to come up with all sorts
- 1:05:25of errors. Next we covered composition
- 1:05:27over inheritance. So these are two
- 1:05:29interesting patterns and for the most
- 1:05:31part I would say composition is what you
- 1:05:33should be using even though inheritance
- 1:05:34sometimes might be the right answer. And
- 1:05:37finally refactoring how everything that
- 1:05:38we're doing here this meant to be a
- 1:05:40constant never- ending process. You are
- 1:05:41constantly meant to be thinking about is
- 1:05:43there a better way for me to do this?
- 1:05:44And if so, take the time to refactor and
- 1:05:46make it better. But at the same time,
- 1:05:48remember how these are just guidelines.
- 1:05:50These are not unbreakable rules. So
- 1:05:52always think, does this code does this
- 1:05:54make the code easy to understand and
- 1:05:56easier to modify? If so, follow the
- 1:05:57guideline. If not, then don't follow it.
- 1:06:01The ultimate goal behind everything that
- 1:06:02we learned in this talk is this. The
- 1:06:04entire goal behind writing clean code is
- 1:06:06it helps you make more complex, more
- 1:06:07interesting, better games. And
- 1:06:08importantly, actually finish them.
- 1:06:10Actually take them to the finish line. I
- 1:06:12want you to have lots of very
- 1:06:13successful, very complete projects as
- 1:06:15opposed to just a mountain of unfinished
- 1:06:17prototypes. So for that, focusing on
- 1:06:19clean focus on focusing on clean code
- 1:06:21quality will help you achieve the final
- 1:06:23product much better. Now if you want to
- 1:06:25learn a bit more, here are some more
- 1:06:27resources. So first of all, this is my
- 1:06:29favorite book of all time, code complete
- 1:06:312. It is a massive book. It is like 500
- 1:06:33pages long. It is insanely detailed,
- 1:06:35insanely deep, insanely massive, but it
- 1:06:37contains a lot of very good information.
- 1:06:40This is the book that when I read it
- 1:06:41massively increased the quality of my
- 1:06:43code by like a thousandx. So I highly
- 1:06:45highly recommend it. Definitely take
- 1:06:47your time to go through it because it is
- 1:06:48very technical, very deep. But if you
- 1:06:50take the time to go through it, it will
- 1:06:51massively increase the quality of your
- 1:06:53code. Then of course I've got my YouTube
- 1:06:55channel where I've got a ton of
- 1:06:56tutorials and a ton of courses. So on
- 1:06:58all of those, I always try to teach you
- 1:07:00how to write proper clean code. I don't
- 1:07:02teach the quick and route. No. So if you
- 1:07:04want to see a very practical application
- 1:07:06of everything that we covered here, go
- 1:07:07ahead, watch these videos. watch these
- 1:07:09tutorials.
- 1:07:11And also on that, if you go onto this
- 1:07:13link, if you scan this QR code, you can
- 1:07:15go to a place where you can find all
- 1:07:16these slides. So, you can basically
- 1:07:17refer back to it. You can also find that
- 1:07:20video that I mentioned a while ago. And
- 1:07:21in there, actually, there's some nice
- 1:07:23bonus coupons. So, I've got my C#
- 1:07:25course, my complete C# course, which has
- 1:07:27a free 12-hour video over here on
- 1:07:29YouTube. That free video contains all
- 1:07:31the video lectures. So, if you just want
- 1:07:32the video, you can watch that for free.
- 1:07:34But if you can afford them, you've got
- 1:07:35the premium version. And that one
- 1:07:37contains a bunch of awesome bonuses.
- 1:07:39Specifically, contains freely last
- 1:07:40questions, quizzes, and interactive
- 1:07:42exercises. Those basically require you
- 1:07:44to actually write code in order to solve
- 1:07:45the exercises. That means you are
- 1:07:47actually learning the contents of each
- 1:07:48lecture. And for that, for the premium
- 1:07:50version, you can use the bonus coupon
- 1:07:52clean to get a bonus discount off. Same
- 1:07:55thing for my problem solving course.
- 1:07:56Basically, programming is all about
- 1:07:58experience. Trying to gain as much
- 1:07:59experience as possible. That is the
- 1:08:00closest thing there is to achieve code.
- 1:08:02The more code you write, the better you
- 1:08:03become. And my problem solving course is
- 1:08:05basically meant for that. It is a
- 1:08:06collection of over 200 exercise that you
- 1:08:08can solve. And if you solve all of them,
- 1:08:10I guarantee your skills as a programmer
- 1:08:13will massively improve. And also
- 1:08:15recently I started my game that practice
- 1:08:16lab that is my weekly game mechanic
- 1:08:18challenge. So basically every two weeks
- 1:08:20I give you a new challenge like
- 1:08:21implement the CCV camera system or
- 1:08:23implement a safe system. I give you the
- 1:08:25starting project files. I give you a set
- 1:08:27of objectives and you basically have to
- 1:08:28download those project files and try to
- 1:08:30achieve those objectives. try to
- 1:08:31actually implement those and with that
- 1:08:33one you can use the coupon clean in
- 1:08:35order to get the first month off. So go
- 1:08:38ahead check out all these check out all
- 1:08:39these resources and yep I hope you enjoy
- 1:08:41the song. Now go ahead write some clean
- 1:08:43understandable and easy to modify code.
- 1:08:45Thank you.
About this transcript
This page contains the full transcript of How to Write High Quality Code that doesn't fall apart. by Code Monkey, generated from the public captions YouTube serves with the video. The transcript has 15,862 words across 2,232 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.