YouTube2Text

How to Write High Quality Code that doesn't fall apart. — Transcript

by Code Monkey · 15,862 words · 2,232 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Hello and welcome. I'm your code monkey.
  2. 0:02All right, so welcome to my talk which
  3. 0:03is titled how to write game code that
  4. 0:05doesn't fall apart. This is going to be
  5. 0:07all about clean code. So what is it and
  6. 0:09why it's something so important? So this
  7. 0:12is my goal with this song. So my goal is
  8. 0:14for you to learn how to write high
  9. 0:16quality clean code so you can make some
  10. 0:17proper professional games as opposed to
  11. 0:19just disposable prototypes. That's an
  12. 0:21issue that a lot of people have,
  13. 0:22especially a lot of beginners. They can
  14. 0:24make some prototypes and sure they write
  15. 0:26whatever kind of code to get it to work.
  16. 0:27But if you want to be a prop
  17. 0:29professional developer, if you want to
  18. 0:30make some actual proper games, if so,
  19. 0:32you need to take some good practices. So
  20. 0:34you end up building some actual games as
  21. 0:36opposed to some disposable prototypes.
  22. 0:38So to achieve this, we're going to go
  23. 0:40through a bunch of things in this talk.
  24. 0:42So first of all, we're going to cover
  25. 0:43what is and what is knuckling code.
  26. 0:45Starting off with some nice definition
  27. 0:46so we know exactly what we're talking
  28. 0:48about. Then we're going to cover why
  29. 0:50does all of this matter if the code just
  30. 0:52works. This is a common objection that I
  31. 0:54get whenever I talk about the importance
  32. 0:56of writing clean code. So I won't cover
  33. 0:58that. I won't cover how just the code
  34. 1:00working is not enough. Making it proper
  35. 1:02is the way that it should be done. There
  36. 1:04we go. Talked about one of the most
  37. 1:05important things. So if you take just
  38. 1:07one thing out of this talk, I hope you
  39. 1:08take this naming rules. They are pretty
  40. 1:10simple to implement and this one change
  41. 1:12alone will massively increase the
  42. 1:14quality of your code. After that we are
  43. 1:16going to talk about how decoupling is a
  44. 1:19superpower. Decoupling is basically the
  45. 1:21process by taking your systems and
  46. 1:22making them as separated as possible. So
  47. 1:24while you're working on one thing, you
  48. 1:25just focus on that one thing and nothing
  49. 1:27else. After that, we're going to talk
  50. 1:30about the single responsibility
  51. 1:31principle. This is another one that is
  52. 1:33greatly going to improve the quality of
  53. 1:35your code. It's how each thing, each
  54. 1:37class, each function should do one thing
  55. 1:38and one thing only. After that, we're
  56. 1:41going to cover the process of
  57. 1:42information hiding. This is meant to
  58. 1:44help you basically manage the mental
  59. 1:46load you have to keep in your head. So,
  60. 1:48as you're working on some kind of game,
  61. 1:49you don't have to keep a massive amount
  62. 1:51of code in your head as you're working
  63. 1:52on it. If you minimize how much you are
  64. 1:54actually exposing, if you hide as much
  65. 1:56information as possible, that is going
  66. 1:58to help you out quite a bit. Then, we're
  67. 2:00going to cover one of the easiest ways
  68. 2:02basically to make debugging the easier
  69. 2:04possible. We're going to cover the
  70. 2:05process of how one source of truth, so
  71. 2:07each piece of that in your game should
  72. 2:09exist in one place and one place only as
  73. 2:11opposed to having copies all over the
  74. 2:12place, which like I said, that will help
  75. 2:14with debugging by quite a bit. After
  76. 2:16that, we're going to cover a slightly
  77. 2:18more controversial topic on
  78. 2:19self-documenting code. So, this all
  79. 2:21about comments. Should you write
  80. 2:23comments? Should you not write comments?
  81. 2:24When to use them, when not to use them.
  82. 2:26So, here I won't cover that topic. Then
  83. 2:29a fun one. So, topic about magic
  84. 2:31numbers. We're going to cover what
  85. 2:32exactly they are and why you should not
  86. 2:34use them and what you should use
  87. 2:36instead. Then we're going to talk about
  88. 2:38topic of strings as identifiers, which
  89. 2:40is something you should also not do. You
  90. 2:42should not use strings to identify
  91. 2:43anything. So again I will talk about why
  92. 2:45you shouldn't use them. What are the
  93. 2:47main problems with it and what are the
  94. 2:48alternatives that I recommend. Then on
  95. 2:51topic of composition over inheritance.
  96. 2:53So when beginners first start learning
  97. 2:54about object-oriented programming
  98. 2:56usually they tend to overuse inheritance
  99. 2:58way too much. So over here I want to
  100. 3:00give you basically a better alternative
  101. 3:02focusing on composition instead of
  102. 3:03inheritance. After that comes also a
  103. 3:06very important thing which is
  104. 3:07refactoring. So basically everything
  105. 3:09we're going to talk uh everything we're
  106. 3:11going to cover in this entire talk is
  107. 3:12not meant to be something that you run
  108. 3:14just once but rather it's meant to be a
  109. 3:15constant iterative process. So over here
  110. 3:17the final topic is all about that all
  111. 3:19about how refactoring is a constant
  112. 3:20process. So we should be applying all of
  113. 3:22these guidelines. We should be applying
  114. 3:24them not once but over and over again.
  115. 3:27Now this is meant to be a very practic.
  116. 3:29It is not meant to be just nonsense
  117. 3:32theory that is just meant for academics
  118. 3:34or something like that. No, this is
  119. 3:35meant to be very very practical. So
  120. 3:37after you watch this video, I want you
  121. 3:38to take all of this and immediately
  122. 3:40apply them to your code and you'll see
  123. 3:41how the code quality will be much much
  124. 3:43better. So before we get to some
  125. 3:46specifics, just a quick example so we
  126. 3:48know what we're talking about. So a
  127. 3:49quick example of bad code versus clean
  128. 3:50code. So over here here we have example
  129. 3:53of some pretty terrible code. So it's a
  130. 3:55pretty small piece of code. So just by
  131. 3:56looking at it, you can probably guess
  132. 3:58what exactly this is doing. But still
  133. 4:00takes you quite a bit of time trying to
  134. 4:01understand, okay, what do all these
  135. 4:03variables represent? What does the class
  136. 4:05represent? what are these functions
  137. 4:06doing? Technically, like I said, it's a
  138. 4:08very small thing. So, if you think about
  139. 4:09it for a few minutes, you can figure out
  140. 4:11what this is doing. But still, it
  141. 4:13requires quite a lot of mental effort
  142. 4:14trying to figure that out. Whereas over
  143. 4:16here, here we have the exact same code,
  144. 4:18but now written with some proper clean
  145. 4:20code practices. And we can see very
  146. 4:21instantly, okay, that one takes you a
  147. 4:24few minutes to understand. Whereas this
  148. 4:25one over here, very instantly
  149. 4:26understandable. You just read the names
  150. 4:27and you can instantly understand, okay,
  151. 4:29this is a health system. We've got a
  152. 4:31health field. We've got functions to
  153. 4:32damage, to heal, and so on. So, yep,
  154. 4:34just at a glance, we can see how both of
  155. 4:36these, that one is very difficult to
  156. 4:38understand, whereas this one is very
  157. 4:39easy to understand. That is the entire
  158. 4:41point behind writing clean code. Also, I
  159. 4:43should point out how over here, the
  160. 4:45compiler doesn't care. These two are
  161. 4:47basically exactly the same thing as far
  162. 4:48as the compiler is concerned. The
  163. 4:49compiler really just converts both of
  164. 4:51these into binary and then just executes
  165. 4:52it. So, everything we're going to talk
  166. 4:54about, all these clean code guidelines,
  167. 4:56they're really meant for your benefit as
  168. 4:58a programmer. The compiler doesn't care.
  169. 4:59The code runs just the same. So this is
  170. 5:01all meant to help you as a programmer
  171. 5:03being able to work easier and with fewer
  172. 5:05issues. Also since I point out the
  173. 5:08massive amount difference that it takes
  174. 5:10between understanding that version and
  175. 5:11this version which is what I was saying
  176. 5:13a while ago difference between making a
  177. 5:15disposable prototype versus making a
  178. 5:16proper game. So if you're working on
  179. 5:18some kind of game for a short amount of
  180. 5:19time doesn't matter too much but imagine
  181. 5:21so that one takes you 10 minutes to
  182. 5:22understand whereas this one takes you a
  183. 5:24few seconds. Imagine that spread across
  184. 5:26making a proper project that takes 6
  185. 5:28months to one year and you can see the
  186. 5:30massive time savings that you get if you
  187. 5:32understand your code in a few seconds
  188. 5:33versus several minutes. Now, before I
  189. 5:36give you some specific advice, just who
  190. 5:38I am so you know where I'm coming from
  191. 5:40and why I can give you this advice.
  192. 5:42First of all, my name is Hugo. I love
  193. 5:43programming. I've been writing code for
  194. 5:45over 25 years. I started off a very long
  195. 5:47time ago back when I was 10 years old. I
  196. 5:49started writing merk scripts which I'm
  197. 5:51guessing nowadays most people don't know
  198. 5:52what is IRC. That was basically the chat
  199. 5:54rooms back in the day. Whereas nowadays
  200. 5:56you've got like WhatsApp groups and
  201. 5:57something like that. That was kind of
  202. 5:58the similar one. And that one I could
  203. 6:01basically customize a program in order
  204. 6:02to make some chat bots. So I made some
  205. 6:04nice trivia games, some hangman
  206. 6:05[clears throat] games. There was no
  207. 6:06visuals. It was really all just
  208. 6:07textbased. But still I could make some
  209. 6:09bots and talk with the bots. So that is
  210. 6:10how I first learned how to write
  211. 6:12programming.
  212. 6:14Then in 2008, that is when I first
  213. 6:16discovered Flash. And before that, I was
  214. 6:18used to playing games just like every
  215. 6:19kid my age, playing web- based games.
  216. 6:21just go in school, go on the computer,
  217. 6:23start playing some flash games. That was
  218. 6:24fun. But this is when I realized, okay,
  219. 6:26I can actually not just play games, but
  220. 6:28I can actually make those games. So that
  221. 6:29is when I discovered Flash was the
  222. 6:31program that people were using to build
  223. 6:33those games. That is when I started
  224. 6:35doing that. Started making a bunch of
  225. 6:36flash games, publishing them on
  226. 6:38Newgrounds, congregate, all those
  227. 6:39things. And eventually over time, I also
  228. 6:42learned that you could put some ads on
  229. 6:44those games, which meant those games
  230. 6:45could actually be a source of income. So
  231. 6:46that is when I started thinking about
  232. 6:48game development as sort of a potential
  233. 6:49career. So I made that for about four
  234. 6:51years. I made an insane amount of flash
  235. 6:53games. And eventually in 2012, that's
  236. 6:55when I decided, okay, I like web games.
  237. 6:57But more and more what I really like
  238. 6:59playing are really just PC games. So I
  239. 7:01would like to make those kinds of games.
  240. 7:02And flash can't really quite do it. So
  241. 7:05that is when I started looking, okay,
  242. 7:06how can I make some PC games? And that
  243. 7:08is when I went looking and I found
  244. 7:10Unity. I think back then GDAU did not
  245. 7:12exist. Unreal was paid. So Unity was the
  246. 7:15only option. It was completely free. So
  247. 7:17I downloaded it, started using it. And
  248. 7:19since then, I have published nine games
  249. 7:21on Steam. Here's just an example of some
  250. 7:23of my games. Now, my games have never
  251. 7:25been a massive hit. I don't have any
  252. 7:26kind of game that most people would
  253. 7:28recognize, but still, my games have
  254. 7:30managed to allow me to make a living as
  255. 7:32a solo indie game dev for over a decade.
  256. 7:34And all that time, I've made about 1.1
  257. 7:36million in gross revenue. So, the games
  258. 7:38have done quite decently well. And then
  259. 7:41in 2018, that's when I started my
  260. 7:42YouTube channel. That's when I figured
  261. 7:43out, okay, I'm already I've already
  262. 7:45spent all this time making games. I have
  263. 7:47quite a bit of knowledge and I know a
  264. 7:49lot of people would learn how to would
  265. 7:50love to learn how to make games. So why
  266. 7:52don't I try doing that? Why don't I
  267. 7:54start making some tutorials trying to
  268. 7:56teach the knowledge that I have gained
  269. 7:57using my own games teaching very
  270. 7:59practical knowledge. So teaching things
  271. 8:00that I actually learned whilst making my
  272. 8:02actual Steam games and basically that's
  273. 8:04what I started doing. Started making
  274. 8:05some nice YouTube videos. And since then
  275. 8:07I have made over 800 videos something
  276. 8:10like that. This is my most popular video
  277. 8:123.7 million views a complete 10-hour
  278. 8:14free course. It is really nice. So yep,
  279. 8:16this has been my journey and throughout
  280. 8:17all this time basically I learned the
  281. 8:19importance of clean code and that is why
  282. 8:21it is something so important which is
  283. 8:22actually topic on this slide right here
  284. 8:25why clean code is so important to me
  285. 8:26because it is something very important
  286. 8:27to me and the reason is simply basically
  287. 8:30I want to help you avoid the mistakes
  288. 8:31that I went through that's entire
  289. 8:32purpose behind this talk basically I did
  290. 8:35not always write high quality clean code
  291. 8:37like everybody else I started off by
  292. 8:38writing some pretty nasty code I started
  293. 8:40writing code that just worked that was
  294. 8:42the only purpose as long as it works and
  295. 8:44it's fine but Of course that led to
  296. 8:46quite a lot of problems. So I suffered
  297. 8:48quite a lot of frustration due to bad
  298. 8:49names, confusing logic, all of those
  299. 8:51things. So basically I suffered through
  300. 8:53it quite a lot. Whereas if I had focused
  301. 8:55on these guidelines right from the
  302. 8:56start, my code quality would have been
  303. 8:57higher and I would have avoided quite a
  304. 8:59lot of frustration. So basically that's
  305. 9:01my goal with here. Basically trying to
  306. 9:03teach you all these concepts so you can
  307. 9:04avoid all the mistakes and all the
  308. 9:06frustration that I went through. Now on
  309. 9:09topic of clean code that is actually a
  310. 9:11very nebulous topic. It is something
  311. 9:13that everyone seems to have their own
  312. 9:15definition. If you go on Google, you'll
  313. 9:16find like a dozen different definitions.
  314. 9:18So, I want to give you my specific
  315. 9:20definition just so you know exactly the
  316. 9:21context for everything we're talking
  317. 9:23about in this talk. And my definition is
  318. 9:25quite simple. It is just this clean code
  319. 9:27is code that is easy to understand and
  320. 9:29easy to modify. That's it. Very simple,
  321. 9:31very basic, very straightforward. That
  322. 9:33is it. If your code is easy to
  323. 9:35understand and if you can easily modify
  324. 9:36it, if so, then I call that clean code.
  325. 9:39So, that's it. That's basically my
  326. 9:40definition. Whereas with some dirty code
  327. 9:42like we saw a while ago, we have to
  328. 9:44stare and we have to try to guess what
  329. 9:45is the code doing. Whereas with clean
  330. 9:47code, you can really just look at it and
  331. 9:48just by reading it, you should be able
  332. 9:50to easily understand it. This is my
  333. 9:52definition quite simple. And as to why
  334. 9:54this is important, it is one of the
  335. 9:56reasons is because this is because you
  336. 9:58actually spend a lot more time reading
  337. 9:59code than you do writing code. I believe
  338. 10:01the actual number is something like 10
  339. 10:03times. So for every one minute you spend
  340. 10:05writing code, you spend 10 minutes
  341. 10:06reading that kind of code. So that means
  342. 10:08that it does pay to actually pay
  343. 10:10attention to how you're writing your
  344. 10:11code so that it is easy to read because
  345. 10:14that is what you're going to spend the
  346. 10:15most amount of time doing.
  347. 10:18Ultimately the entire purpose behind all
  348. 10:19this this is the goal to help you make
  349. 10:21more complex more interesting better
  350. 10:22games and importantly actually finish
  351. 10:24them. So all of you I'm sure just like
  352. 10:26me just like everybody else you have a
  353. 10:28mountain of unfinished prototypes and a
  354. 10:30lot of the reasons why those prototypes
  355. 10:31end up being unfinished is because
  356. 10:33you're trying to build it but it gets
  357. 10:34more and more complex. it becomes harder
  358. 10:36and harder to add things and eventually
  359. 10:37the whole thing comes crumbling down. So
  360. 10:40that is why clean code is important
  361. 10:42because not only it makes things easier
  362. 10:44to work with but it helps you actually
  363. 10:45finish those so you can actually end up
  364. 10:47with a bunch more finished project as
  365. 10:49opposed to just a ton of prototypes.
  366. 10:52Now it is important to know what is
  367. 10:53clean code and also what is not clean
  368. 10:55code. So on what is clean code? So clean
  369. 10:58code is all about minimizing complexity
  370. 10:59and mental load. So as you're working on
  371. 11:01some kind of codebase, you don't have to
  372. 11:03keep in mind a massive amount of code.
  373. 11:05You can just think about the one thing
  374. 11:06that you are working on and that's it.
  375. 11:08And if you write your code properly, if
  376. 11:10you organize things properly, if so, you
  377. 11:12can do that. Whilst working on one piece
  378. 11:13of code, you just focus on that. So it's
  379. 11:15really all about trying to minimize
  380. 11:16complexity as much as possible and
  381. 11:18trying to minimize the mental load that
  382. 11:20you have to keep in your head. Clinko is
  383. 11:22also about reducing future debugging
  384. 11:24pain. So it's all about trying to be
  385. 11:26kind to your future self because bugs
  386. 11:28will guarantee to happen that that
  387. 11:30there's no way around that. Bugs will
  388. 11:32happen but if you write your code
  389. 11:33properly the debugging process will be
  390. 11:35much much easier as opposed to if you
  391. 11:36write things quick and dirty debugging
  392. 11:38process will be a nightmare. So yep
  393. 11:40clean code is all about reducing that
  394. 11:42future debugging pain.
  395. 11:44Then clean code is also about making
  396. 11:46intent obvious. So again just by reading
  397. 11:48it you should be able to easily
  398. 11:49understand okay what exactly this is
  399. 11:50doing. You shouldn't have to think long
  400. 11:52and hard. Okay trying to understand what
  401. 11:53is this function doing? What does this
  402. 11:54class represent? No, just by reading it,
  403. 11:56you should be able to understand. So the
  404. 11:58intent should be very, very obvious.
  405. 12:01Clink [snorts] code is also about making
  406. 12:02the project easier to expand. So again,
  407. 12:04if you're making a tiny prototype,
  408. 12:05doesn't really matter too much. But if
  409. 12:07you're trying to make a six to oneear
  410. 12:08game, if so, you're going to be
  411. 12:09expanding and adding more and more
  412. 12:11features onto it. And if you write quick
  413. 12:12and dirty code, then it's going to be
  414. 12:14very hard to do that. But if you write
  415. 12:15some proper clean code, then adding more
  416. 12:17and more features should be a breeze.
  417. 12:20And clean code is also about helping
  418. 12:21feature you understand PESU. So, in a
  419. 12:23little bit I have a slide all about
  420. 12:25this, how even if you're working just by
  421. 12:27yourself, it still makes sense to pay
  422. 12:29attention to the quality of your code
  423. 12:30because even if you're working by
  424. 12:31yourself, you're still always at least
  425. 12:33two people. There's present you and
  426. 12:35there's future you and those might as
  427. 12:37well be very different people. So,
  428. 12:38writing good clean code is basically
  429. 12:40making it easier on future you so that
  430. 12:42they can easily understand past you.
  431. 12:45Now, what is clean code not about? So,
  432. 12:47clean code is not about performance.
  433. 12:49Those are two very much separate topics.
  434. 12:51They can sometimes overlap sometimes but
  435. 12:53for the most part they are separate
  436. 12:55things. So you focus on clean code
  437. 12:57writing things properly and
  438. 12:58alternatively you can focus on
  439. 12:59performance making things fast and
  440. 13:01sometimes they do overlap but they are
  441. 13:03not related. So the whole thing about
  442. 13:05saying clean code is fast or slow that's
  443. 13:07very much basically comparing apples to
  444. 13:09oranges those are very much separate
  445. 13:11topics.
  446. 13:13Clean code is also not about impressing
  447. 13:15other programmers. So like I said this
  448. 13:17talk this is all very practical stuff
  449. 13:18that you should be able to use as soon
  450. 13:20as you finish watching this video. So it
  451. 13:22is not just arbitrary things just to
  452. 13:24make it good for some kind of teacher to
  453. 13:26impress someone. Nope. It's not it. It's
  454. 13:28very practical meant to help you as a
  455. 13:30programmer. Clean code is also not about
  456. 13:32following some arbitrary rules. So it's
  457. 13:34not about looking good on some kind of
  458. 13:36CV something like that. Nope. Like I
  459. 13:38said it's all very practical very much
  460. 13:40to help you write some awesome code.
  461. 13:42Clean code is also not about a specific
  462. 13:44book. Basically if you go on Google and
  463. 13:46you search for clean code you'll find
  464. 13:47there's one book literally with that
  465. 13:48title and there are some interesting
  466. 13:50ideas in that book and some not so
  467. 13:52interesting ones. So over here basically
  468. 13:53I am not talking about that book
  469. 13:55specifically. I'm talking about more of
  470. 13:56the highle concept of what exactly is
  471. 13:59link code which like I said already gave
  472. 14:01you my definition code that is easy to
  473. 14:03understand and easy to modify.
  474. 14:05And link code is also not unbreakable
  475. 14:08rules that you must follow. So
  476. 14:09everything that we're going to cover in
  477. 14:10this song, these are very much just
  478. 14:11guidelines and it's up to you and it's
  479. 14:13up to you to know when to apply these
  480. 14:15guidelines and when to not apply them.
  481. 14:16Again, keep in mind always at Northstar.
  482. 14:18It is always meant to help you. So these
  483. 14:20are things you always keep in mind, but
  484. 14:22sometimes breaking these guidelines
  485. 14:24might actually help. Now, here's the
  486. 14:26most common objection that I see
  487. 14:28whenever I try to encourage people to
  488. 14:29write higher quality code. They
  489. 14:30basically tell me, "So if the code
  490. 14:32works, why does any of this matter?" And
  491. 14:35it's an interesting argument, but the
  492. 14:37thing is this argument only works
  493. 14:38basically if you're making some very
  494. 14:40very tiny games. If you're making
  495. 14:41something like a Flappy Bird where an
  496. 14:43experienced developer kind of like
  497. 14:44myself can build in let's say like two
  498. 14:46hours, that's a very simple game. So
  499. 14:48with something that simple, even if you
  500. 14:49write some very terrible code, you are
  501. 14:51not really going to live in that
  502. 14:52codebase for any length of time. So it
  503. 14:55doesn't really matter the quality of the
  504. 14:56code. But as soon as you're trying to
  505. 14:58make something more complex, if you
  506. 14:59don't have a nice solid foundation, if
  507. 15:01so, then the whole thing is really just
  508. 15:02going to come crashing down. You have to
  509. 15:05write some proper clean code. You need
  510. 15:07some proper practices so you can
  511. 15:08actually build something more
  512. 15:09interesting on top of it. And that is
  513. 15:11basically what clean code does. If you
  514. 15:12focus on clean code, then your
  515. 15:14foundation will be very solid and you
  516. 15:16can easily add things on top and things
  517. 15:17will still stay solid. So essentially,
  518. 15:20sure your code works right now, but
  519. 15:22won't still work after you add 20 new
  520. 15:23features. If you write some very dirty
  521. 15:25code, the more things you write, the
  522. 15:27more the whole thing is going to come
  523. 15:28crashing down. But if you have a proper
  524. 15:30solid foundation, you will be able to
  525. 15:31keep adding things on top of it and it
  526. 15:33will still work. Messy code is just
  527. 15:35going to increase your debugging pain.
  528. 15:37Whereas clean code, again, that won't
  529. 15:38actually help you take your game to the
  530. 15:40finish line. Now, here's another common
  531. 15:43objection that I see when I talk about
  532. 15:44this. Someone saying, "I work alone. No
  533. 15:46one else needs to understand my code but
  534. 15:48me and I know what I wrote." So,
  535. 15:50sometimes people use this to not worry
  536. 15:51about naming rules, those sort of
  537. 15:52things. But like I was saying a while
  538. 15:54ago, you are never really working by
  539. 15:56yourself. Even if you are totally by
  540. 15:57yourself, even if so, you are always at
  541. 15:59least two people. That is you from the
  542. 16:02present who has all the context from all
  543. 16:03the code that you are writing. But then
  544. 16:05there's you in the future and that one
  545. 16:07does not have all that context. So you
  546. 16:09of the present and you of the future
  547. 16:10might as well be two completely
  548. 16:12different people. In the future, you
  549. 16:14will not have all the content you have
  550. 16:15right now. So things that make sense
  551. 16:17right now, they will not make sense in
  552. 16:18the future. Like let's say for example
  553. 16:20you're making some kind of code and you
  554. 16:22define some kind of function and you
  555. 16:23decide to name it DST just because for
  556. 16:25some reason for you right now that name
  557. 16:27DST seems to make sense. So you write it
  558. 16:29like that but then you come back to it
  559. 16:31one month later and you no longer have
  560. 16:32all that context and now you are left
  561. 16:34wondering what exactly did I mean by
  562. 16:36DSC? Was it distance destination some
  563. 16:39kind of distribution something there is
  564. 16:40no way of knowing because you named it
  565. 16:42DSC you named it something that made
  566. 16:44sense at the time but now in the future
  567. 16:46without all that context now it does not
  568. 16:48make any sense. So even if you are
  569. 16:49working technically by yourself it is
  570. 16:51never really just one person it is
  571. 16:52always multiple developers working
  572. 16:54across time. So essentially focus on
  573. 16:57writing clean code and do not make
  574. 16:58things harder on your future self for no
  575. 17:01reason. Again remember you spend a lot
  576. 17:03more time reading code than you do
  577. 17:04writing the code. So that should be your
  578. 17:06focus writing code that is going to be
  579. 17:08easier to read when eventually you do
  580. 17:10have to read them. Now on this topic
  581. 17:12there's a funny joke related to this
  582. 17:14which is a nice way to sort of have a a
  583. 17:16lens from which to see all the things
  584. 17:18we're talking about here which is
  585. 17:20basically write code as if the person
  586. 17:21who will maintain it after you is a
  587. 17:23serial killer who knows where you live.
  588. 17:24So try to think it like that. Try to
  589. 17:26write code right now so that in the
  590. 17:28future that person who might be a serial
  591. 17:30killer, you don't want to make them
  592. 17:31angry. So write some very good code so
  593. 17:33that that person is happy with your code
  594. 17:35as opposed to being very unhappy and
  595. 17:37trying to do something very bad to you.
  596. 17:40Okay? So this is what we're going to
  597. 17:42learn how to go from the left to the
  598. 17:43right side. But like I said, remember
  599. 17:45how there's nuance to everything. These
  600. 17:47aren't just guidelines. These are not
  601. 17:48unbreakable rules. So as I start giving
  602. 17:51you some very practical advice, always
  603. 17:52keep this in mind. I encourage you to
  604. 17:54follow all these guidelines. Especially
  605. 17:55if you are a beginner, you should opt
  606. 17:57for following them more than for
  607. 17:59breaking them. But sometimes not
  608. 18:00following these guidelines might
  609. 18:01actually lead to better code. So that
  610. 18:03should always be your guiding star.
  611. 18:04Always thinking, does this make the code
  612. 18:06easier to understand and easier to
  613. 18:07modify? If so, go ahead and apply the
  614. 18:09guideline. But as always, these are not
  615. 18:11unbreakable rules. Sometimes it is
  616. 18:13better to break them. Okay, starting off
  617. 18:15with like I said the most important
  618. 18:16thing. If you take just one thing away
  619. 18:18from this talk, I hope you take this. If
  620. 18:20you just focus on this, your code
  621. 18:21quality will massively improve. naming
  622. 18:23rules. These by themselves they are the
  623. 18:25easiest clean code improvement you can
  624. 18:26make. It is very easy and will massively
  625. 18:28increase the quality of your code. And
  626. 18:30the reason is because naming naming is
  627. 18:32actually architecture. It is not just a
  628. 18:34cosmetic thing. When you give a variable
  629. 18:36or a function some kind of name, it is
  630. 18:38not just a visual thing. No, it is
  631. 18:39something very core to your entire
  632. 18:41codebase. Very core to how the code
  633. 18:43actually works. Naming is very much a
  634. 18:45crucial part of your entire codebase. So
  635. 18:47you need to focus on writing good names
  636. 18:49so you have a nice solid foundation upon
  637. 18:51to which [snorts] good names they
  638. 18:53essentially make it clear what the
  639. 18:54variable or class represents or what the
  640. 18:56function does. That's it very very
  641. 18:58simple but if you just follow this the
  642. 19:00code quality will go up by quite a bit.
  643. 19:02So for example here's a bad example. So
  644. 19:05let's say we got some kind of variable
  645. 19:06float s. Now what exactly does that
  646. 19:08represent? Just by looking at it you
  647. 19:10have no idea. Is it some kind of speed,
  648. 19:12movement speed, rotation speed? Is it
  649. 19:14some kind of shield amount? Something
  650. 19:16like that. I don't know. What about D?
  651. 19:18Is it some kind of distance? Some kind
  652. 19:20of destination? No way of knowing. What
  653. 19:22about a function named do stuff? What is
  654. 19:23that doing? Literally, you cannot tell
  655. 19:25because that is the most generic name
  656. 19:27ever. Do stuff. Stuff can be literally
  657. 19:29anything. So, just by looking at the
  658. 19:30name of the function, you cannot tell
  659. 19:32what it is doing. So, if you use these
  660. 19:34kinds of names, you have to waste a ton
  661. 19:36of mental energy trying to understand
  662. 19:38what does this represent? What is this
  663. 19:39thing doing? Whereas if you use some
  664. 19:42proper names then you can just see oh
  665. 19:43okay so that's movement speed that's the
  666. 19:45reach start distance that function is
  667. 19:47updating the health bar. So if you write
  668. 19:49some proper names then just by
  669. 19:50themselves the code will instantly be
  670. 19:52easier to understand. You can just read
  671. 19:54and you instantly know what does this
  672. 19:56represent? What is this doing? There's
  673. 19:58basically no guessing required like this
  674. 20:00things are much much easier to follow.
  675. 20:03So on this here's some very specific
  676. 20:05advice. So first of all be descriptive
  677. 20:07be verbose. So I very much encourage you
  678. 20:10if a function or if a field is meant to
  679. 20:12represent the health amount then name it
  680. 20:14health amount. That's it. Use whatever
  681. 20:17is the most descriptive name you can
  682. 20:18come up with that represents that thing
  683. 20:20or what that function does. Try to come
  684. 20:22up with the best name possible. And like
  685. 20:24I said be verbose. So if the absolute
  686. 20:26best name that you can come up with is
  687. 20:28let's say 20 characters long. If so then
  688. 20:30just use that name. Don't try to shorten
  689. 20:32it. Don't try to like eliminate vowels
  690. 20:34or come up with abbreviations. No, don't
  691. 20:36do that. There's basically no limitation
  692. 20:38to the memory size for your variable
  693. 20:40names. So just use whatever variable
  694. 20:42names make most sense. Doesn't matter if
  695. 20:44it's 20 characters. If that's the best
  696. 20:46name you can come up with, then go ahead
  697. 20:47and use that name. Just make sure it is
  698. 20:49very descriptive. Just by reading the
  699. 20:51name, you should be able to understand
  700. 20:52what does this represent or what is this
  701. 20:54doing. Also avoid abbreviations and
  702. 20:57acronyms. Again, goes back to the same
  703. 20:59thing how present you and future you are
  704. 21:01different people. So some kind of
  705. 21:03abbreviation, some kind of acronym might
  706. 21:05make sense to you right now, but one
  707. 21:06month from now, chances are it won't
  708. 21:08make sense and you will have no idea
  709. 21:09what does that acronym mean. So try to
  710. 21:11avoid those because it is very, very
  711. 21:13easy to forget what do they mean. Also,
  712. 21:16don't try to be clever. I know some
  713. 21:18people, especially some beginners, they
  714. 21:19find it fun how you can name it
  715. 21:21literally anything. So they start naming
  716. 21:23it like inside jokes or sometimes using
  717. 21:25some curse words. And sure, again,
  718. 21:27that's something that [snorts] might be
  719. 21:28fun to you right now. It's quite funny,
  720. 21:31but one month from now will not be as
  721. 21:33funny because you won't understand.
  722. 21:34Okay, I named this some kind of joke.
  723. 21:36But what does this mean? I don't know
  724. 21:38what it means. So don't be clever.
  725. 21:40[clears throat]
  726. 21:41Also, do not use single letter variable
  727. 21:43names. So basically names like we saw
  728. 21:45like ds. What exactly does that mean?
  729. 21:47There's no way of knowing. There's not
  730. 21:48enough information in the variable to
  731. 21:50represent what exactly it's
  732. 21:52representing. So don't use those. The
  733. 21:54obvious exception of course are for loop
  734. 21:56iterators. So if you're making some kind
  735. 21:57of for loop sure using I, J, K, L, all
  736. 22:00of those usually those are easy to
  737. 22:02understand. So for that exception, you
  738. 22:04can use those. But everywhere else, do
  739. 22:06not use single letter variables.
  740. 22:10Then also remember how you don't get
  741. 22:11bonus points for comeback code. Like I
  742. 22:13was saying, there's really no
  743. 22:14limitations to memory nowadays.
  744. 22:15Computers are insanely good. Maybe if
  745. 22:17you were back in the 80s, you had to
  746. 22:19actually limit what name you gave a
  747. 22:20variable because that actually mattered.
  748. 22:22But nowadays, memory as far as it's
  749. 22:24concerned with regards to naming rules,
  750. 22:26regards to size of names, it's basically
  751. 22:28infinite. So, like I was saying, if the
  752. 22:30best name you can come up with is 20
  753. 22:32characters long, then do it. Don't try
  754. 22:34to shorten it just for the sake of
  755. 22:35compact code because you really don't
  756. 22:37get bonus points. But you do get bonus
  757. 22:39points for code that another developer,
  758. 22:41including yourself, can read instantly.
  759. 22:43So that should very much be your focus
  760. 22:45trying to come up with names that
  761. 22:46someone without any context of the
  762. 22:48codebase can just look at it and see oh
  763. 22:50okay I know exactly what this is doing.
  764. 22:53So these are the practical rules but
  765. 22:55also very important to remember how
  766. 22:57names are very much not set in stone.
  767. 22:59You can easily rename things. So you
  768. 23:01should be constantly trying to think
  769. 23:02okay what is the best name that I can
  770. 23:04come up with in this scenario. But if in
  771. 23:06the future you come up with a better
  772. 23:07name. If so just take a few seconds just
  773. 23:09rename it. Nowadays ids are very good.
  774. 23:12Here's an example in Visual Studio.
  775. 23:13Visual Studio you just press CtrlR and
  776. 23:16you can easily rename things and it will
  777. 23:17automatically rename every single
  778. 23:19instance of that symbol. So there's
  779. 23:21really no reason not to rename things.
  780. 23:23So when you define some kind of
  781. 23:24variable, some kind of function, think
  782. 23:25of what is the best name possible. But
  783. 23:27then in the future, always keep trying
  784. 23:29to think, okay, is there a better name
  785. 23:30for this thing? If so, spend two seconds
  786. 23:33rename it and give it the better name.
  787. 23:36Then in terms of how you actually write
  788. 23:38your code, the code style, that one is
  789. 23:40also something that is quite important.
  790. 23:42But the most important thing is really
  791. 23:43just this consistency. You can basically
  792. 23:45pick whatever code style you want. Go
  793. 23:46with uppercase, lowerase, Pascal, camel
  794. 23:49case, none of that matters. Those are
  795. 23:50really just visual things. So the
  796. 23:52specific code sound that you follow.
  797. 23:54That one does not matter. What matters
  798. 23:55is this. You need to be consistent. So
  799. 23:57define some kind of code sound that you
  800. 23:59want to follow and then really just
  801. 24:00follow it 100% of the time. You don't
  802. 24:02want to have one file following this
  803. 24:04specific type of code cell and different
  804. 24:06file following a completely different
  805. 24:07code style. No, you want things to
  806. 24:09match. You want to be able to open any
  807. 24:11kind of file in your codebase and be
  808. 24:13able to easily understand because they
  809. 24:15all follow the same code style. As an
  810. 24:16example, here is my own personal code.
  811. 24:19What I've developed over my 20 years of
  812. 24:21writing code. So for my classes, I
  813. 24:23define them in Pascal case. For
  814. 24:25constants, uppercase snake case. For my
  815. 24:28properties and events, PESL case. Then
  816. 24:30for fields, I've got cheml case. for
  817. 24:32parameters I've got chem as well. So,
  818. 24:34yep, this is the code sound that I
  819. 24:35follow and I can basically open up any
  820. 24:37script from the past 10 years that I've
  821. 24:38written and it will be following this
  822. 24:40code sound. So, I can very easily
  823. 24:41understand, I can very easily read it.
  824. 24:43But like I said, there's nothing special
  825. 24:45about this specific code sound. This is
  826. 24:47just the one that I came up with myself.
  827. 24:49But that doesn't mean that you should
  828. 24:50use this sound. Like I said, most
  829. 24:52important thing is consistency. So, come
  830. 24:54up with whatever code sound makes most
  831. 24:55sense to you and really just make sure
  832. 24:57you follow it 100% of the time.
  833. 25:00Next on the topic of decoupling and how
  834. 25:02it is a superpower. So decoupling is
  835. 25:05really awesome and decoupling basically
  836. 25:08just means keeping classes as separated
  837. 25:10as possible and doing so it reduces
  838. 25:11mental load. So if you have like two
  839. 25:13classes you don't want them to be
  840. 25:15constantly talking to one another. If so
  841. 25:16they are going to be tightly coupled.
  842. 25:18Instead you want to separate them as
  843. 25:19much as possible. If two classes can
  844. 25:21work independently, you should separate
  845. 25:23them as much as possible. Because if you
  846. 25:25make everything coupled, if everything
  847. 25:27talks to another, so you've got some
  848. 25:28kind of giant class that talks to the AI
  849. 25:30system, to the physics engine, the
  850. 25:31rendering pipeline, the animation,
  851. 25:32whatever it is. If so, then as you're
  852. 25:34working on that one class, you're
  853. 25:36constantly having to keep in your head,
  854. 25:37okay, how is this change that I'm doing
  855. 25:39to this class, how is this affecting all
  856. 25:41of these other different classes? You
  857. 25:43have to keep all of that massive amount
  858. 25:45of information in your brain as you're
  859. 25:46working on just one specific class. So,
  860. 25:48this is quite terrible. this going to
  861. 25:50cause you quite a lot of mental load
  862. 25:52whereas your goal should be to reduce
  863. 25:54mental load. So in turn you should keep
  864. 25:56things as decoupled as possible. If you
  865. 25:58want if you can separate the animation
  866. 26:00system from let's say the physicism
  867. 26:01entirely. That way if you're working on
  868. 26:03the animation that's it. You just work
  869. 26:04on that one. You don't have to worry
  870. 26:06about the physicism because you know for
  871. 26:08a fact they are completely decoupled
  872. 26:10from one another. Or if you're working
  873. 26:12on some kind of player movement let's
  874. 26:14say your entire code base is like a
  875. 26:15thousand classes long. But if the player
  876. 26:17movement is 100% decoupled from
  877. 26:18everything else, then it doesn't really
  878. 26:20matter. Your codebase could be 100
  879. 26:21classes long, a thousand classes long,
  880. 26:23it doesn't matter because if that one is
  881. 26:24decoupled whilst you're working on that
  882. 26:26one, you know for a fact it is not
  883. 26:27touching a dozen other classes whilst
  884. 26:29you're working on that. You just focus
  885. 26:31on that and that's it. So the mental
  886. 26:32load you have to keep inside your head
  887. 26:34is very very small which helps you out
  888. 26:36quite a bit. And if you want a very
  889. 26:39practical example on how to decouple
  890. 26:40things, just this decoupling action is
  891. 26:43just to separate the logic from the
  892. 26:44visuals. So let's say you've got some
  893. 26:46kind of player movement script and that
  894. 26:47one should just handle movement. So that
  895. 26:49one should take some kind of transform
  896. 26:50and just move it around and then you
  897. 26:51have a separate some kind of player
  898. 26:53visual script and that basically just
  899. 26:54listens to the logic on movement and
  900. 26:56then calls some kind of animation. So
  901. 26:58the logic script that one does not know
  902. 27:00anything about any kind of visual, any
  903. 27:01kind of animation, any kind of mesh,
  904. 27:03nope, does not know any of that. It just
  905. 27:05knows it has a position and just moves
  906. 27:07that kind of position. And then you've
  907. 27:08got the visual script. that one just
  908. 27:10listens to what is changing on the logic
  909. 27:11script and that is the one that is
  910. 27:12actually playing sound effects, playing
  911. 27:14animations, those kinds of things. So
  912. 27:16try to keep those separated the logic
  913. 27:18and visuals try to keep those as
  914. 27:20separated as possible and that alone
  915. 27:21will help the increase the code quality
  916. 27:23by quite a bit. Now in coupling just one
  917. 27:26last thing which is you always have to
  918. 27:28have some coupling. So for example in
  919. 27:30this scenario the logic is completely
  920. 27:31decoupled from the visuals but the
  921. 27:33visuals need to know what the logic is
  922. 27:34doing. So the visual is tightly coupled
  923. 27:36to the logic. So some coupling is always
  924. 27:38guaranteed to happen. You can't really
  925. 27:40make a game or any kind of software. If
  926. 27:42you have classes that do not talk to
  927. 27:43each other at all, no, they do have to
  928. 27:45talk to each other somewhat. So the
  929. 27:47entire purpose is trying to minimize as
  930. 27:48much as possible, trying to separate
  931. 27:50things as much as you can and doing so
  932. 27:52again reduce your mental load which
  933. 27:54makes your job as a programmer much much
  934. 27:56easier.
  935. 27:58Then on topic of the single
  936. 27:59responsibility principle, this one is
  937. 28:01really nice and really easy to
  938. 28:03understand. It is basically one class,
  939. 28:05one function should do one thing and one
  940. 28:06thing only. That is it. Super basic,
  941. 28:09super simple. But if you just follow
  942. 28:11this, your code quality will massively
  943. 28:12increase. And this one, some of you
  944. 28:15might know it is actually part of the
  945. 28:16solid acronym. So it's got all these
  946. 28:18various things. So if you want a little
  947. 28:20bit more homework, you can actually go
  948. 28:21and research all these other principles.
  949. 28:23They're also nice, but I think the most
  950. 28:25important one by far is the first one.
  951. 28:27So that is the one that we're going to
  952. 28:28be focusing on. And on that one, like I
  953. 28:31said, one class should do one thing and
  954. 28:32one thing only. So, here's a bad
  955. 28:34example. We've got some kind of player
  956. 28:36movement class. And sure, it is moving
  957. 28:37the player, but then it's also playing
  958. 28:39footstep sounds. It's also starting some
  959. 28:41kind of running animation and updating
  960. 28:43UI. That is not correct. It says that
  961. 28:45it's player movement, but it's moving
  962. 28:47the player and playing footstep sounds,
  963. 28:48animation, so on. It's doing multiple
  964. 28:50things at once. That is not correct. So,
  965. 28:52this one is violating the single
  966. 28:54responsibility principle. Instead, this
  967. 28:56should be nicely sped up. You should
  968. 28:57have different classes. So this one
  969. 28:59class just handles player movement. This
  970. 29:01one class just player sounds. This one
  971. 29:02class just player visuals. You should
  972. 29:04keep things separated. So each thing
  973. 29:06does one thing and one thing only. The
  974. 29:09same thing applies to functions as well.
  975. 29:11So here's an example. You've got some
  976. 29:12kind of function named move player and
  977. 29:14it is moving the player. But then it's
  978. 29:15also playing some kind of footstep
  979. 29:17sounds. That is not correct. You should
  980. 29:18not be doing this. You should not be
  981. 29:20doing both things. Both moving the
  982. 29:21player and playing footstep sounds.
  983. 29:23Nope. You should focus on one thing and
  984. 29:25one thing only. And actually in this
  985. 29:26specific case for functions you can
  986. 29:28really just remember the naming rules
  987. 29:30always remember. So try to come up with
  988. 29:33the best the most descriptive name you
  989. 29:35can come up with the name that actually
  990. 29:36represents what the function is doing
  991. 29:38which in this case this name move player
  992. 29:40that is not the correct name. It says
  993. 29:42move player but the function is clearly
  994. 29:43moving the player but then also playing
  995. 29:45footstep sounds. So the name is not
  996. 29:47correct. The better name for this
  997. 29:48function should be move player and play
  998. 29:50footstep sounds because that is what the
  999. 29:52function is doing which in itself that
  1000. 29:54name that is actually a very very useful
  1001. 29:57heristic. Basically if the best name you
  1002. 29:59can come up with includes the word and
  1003. 30:01if so you're probably doing too much.
  1004. 30:03Sure if you if the best name is move
  1005. 30:05player and play footstep sounds that is
  1006. 30:07a very nice guide. Okay, I should
  1007. 30:09probably split this up. There should be
  1008. 30:10one function to move the player and one
  1009. 30:12function to play sound effects. Those
  1010. 30:14should be their own thing. So, yep, if
  1011. 30:16the best name includes the word and,
  1012. 30:18there is a very clear guide, not always,
  1013. 30:20but most times that you probably should
  1014. 30:22split this up. But again, like I said,
  1015. 30:24just a quick reminder, remember
  1016. 30:26everything we're talking about here,
  1017. 30:27these are all guidelines. These are not
  1018. 30:28unbreakable rules. So, sometimes it
  1019. 30:31doesn't make sense. For example, for
  1020. 30:32this one, for the single responsibility,
  1021. 30:34sometimes you have a function that is
  1022. 30:35like 20 lines long, and technically it's
  1023. 30:37doing more than one thing. But if you
  1024. 30:38break that one function into five
  1025. 30:40functions that each are like two lines
  1026. 30:42long, if so, it might actually increase
  1027. 30:44complexity as opposed to minimizing it.
  1028. 30:46So that is why you should focus on that
  1029. 30:47guideline always every time. The go the
  1030. 30:50entire goal behind all of this is trying
  1031. 30:51to minimize complexity. And these
  1032. 30:53guidelines, they should help you achieve
  1033. 30:55that. But if there's some scenario where
  1034. 30:56they don't where they actually make the
  1035. 30:58code more complex, if so, then don't
  1036. 30:59follow them in that specific scenario.
  1037. 31:02Next on the topic of information hiding.
  1038. 31:05This is another interesting one which is
  1039. 31:07remember how clean code is all about
  1040. 31:08minimizing complexity and mental load
  1041. 31:10management. So you should try to
  1042. 31:12minimize how much you have to keep in
  1043. 31:13your head. So in this case let's look at
  1044. 31:15a bad example. Here we've got a bunch of
  1045. 31:18fields and all of them are marked as
  1046. 31:19public. Now public means that any class
  1047. 31:21anywhere in your entire codebase can
  1048. 31:23both read and write to it. This is
  1049. 31:25pretty terrible because if something bad
  1050. 31:27happens, you don't know what is going
  1051. 31:29on, where exactly that bug came from. So
  1052. 31:32if you have a codebase that is a
  1053. 31:33thousand class long, then in theory that
  1054. 31:35is a thousand class a thousand places
  1055. 31:38where anything could be modifying any of
  1056. 31:39those fields. So if there's some kind of
  1057. 31:41bug where the movement speed is set to
  1058. 31:43zero, how exactly do you know where that
  1059. 31:45came from? How do you debug it? It is
  1060. 31:47going to be very very difficult because
  1061. 31:48this because technically if it is
  1062. 31:50public, there's a thousand places that
  1063. 31:52could be modifying that. So this means
  1064. 31:54that as you're working on this codebase,
  1065. 31:56you're constantly having to think about
  1066. 31:57the entire codebase because technically
  1067. 31:59anything could be modifying that one
  1068. 32:01field.
  1069. 32:02So instead, you should default to make
  1070. 32:04things private. Private means that only
  1071. 32:06that specific class can access that
  1072. 32:08specific field. So if you've got some
  1073. 32:09kind of bug, you know for a fact it has
  1074. 32:11to exist in that one class. It cannot
  1075. 32:12exist outside of it because it is
  1076. 32:14physically impossible for something
  1077. 32:15outside to modify it. You should try to
  1078. 32:17limit access as much as possible and
  1079. 32:19basically only expose what you
  1080. 32:21absolutely need to. So in this case, if
  1081. 32:23it is private, it doesn't really matter
  1082. 32:25how many class you have in your entire
  1083. 32:26codebase. Could be 10, could be a
  1084. 32:28thousand, could be 10,000. Doesn't
  1085. 32:29really matter because if it is private,
  1086. 32:31you know for a fact, guaranteed if you
  1087. 32:33got some kind of bug on your movement
  1088. 32:34speed, it has to exist right here in
  1089. 32:36this one class. Now, just a quick Unity
  1090. 32:39tip, which is if you mark it as private,
  1091. 32:41it does not show up in the editor. So if
  1092. 32:42you do want to follow some good clean
  1093. 32:44code guidelines and you also want to
  1094. 32:46expose something in the editor, if so,
  1095. 32:48then just do that. Make it private and
  1096. 32:49add the serialized film attribute. That
  1097. 32:51way it will be private meaning other
  1098. 32:53classes cannot modify that field but
  1099. 32:56YouTube itself unity itself will be able
  1100. 32:58to showcase that field in the inspector
  1101. 33:00so you can easily edit it. Now like I
  1102. 33:03said limitex as much as possible and
  1103. 33:04only expose what you absolutely need to.
  1104. 33:06So if you do need to expose this let's
  1105. 33:08say you've got some kind of UI function
  1106. 33:09that needs to showcase the movement
  1107. 33:11speed that means UI does need to know
  1108. 33:13about it. You can't hide it from the UI
  1109. 33:15and if so really just make some simple
  1110. 33:17get functions. Now technically if you
  1111. 33:19want you can also use properties.
  1112. 33:20They're basically the same thing. Just
  1113. 33:21me personally, I prefer to use
  1114. 33:22functions. So just like this, if I've
  1115. 33:24got some kind of UI that needs to know
  1116. 33:26movement speed, I really just expose a
  1117. 33:27function get move speed and the UI
  1118. 33:29actually reads the move speed. Then if I
  1119. 33:31need something to actually set movement
  1120. 33:34speed, then I expose the function to do
  1121. 33:35that. So yep, just like this. This helps
  1122. 33:37out quite a bit. Now, of course, you
  1123. 33:39might be asking, okay, so we made them
  1124. 33:41private, so we don't have access. But
  1125. 33:43now, if we add these functions, don't we
  1126. 33:44end up with the same thing? And yes and
  1127. 33:46no. So first of all, like I said, only
  1128. 33:48expose things if you absolutely need
  1129. 33:50them to. So if you don't need anything
  1130. 33:52to get some kind of variable, then don't
  1131. 33:54expose it. Not at all. You should only
  1132. 33:56make these two functions. These are
  1133. 33:57optional. Only make them if you
  1134. 33:59absolutely need them. If something else
  1135. 34:00needs to get that function, needs to
  1136. 34:02actually read that value. If so, make
  1137. 34:04the get function. If something else
  1138. 34:05really needs to set it, if so, make a
  1139. 34:07set function. But only make these if you
  1140. 34:09need them. And even if you do need them,
  1141. 34:11if you do end up making these these,
  1142. 34:13this is still a better approach just
  1143. 34:14because with this it is possible to add
  1144. 34:16access controls, validation, logging,
  1145. 34:18events, those kinds of things. So you
  1146. 34:20can add some kind of function, some kind
  1147. 34:22of log over here when you set the
  1148. 34:23movement speed. So if you have some kind
  1149. 34:25of bug where the movement speed is set
  1150. 34:26to zero, at least you can follow the
  1151. 34:28log, you can follow the stack trace and
  1152. 34:29try to see where is that bug came from.
  1153. 34:32Whereas if you make a public field, you
  1154. 34:33really have no way of attaching extra
  1155. 34:35logic to that. If something modifies it,
  1156. 34:37that's it. So over here you can ask
  1157. 34:39validation X controls and a bunch more.
  1158. 34:41So yeah, even if you make these two
  1159. 34:43functions, these are still better than
  1160. 34:44making it public. But like I said, these
  1161. 34:46are optional. You should only expose
  1162. 34:48these if you absolutely need to. Again,
  1163. 34:51remember the entire purpose behind
  1164. 34:52everything we're talking about here is
  1165. 34:54just this. The entire goal is to
  1166. 34:55minimize how much code you have to keep
  1167. 34:57in your head as you're working. So if
  1168. 34:58you try to minimize something, if you
  1169. 35:00make something private, then you know
  1170. 35:01for a fact if there's a bug in there, it
  1171. 35:03has to exist in this one class. It
  1172. 35:05cannot exist outside of it. So as you're
  1173. 35:07working on this one thing as you're
  1174. 35:08solving this kind of bug, you know for a
  1175. 35:10fact you don't have to think about every
  1176. 35:11other class that exists because only
  1177. 35:13this one can actually modify.
  1178. 35:15Then another very important topic on the
  1179. 35:17one source of truth. This is one of the
  1180. 35:19best ways to eliminate bugs. So one
  1181. 35:22source of truth is basically how your
  1182. 35:23data should have one single source of
  1183. 35:25truth as opposed to existing in multiple
  1184. 35:27places all over the place. So here's a
  1185. 35:30bad example. So let's say we got some
  1186. 35:32kind of player health function that has
  1187. 35:33a current health field. But then the
  1188. 35:35health bar, that one also stores a
  1189. 35:36current health. The safe system also has
  1190. 35:38it. The animator has some kind of is
  1191. 35:40that parameter and the UI is saying
  1192. 35:41dead. So you've got all of this data
  1193. 35:43copied on multiple different places. And
  1194. 35:45if you have this, so who exactly are you
  1195. 35:47going to ask to know if the player's
  1196. 35:49dead? All of them have that kind of
  1197. 35:51data. So which one do you ask? There's
  1198. 35:52no way of knowing. And importantly, what
  1199. 35:54if you have this? What if data goes out
  1200. 35:56of sync? What if the player health says
  1201. 35:58the current health is zero, but the end
  1202. 36:00meta says is dead is set to false. If
  1203. 36:02this is the case, is the player actually
  1204. 36:04dead or alive? And the answer is there's
  1205. 36:06no answer. You can't really know because
  1206. 36:07you have multiple data on multiple
  1207. 36:10places that are all meant to represent
  1208. 36:11the same thing. But if they go out of
  1209. 36:13sync, you don't know because you don't
  1210. 36:14have one source of truth. So this whole
  1211. 36:16thing is a debugging nightmare. If you
  1212. 36:18have some kind of bug, you never know
  1213. 36:19where the bug is coming from because
  1214. 36:21you've got multiple data all over the
  1215. 36:22place. So instead, you should do
  1216. 36:25something like this. You've got the
  1217. 36:26player health. That is the one that
  1218. 36:27actually stores the current health. That
  1219. 36:29is the one source of truth. That is the
  1220. 36:30only one that matters. And everywhere
  1221. 36:32else like the health smart safe system,
  1222. 36:34those just access the player health and
  1223. 36:35they actually read the current health.
  1224. 36:37So they don't store any data themselves.
  1225. 36:39They really just read the actual source
  1226. 36:41of truth, the actual data that actually
  1227. 36:43makes sense. That way if you got some
  1228. 36:44kind of bug on the current house, if so,
  1229. 36:46you know for a fact it has to exist in
  1230. 36:48this one place because everywhere else,
  1231. 36:50all these other ones are just reading
  1232. 36:51from that one. So if there's a bug, it
  1233. 36:53has to be on that one. So, yep. This
  1234. 36:55right here makes it super easy to debug.
  1235. 36:58Then on a potentially slightly more
  1236. 37:00controversial topic here, we have
  1237. 37:02self-documented code. So, basically all
  1238. 37:04about comments. Should you use them?
  1239. 37:05Should you not use them? Essentially,
  1240. 37:07comments, they should be rare and it
  1241. 37:09should explain the why and not the what.
  1242. 37:11For the most part, the goal should be
  1243. 37:13the intent should be clear in the code
  1244. 37:15itself. So, just by reading the code,
  1245. 37:17you should be able to understand what
  1246. 37:18this code is doing, what this variable
  1247. 37:20represents. Just by name itself, it
  1248. 37:22should be understandable. You don't want
  1249. 37:24to use comments trying to explain
  1250. 37:25something. Here's a bad example. So
  1251. 37:27don't write some useless comments. So
  1252. 37:30right here you've got some kind of code
  1253. 37:31adding score by 10 and then you add in
  1254. 37:33the comment at score by 10. This is
  1255. 37:35completely useless. You can understand
  1256. 37:37that just by reading the code itself. So
  1257. 37:39this comment right here is just visual
  1258. 37:41noise. It is just occupying your brain
  1259. 37:42for no benefit whatsoever. So do not
  1260. 37:45write these kinds of comments.
  1261. 37:47Also don't write outdated incorrect
  1262. 37:50comments. So in this case, let's say we
  1263. 37:51started off adding 10 to the score, but
  1264. 37:53then we refactored it. We made it just
  1265. 37:55five, but we forgot to update the
  1266. 37:57comment. So now, not only is the comment
  1267. 37:59adding visual noise, it is actually just
  1268. 38:00an outright lie. And if you believe the
  1269. 38:02comment, it is going to take you in the
  1270. 38:04wrong direction. So in this case, it's a
  1271. 38:06very bad use case for comments. Remember
  1272. 38:09the actual code is the one source of
  1273. 38:10truth that is one that actually matters.
  1274. 38:13Also, do not use comments to explain
  1275. 38:15some bad names. For example, you name
  1276. 38:17some kind of variable D and you are
  1277. 38:19trying to explain, okay, D is a distance
  1278. 38:21something. Again, this is a terrible use
  1279. 38:23case. So in this really remember the
  1280. 38:25naming rules. So in [snorts] this case,
  1281. 38:27instead of a comment explaining it,
  1282. 38:28really just give it a better name. Pick
  1283. 38:30up distance. That is it. You just read
  1284. 38:32the name and it is easily
  1285. 38:33understandable. No need to write a
  1286. 38:35useless comment.
  1287. 38:37Also, not use comments to explain some
  1288. 38:39complex confusing logic. So over here
  1289. 38:41we've got some complex conditions.
  1290. 38:43checking for distance, checking if a
  1291. 38:45keys press and some kind of boolean and
  1292. 38:47we using a comment to try to explain
  1293. 38:48that. Don't do that over here. Instead
  1294. 38:50of using a comment, really just put all
  1295. 38:52that logic in a separate function and
  1296. 38:54then give that function a very good name
  1297. 38:56and that way it is easy to understand.
  1298. 38:58You can just see, oh, I'm asking can I
  1299. 39:00pick up this item and if so, I do pick
  1300. 39:02up the item. That way, no comments are
  1301. 39:04necessary. You really just read it and
  1302. 39:06that's it.
  1303. 39:08Then in terms of dos, do use comments
  1304. 39:10that explain design decisions. So, for
  1305. 39:12example, let's say we're working on some
  1306. 39:13kind of game and most levels in the game
  1307. 39:15you do use path finding. But then, let's
  1308. 39:17say you've got some specific game, some
  1309. 39:19specific level where it doesn't actually
  1310. 39:21have any obstacles and you intentionally
  1311. 39:23don't want to use path finding in that
  1312. 39:24level. If so, then that's actually a
  1313. 39:26great use case for a comment trying to
  1314. 39:27explain why you made that decision
  1315. 39:29because again, try to think so one month
  1316. 39:31from now, you come back to this codebase
  1317. 39:32and you'll look at this code and you
  1318. 39:34might think, oh, this is wrong. I should
  1319. 39:36be using pathfinding. Why am I not using
  1320. 39:38pathfinding? So to avoid that confusion,
  1321. 39:40it does make sense to write a comment
  1322. 39:42that actually explains why you made that
  1323. 39:44one specific design decisions.
  1324. 39:46Also, do you use comments to explain
  1325. 39:48some potential bugs? Let's say you're
  1326. 39:50working on some kind of camera system
  1327. 39:51and you want to make sure that the
  1328. 39:53camera actually updates after the
  1329. 39:54player, otherwise it causes some kind of
  1330. 39:56bug. So that is another use case where
  1331. 39:58it does make sense to write the comment
  1332. 40:00because if you don't have that comment
  1333. 40:01again, future is going to come back to
  1334. 40:03this and is going to see the late update
  1335. 40:04and might think, "Oh, why did I put this
  1336. 40:06on late update? that seems to be a
  1337. 40:08mistake. Let me replace this. Put this
  1338. 40:10on update and doing so you would cause
  1339. 40:11that bug. So to avoid that, go ahead
  1340. 40:14explain a comment trying to explain why
  1341. 40:16exactly use this specific thing and how
  1342. 40:18it was a very conscious choice and not
  1343. 40:20by accident. Also do use comments for
  1344. 40:23class overview or documentation. So any
  1345. 40:25kind of thing where you want to explain
  1346. 40:26it a little bit more. Again like I said
  1347. 40:28the name should be as readable as
  1348. 40:30understandable as possible. But
  1349. 40:32sometimes class can represent some more
  1350. 40:34let's say ethereal concepts and if so
  1351. 40:36using a comment can help with that. And
  1352. 40:38of course for documentation that also
  1353. 40:40makes sense. [snorts] For the most part
  1354. 40:43bad comments they explain code that
  1355. 40:44really should just have been clearer.
  1356. 40:46Whereas good comments they explain
  1357. 40:47context that the code cannot express by
  1358. 40:49itself. For the most part the code
  1359. 40:51should be the thing that you should
  1360. 40:52focus on and comments should just only
  1361. 40:54be used for things that the code cannot
  1362. 40:56express. So the rule is not never write
  1363. 40:58comments. No, instead you should focus
  1364. 41:00on making the code self-documenting and
  1365. 41:02only use comments so add context that
  1366. 41:03the code cannot express clearly. But
  1367. 41:05that should be your main focus. Trying
  1368. 41:07to make sure that just code itself
  1369. 41:08explains everything. And in most cases,
  1370. 41:10in 90% cases, you can do that. Just
  1371. 41:12reading the code should be
  1372. 41:13understandable. Comment should only add
  1373. 41:15context that the code cannot express by
  1374. 41:17itself.
  1375. 41:19Then on the topic of magic numbers,
  1376. 41:21which is a very fun name for something
  1377. 41:23very simple, which is simply numbers
  1378. 41:25where their meaning is unclear. So over
  1379. 41:27here we've got some kind of code moving
  1380. 41:29some kind of transform. It's moving by a
  1381. 41:31move direction and then multiplied by
  1382. 41:33five and 1.2. What exactly do these
  1383. 41:35numbers represent? And the answer is
  1384. 41:37there is no way of knowing. Just looking
  1385. 41:38at this, you can't quite tell what is a
  1386. 41:40five, what is a 1.2? Can I change them?
  1387. 41:42Can I remove them? I have no idea. So
  1388. 41:45these are numbers where their meaning is
  1389. 41:46unclear. So you should not be using
  1390. 41:48these. Instead, use named variables.
  1391. 41:51That way you can easily name them. Okay,
  1392. 41:53so that's movement speed. That's a
  1393. 41:54sprint multiplier. That way things are
  1394. 41:56much easier to understand. I can just
  1395. 41:58read the code and I understand what it's
  1396. 41:59doing as opposed to up there where I
  1397. 42:01have no idea what those numbers
  1398. 42:03represent.
  1399. 42:05These are also numbers where basically
  1400. 42:07you can see kind of like this on some
  1401. 42:09kind of if you're testing if it's under
  1402. 42:103F. What is 3F? Again, no way of
  1403. 42:13knowing. So don't use magic numbers.
  1404. 42:15Instead, give it a proper variable and
  1405. 42:17it would make sense. Oh, okay. So if I'm
  1406. 42:18closer than the attack distance, I'm
  1407. 42:20going to attack the player. Or another
  1408. 42:22example for function parameters. So
  1409. 42:24you've got some kind of function and has
  1410. 42:26multiple parameters technically you can
  1411. 42:28use intellisense in order to see okay
  1412. 42:29what is the second parameter what is the
  1413. 42:31third one what are those meant to
  1414. 42:32represent so in this case technically if
  1415. 42:34you want you can rely on intellisense
  1416. 42:36but again trying to minimize your mental
  1417. 42:38load really just use some local
  1418. 42:40variables that we can define okay that's
  1419. 42:42a minimum zoom maximum zoom so just by
  1420. 42:44reading it is much easier remember the
  1421. 42:47entire purpose the entire goal behind
  1422. 42:48all this is really making it easier to
  1423. 42:51understand so you don't have to waste
  1424. 42:52mental energy so any kind thing that is
  1425. 42:54hidden meaning. Make sure you use some
  1426. 42:56kind of variable where you can give it a
  1427. 42:58proper name and just by reading it you
  1428. 42:59understand what it is doing. Next, do
  1429. 43:02not use strings as identifiers. So the
  1430. 43:05reason is because it is very easy to
  1431. 43:07make mistakes and the compiler simply
  1432. 43:09cannot save you. So here's a quick
  1433. 43:11example. Look at all these strings. Can
  1434. 43:13you tell what exactly they are? How
  1435. 43:14exactly they are different? And at a
  1436. 43:16glance you really can't because they are
  1437. 43:18very very similar. But one of them is
  1438. 43:20player 10. But then that one instead of
  1439. 43:22a zero, that one actually has an
  1440. 43:23uppercase O. This one over here instead
  1441. 43:25of a one actually has a lowercase L. So
  1442. 43:28visually they look very similar. But the
  1443. 43:29reality is all of these are completely
  1444. 43:31separate. All these are completely
  1445. 43:33different things. And importantly also
  1446. 43:35the compiler cannot save you because all
  1447. 43:36of these these are all valid. This is
  1448. 43:38all valid C# code. Even though they are
  1449. 43:40all different, this is all 100% valid.
  1450. 43:43So if you use strings, the compiler
  1451. 43:45cannot help you. you are only going to
  1452. 43:46find those errors during actual runtime
  1453. 43:49and not whilst you're actually writing
  1454. 43:50the code. Whereas you should always
  1455. 43:53prefer using some nice strong types
  1456. 43:55because if you do if you name some kind
  1457. 43:57of variable player 10 but then replace
  1458. 43:59the zero with an O then the compiler
  1459. 44:01won't tell you okay this looks similar
  1460. 44:02but very clearly you wrote this a
  1461. 44:04different way. Did you mean to do that?
  1462. 44:06No, probably not. So if you use some
  1463. 44:07strong types the compiler can help you
  1464. 44:09even though visually it still looks
  1465. 44:11quite different.
  1466. 44:13Now on this here's a very real example
  1467. 44:14that happened to me. So a few years ago
  1468. 44:16like four years ago I was making a
  1469. 44:18spaceship construction system something
  1470. 44:20that is quite interesting and over there
  1471. 44:21I was basically on a live stream
  1472. 44:23building something and I was doing the
  1473. 44:24same thing that I always do using
  1474. 44:26transform.find in order to find some
  1475. 44:28kind of chomp objects. Back then I
  1476. 44:29already knew not to use strings as
  1477. 44:31identifiers and this was basically my
  1478. 44:32one and only exception. I just used find
  1479. 44:35in order to find the chomp objects. So
  1480. 44:36over here I wrote some similar code. So
  1481. 44:38transform.find find the S button and
  1482. 44:40then trying to get the S button making
  1483. 44:42sure okay that is the actual name for
  1484. 44:44the game object. So looking at that at a
  1485. 44:46glance, it seems correct. Everything
  1486. 44:47seems to be very much like it should
  1487. 44:49work. But of course, once I started
  1488. 44:51running the code, there go a dreaded no
  1489. 44:53reference exception. Everything breaks.
  1490. 44:55And yep, this was quite tricky. So then
  1491. 44:57I went and I double checked. Okay, is
  1492. 45:00the code actually saying S button? So I
  1493. 45:02I erased it. I wrote it again. Yes, it
  1494. 45:05is named S button. What about the
  1495. 45:06object? Is it named S button? Yes, it is
  1496. 45:08clearly named S button. So looking at
  1497. 45:10this, it looks like it should work. Why
  1498. 45:12is this not working? Why is this
  1499. 45:13throwing a no reference exception? So it
  1500. 45:15drove me crazy for like 10 20 minutes
  1501. 45:17trying to understand okay the code is
  1502. 45:19correct why is this not throwing an
  1503. 45:20error and the answer is finally after a
  1504. 45:23while I discovered this. So that game
  1505. 45:25object looks like it is named S button
  1506. 45:27but it's actually S button with a space
  1507. 45:29and of course as far as strings are
  1508. 45:31concerned S button and S button space
  1509. 45:33those might as well be completely
  1510. 45:35different things those don't have
  1511. 45:36anything to do with one another. So very
  1512. 45:38correctly the code was saying okay
  1513. 45:40there's nothing named S button because
  1514. 45:41the only thing that exists is S button
  1515. 45:43with a space. So yep I went insane for
  1516. 45:45like 10 20 minutes just because this
  1517. 45:47just because I was using strings to
  1518. 45:48identify a child object. So this
  1519. 45:50terrible you don't want this to happen
  1520. 45:52again. You don't want to suffer all of
  1521. 45:54that all of that nonsense all of that
  1522. 45:57complexity in your own brain. You want
  1523. 45:58to keep things as similar to yourself as
  1524. 46:00possible. So yep, basically after this
  1525. 46:03happened, I decided okay, even for
  1526. 46:04transform.find, I will never ever use
  1527. 46:06that again just to avoid using strings
  1528. 46:08100% of the time. And since then, I have
  1529. 46:11not used them just to try to avoid all
  1530. 46:13of this mistake. Now, also don't use
  1531. 46:16tags since they are based on strings.
  1532. 46:18This is something that you might see in
  1533. 46:19a lot of especially Unity beginner
  1534. 46:21tutorials. They use tags to identify
  1535. 46:23objects, identify things. Don't use them
  1536. 46:25for the same reason because tags are
  1537. 46:27based on strings. So you should not use
  1538. 46:29them. Instead, you should use some kind
  1539. 46:30of tag component. So, if you're working
  1540. 46:32with some kind of game object and you
  1541. 46:33want to know if it's a certain specific
  1542. 46:35type, if so, then just check does it
  1543. 46:37have this component? If so, then it is
  1544. 46:38of this type. If not, then it isn't.
  1545. 46:42Now, if you absolutely must use strings,
  1546. 46:44because sometimes you do have to use
  1547. 46:45them. For example, when working with the
  1548. 46:47Unity animated system, if you're working
  1549. 46:49with parameters, you have to use
  1550. 46:51strings. There's no way around it. The
  1551. 46:52the API really only works with strings.
  1552. 46:54or if you're working with some kind of
  1553. 46:56web API, chances are you're probably
  1554. 46:58going to have to use strings. So if
  1555. 47:00that's the case, if you absolutely must
  1556. 47:01use them, if so, then use a constant
  1557. 47:03instead. That way, you define a constant
  1558. 47:04just once in your entire codebase. And
  1559. 47:07then everywhere else, you just use that
  1560. 47:08constant, you don't use the string. That
  1561. 47:10way, if you make a mistake, at least you
  1562. 47:12can only make the mistake in one place.
  1563. 47:14Everywhere else, if you're using the
  1564. 47:15constant and you make a mistake on the
  1565. 47:16constant name, the compiler will help
  1566. 47:18you. It won't tell you, oh, you wrote is
  1567. 47:20walking incorrectly. You probably didn't
  1568. 47:22mean that. So in this case at least you
  1569. 47:24try to keep yourself a little bit safe.
  1570. 47:26So for the most part try to avoid using
  1571. 47:28strings to identify anything instead.
  1572. 47:30But if you do need to use them if so
  1573. 47:32then use a constant. Essentially
  1574. 47:34[snorts] strings should only ever be
  1575. 47:36used for text. So if you want to tell
  1576. 47:37the player hello if so then go ahead use
  1577. 47:40a string but for identifying anything do
  1578. 47:42not use strings.
  1579. 47:44Next, on the topic of composition over
  1580. 47:47inheritance, which like mentioned
  1581. 47:48beginning, this quite interesting
  1582. 47:50because beginners when they start
  1583. 47:51learning object-oriented programming,
  1584. 47:53they'll learn about inheritance and they
  1585. 47:54tend to overuse it way too much. Whereas
  1586. 47:57the reality is inheritance technically
  1587. 47:59isn't always bad. It can be good in some
  1588. 48:01scenarios, but composition is usually
  1589. 48:03the better option. Inheritance is good,
  1590. 48:06but only in very small, very specific
  1591. 48:08use cases. For example, we've got this,
  1592. 48:10we've got some kind of door object, some
  1593. 48:12kind of chest object. So these does make
  1594. 48:14sense. Both a door and a chest should be
  1595. 48:16unlockable and they should be openable.
  1596. 48:18So in this case this makes sense. You
  1597. 48:20make a base class and all these objects
  1598. 48:21are meant to inherit all of that logic.
  1599. 48:23So in this case it makes sense. But then
  1600. 48:26let's say you want to implement some
  1601. 48:27kind of terminal object and all of a
  1602. 48:29sudden this does not make sense because
  1603. 48:30terminal you can unlock it. So you can
  1604. 48:32type on it but you can't really open a
  1605. 48:35terminal. That does not make sense. So
  1606. 48:37in this case if you do this you end up
  1607. 48:39with the door and chest. Those make
  1608. 48:41sense. Those do implement this logic but
  1609. 48:43terminal is all of a sudden implementing
  1610. 48:45some logic that does not make sense to
  1611. 48:46it open. So you are going to have some
  1612. 48:49kind of function that is probably going
  1613. 48:50to be open that is probably going to be
  1614. 48:52empty because it does not make sense
  1615. 48:53within this context. That is a problem
  1616. 48:55of inheritance. You end up inheriting
  1617. 48:57way more logic than you actually
  1618. 48:59intended. On this topic there's also a
  1619. 49:02funny joke which is you want the banana
  1620. 49:04but what you got was a gorilla holding
  1621. 49:05the banana and the entire jungle. So
  1622. 49:07let's say you've got some kind of banana
  1623. 49:08but the banana inherits from grabbable
  1624. 49:10object and the gorilla interacts with
  1625. 49:12grab object but then the gorilla also
  1626. 49:14inherits from animal and the animal
  1627. 49:16inherits from like organism which the
  1628. 49:18entire jungle inherits from. So yeah
  1629. 49:20basically you want just one tiny thing
  1630. 49:22but because of a very complex very messy
  1631. 49:24inheritance structure because that you
  1632. 49:26end up getting way more than you wanted.
  1633. 49:29So for the most part you should prefer
  1634. 49:31composition usually with interfaces. So
  1635. 49:34in this case you can define two
  1636. 49:35interfaces one unlocks one opens and
  1637. 49:38then you can say okay so the door this
  1638. 49:40one is meant to be unlockable and
  1639. 49:41openable. Same thing for chest but
  1640. 49:43terminal that one only makes sense to be
  1641. 49:45unlockable does not make sense to be
  1642. 49:46openable. So that way you basically
  1643. 49:48compose your objects based on different
  1644. 49:50pieces of behavior and you only add the
  1645. 49:51behavior that you actually need to. And
  1646. 49:54it's a very practical use case which is
  1647. 49:56this. So Unity itself already uses a
  1648. 49:58composition pattern with Unity. you've
  1649. 50:00got the main game object and then you
  1650. 50:02basically just attach components onto
  1651. 50:03it. So if you want a very practical use
  1652. 50:05case of composition over inheritance
  1653. 50:07then Unity itself is a great example.
  1654. 50:11Now in all of this again like I said
  1655. 50:13there's nuance to this. So inheritance
  1656. 50:15isn't always bad. There are some
  1657. 50:16scenarios where inheritance does make
  1658. 50:18sense like we saw for very tiny
  1659. 50:20scenarios where you do want to share all
  1660. 50:22this logic with multiple classes. If so,
  1661. 50:25inheritance might make sense, but I
  1662. 50:26would say in 95% of use cases, chances
  1663. 50:29are a composition pattern is going to be
  1664. 50:31easier.
  1665. 50:33Then finally, the topic of refactoring.
  1666. 50:35So this is another very important thing
  1667. 50:37because this refactoring is a constant
  1668. 50:39process of improving the code quality
  1669. 50:40and this something that you should be
  1670. 50:42doing constantly over and over again. So
  1671. 50:44you try to come up with some kind of
  1672. 50:45game mechanics, some kind of system.
  1673. 50:47First you make it work, then you make it
  1674. 50:49clean and then you're constantly
  1675. 50:50refactoring. You're constantly thinking,
  1676. 50:52is there a better way that I can do
  1677. 50:53this? And if so, then take the time to
  1678. 50:55do it. Now, in this, there's also
  1679. 50:57something that a lot of beginners
  1680. 50:58usually tell me when they see my videos,
  1681. 50:59which is, "Oh, the code quality that I
  1682. 51:02write is pretty terrible when I compare
  1683. 51:03with your videos." And the answer is
  1684. 51:05because they are comparing their first
  1685. 51:06draft with my six or seven drafts.
  1686. 51:08Basically, what you see in a video is
  1687. 51:09after I've done a ton of research, after
  1688. 51:11I've written and rewritten and
  1689. 51:13refactored a ton of code. So, what you
  1690. 51:15see is very much the final version. So,
  1691. 51:17don't compare it to your first draft
  1692. 51:18because your first draft, that one will
  1693. 51:20never be perfect. That is perfectly
  1694. 51:22normal. That is okay. That will not
  1695. 51:24happen. Essentially with the first
  1696. 51:26version, you first understand and
  1697. 51:27actually solve the problem. And once you
  1698. 51:29solve the problem once, then you have
  1699. 51:31quite a bit more understanding on the
  1700. 51:32problem. So you can figure out better
  1701. 51:33ways for solving that same problem. And
  1702. 51:35that is exactly what you do on the
  1703. 51:36second version, you take the solution
  1704. 51:38you came up with and with your better
  1705. 51:40knowledge, with your newer acquired
  1706. 51:41knowledge with that you can basically
  1707. 51:43improve and come up with a better
  1708. 51:44solution. And then afterwards, like I
  1709. 51:47said, constant subsequent versions you
  1710. 51:49are just constantly improving that same
  1711. 51:51problem. So again it's a very constant
  1712. 51:53process something that happens over and
  1713. 51:55over again. Now this some pregnant tips
  1714. 51:58what are some possible refactoring
  1715. 51:59steps. First of all renaming variables.
  1716. 52:02So again like I said you should come up
  1717. 52:03with the best name you can come up with
  1718. 52:05but then after you come up with name
  1719. 52:06constantly try to think is there a
  1720. 52:08better name for this thing. If so take
  1721. 52:09the time to rename it. Refactoring can
  1722. 52:12also mean extracting functions. So if
  1723. 52:14you got some kind of function that you
  1724. 52:15think is doing way too many things. If
  1725. 52:17so, then you probably want to split it
  1726. 52:19into multiple functions and each
  1727. 52:20function do one specific thing.
  1728. 52:22Refactoring can also mean splitting a
  1729. 52:24class. So if you define a class that is
  1730. 52:25meant to represent, let's say, the
  1731. 52:27player movement, but then you end up
  1732. 52:28doing more than just a player movement.
  1733. 52:30If so, that might be worthwhile. Split
  1734. 52:32it into multiple classes. But at the
  1735. 52:34same time, if you split it too much,
  1736. 52:35refactoring might just mean combining
  1737. 52:37some oversplit code. Sometimes you can
  1738. 52:39go way too insane and split things all
  1739. 52:41over the place and you realize, okay, if
  1740. 52:43some of these things were together, it
  1741. 52:44would actually make the code simpler. If
  1742. 52:46so, refactoring can also mean just
  1743. 52:48combining oversplit code. Refactoring is
  1744. 52:51also all about removing that code. So
  1745. 52:53game development is a very iterative
  1746. 52:55process. You're constantly trying out
  1747. 52:56new things trying to see what works. So
  1748. 52:58let's say you figure out, okay, I'm
  1749. 52:59going to make this kind of mechanic, try
  1750. 53:00to see if it works. So you build that
  1751. 53:02mechanic, but then you realize, uh, this
  1752. 53:04doesn't quite work right. Let's go with
  1753. 53:06another mechanic. Let's go in another
  1754. 53:07direction. And once you do that, that's
  1755. 53:08perfectly normal. That is the entire
  1756. 53:10purpose behind game development. But
  1757. 53:11once you do that, definitely go back and
  1758. 53:13try to get rid of that dead code. You
  1759. 53:15don't want it to keeping around because
  1760. 53:17then you don't know okay why is this not
  1761. 53:18being used anywhere is it intentional is
  1762. 53:20it not no so for refactoring make sure
  1763. 53:23any dead code you try to remove it as
  1764. 53:24much as possible
  1765. 53:26or assembly refactoring is just
  1766. 53:28simplifying an over complicated
  1767. 53:29abstraction. So if you come up with some
  1768. 53:31kind of pattern and you have some kind
  1769. 53:33of object that inherits from like a
  1770. 53:36bunch of different base classes and
  1771. 53:37implements a bunch of interface and a
  1772. 53:39bunch of things sometimes you can go way
  1773. 53:41too complex with all the kind of levels
  1774. 53:43of abstraction. So sometimes simplifying
  1775. 53:46that might be the better thing. So
  1776. 53:47constantly trying to think okay is there
  1777. 53:49a better way for me to solve this
  1778. 53:50problem something that is simpler that
  1779. 53:52is easier to understand if so go ahead
  1780. 53:54refactor that. But the important thing
  1781. 53:56is this it's a constant never- ending
  1782. 53:58process. You are constantly refactoring
  1783. 53:59things. you're constantly thinking okay
  1784. 54:01what exactly am I doing here what could
  1785. 54:03I do better constantly try to think that
  1786. 54:05and constantly try to improve the
  1787. 54:06quality of your code
  1788. 54:09now that let's do a quick simple live
  1789. 54:11refactor just so we can see how to apply
  1790. 54:13all these guidelines in nice practical
  1791. 54:16manner okay so here we've got some code
  1792. 54:18it's supposed to be some kind of keypad
  1793. 54:20or system so like the gift here is
  1794. 54:22showing so basically we want to have
  1795. 54:23some kind of keypad on some kind of door
  1796. 54:25then we want to input some kind of code
  1797. 54:27and if we get the right code it should
  1798. 54:28open door And basically that's how it
  1799. 54:30works. So input one, two, three, four
  1800. 54:32and then the door basically opens. We
  1801. 54:34can walk through just like that. So very
  1802. 54:35simple keypad system.
  1803. 54:39So here we've got that and we've got
  1804. 54:41some code and okay it is doing exactly
  1805. 54:43that. But this is some pretty bad code.
  1806. 54:46We can see for the start how we can
  1807. 54:48already find quite a lot of mistakes. So
  1808. 54:50we can see everything is public. We can
  1809. 54:52see the names are pretty bad. We can see
  1810. 54:53everything both the logic, the
  1811. 54:55animations, the audio it's all in just
  1812. 54:57one place. So that is not correct. So
  1813. 54:58instead, let's start off by fixing this.
  1814. 55:00First of all, like I said, the most
  1815. 55:01important thing are naming rules. So we
  1816. 55:03should give them some proper names. So
  1817. 55:05for here, instead of code, let's put it
  1818. 55:07like correct code. And again, I'm using
  1819. 55:09the ID in order to actually rename and
  1820. 55:11renames every instance of that. Then for
  1821. 55:13the current, this is actually the
  1822. 55:15current code. So let's give it some
  1823. 55:17proper name. Then for txt, again, don't
  1824. 55:20use abbreviations, don't use
  1825. 55:21dimminatives. So over here UI text mesh
  1826. 55:26which again we're going to refactor this
  1827. 55:28in a little bit. Then if you add the
  1828. 55:29animator instead of animator like that
  1829. 55:32then the audio source audio source and
  1830. 55:35then boom open instead of that is open
  1831. 55:38just like this and okay again just like
  1832. 55:40this with some proper names and let's
  1833. 55:41also add some proper formatting just
  1834. 55:43like this it is already quite a bit more
  1835. 55:45understandable you can just look at the
  1836. 55:46names of the things and you can easily
  1837. 55:47understand what exactly it is doing.
  1838. 55:49Next let us not make things public. Let
  1839. 55:52us hide as much information as possible.
  1840. 55:54So this should be private. So let's put
  1841. 55:56this one private. All of these as
  1842. 55:59private like this. And the function
  1843. 56:01itself is meant to be called from
  1844. 56:02outside. So that one does make sense to
  1845. 56:04be public. Now if we make it private, we
  1846. 56:06cannot assign it in the editor. Let's
  1847. 56:08say for the correct code, we actually
  1848. 56:09intentionally did want to do it in the
  1849. 56:12editor. If so, let's add a serialized
  1850. 56:14field attribute. That way it won't show
  1851. 56:16up in the editor, but it won't be
  1852. 56:17private, so it's not accessible by other
  1853. 56:19classes. Next up, let's solve some magic
  1854. 56:21numbers. So, for example, over here
  1855. 56:24we've got a four. What does this four
  1856. 56:26represent? Now, looking at it for a
  1857. 56:27little bit, I can see, okay, so this is
  1858. 56:29meant to be the correct code. That is
  1859. 56:30meant to be the actual length of the
  1860. 56:32code. But again, we shouldn't have to
  1861. 56:34try to guess that. It should be very,
  1862. 56:35very clear. So, over here, we can either
  1863. 56:37use an int for the correct code length.
  1864. 56:40We can use that and set it to four. We
  1865. 56:42can do this or in this case since that
  1866. 56:44one is actually meant to match up what
  1867. 56:45we have over here on the correct code
  1868. 56:47function because that we should really
  1869. 56:49just give it a even better name. So
  1870. 56:52instead of using a local variable for
  1871. 56:54that we can just use the correct code by
  1872. 56:56itself use.length and that way we have
  1873. 56:59just one source of truth. So we make
  1874. 57:00sure that even if we modify the correct
  1875. 57:02code if we put it five characters
  1876. 57:04everything still works. Okay. So next up
  1877. 57:07like I said do not use strings to
  1878. 57:09identify things. over here for the
  1879. 57:11boolean for this parameter. Like I said,
  1880. 57:13you can't really avoid this. You have to
  1881. 57:15use this. So that really sucks, but that
  1882. 57:17is the way it works. So if we have to
  1883. 57:19use strings, if so, let's use a proper
  1884. 57:22constant. So a private const. Let's make
  1885. 57:25a con string
  1886. 57:27and name it open and equals open. And
  1887. 57:30that way we just need to make sure that
  1888. 57:32this one over here is correct. And if
  1889. 57:33this one is correct, then everything is
  1890. 57:35going to work. So down here we just use
  1891. 57:37a constant as opposed to using a string.
  1892. 57:40Okay. So this is already looking quite a
  1893. 57:42bit better. But of course we are still
  1894. 57:44doing way too many things here. We are
  1895. 57:45handling both the logic. We're handling
  1896. 57:47the animation, the UI, the audio and so
  1897. 57:48on. That is not correct. So let us split
  1898. 57:51those up by quite a bit. And over here
  1899. 57:52by the way I'm using everything in just
  1900. 57:54one class just because to make it easier
  1901. 57:56over here for this video presentation.
  1902. 57:59Whereas obviously in Unity I would
  1903. 58:00create separate files and actually
  1904. 58:02attach them. put over here. So let's say
  1905. 58:03a public class and let's say one for the
  1906. 58:07keypad door UI and I want this class to
  1907. 58:10basically just handle the UI and nothing
  1908. 58:11else. So the UI text mesh that one
  1909. 58:12should not be here. That one should
  1910. 58:14instead be right here. And of course you
  1911. 58:16need to know okay how exactly do I make
  1912. 58:18this work? How exactly do I connect this
  1913. 58:20logic? Do I know when this one is
  1914. 58:22actually modifying? And the answer to
  1915. 58:24that to keeping things decoupled is to
  1916. 58:25use some nice events. So over here we
  1917. 58:27can find a public event using event
  1918. 58:30handler just because that's the standard
  1919. 58:32C# thing
  1920. 58:34and we can use on current code changed.
  1921. 58:38So we can do this kind of event and then
  1922. 58:42we are going to invoke this event. So
  1923. 58:44over here whenever the code changes we
  1924. 58:46invoke that. So on current change we do
  1925. 58:49invoke and pass in this and event args.
  1926. 58:55So we close that out and then down here
  1927. 58:57when the code is reset we do the same
  1928. 58:59thing. So then down here we just need to
  1929. 59:01listen that event which means we need
  1930. 59:02the reference to this. Now in this case
  1931. 59:04like I said imagining this was a Unity
  1932. 59:05game. So I would have a kind of
  1933. 59:08serialized field. So serialize field
  1934. 59:10private of keypad door for the keypad
  1935. 59:14door. So I've got this one right here.
  1936. 59:17And then on private void start on start
  1937. 59:22here I can basically just go onto that
  1938. 59:23one and hook on to on code change. I
  1939. 59:26hook onto that event. Then on this one
  1940. 59:28now I can go on the UI text mesh and set
  1941. 59:30that text and set it to what that one
  1942. 59:33currently has. So again information
  1943. 59:35hiding. So we want to keep the current
  1944. 59:37code as actual private and only expose
  1945. 59:39it if we need to which in this case we
  1946. 59:40do need to. So let's make a public void
  1947. 59:44get current code. Actually, it's not
  1948. 59:46void, but actually returning a string.
  1949. 59:49So, on this one and this one, we do
  1950. 59:51return that. So, return the current
  1951. 59:53code. Return that. And then down here,
  1952. 59:56we can actually do that. So, we can go
  1953. 59:57on the keypad door and get the current
  1954. 1:00:00code and two string on this one. And
  1955. 1:00:02yep, there you go. So, now up here, we
  1956. 1:00:04don't use UI text mesh. We don't use any
  1957. 1:00:07of this. Nope. We just fire off events.
  1958. 1:00:09And then we've got the UI class. That
  1959. 1:00:10one is listening to those events and
  1960. 1:00:11actually applying those changes. Now,
  1961. 1:00:13importantly is the keypad door. This one
  1962. 1:00:15over here is completely decoupled from
  1963. 1:00:16the UI. So I could get rid of this class
  1964. 1:00:19entirely and the code would still
  1965. 1:00:20compile because everything is nicely
  1966. 1:00:22decoupled.
  1967. 1:00:23Okay. So next we've got basically the
  1968. 1:00:25same thing for the audio source and for
  1969. 1:00:26the animator. So over here let's do
  1970. 1:00:28those. So public class the keypad door
  1971. 1:00:32animations.
  1972. 1:00:34So we've got this one right here. And
  1973. 1:00:36over here same thing we're going to have
  1974. 1:00:37a reference to the keypad door. We're
  1975. 1:00:40going to have that one. And on private
  1976. 1:00:41void start we're basically going to
  1977. 1:00:43listen to that event except over here we
  1978. 1:00:46need to make another event. So let's see
  1979. 1:00:47when is the animator meant to change.
  1980. 1:00:49It's basically when the door is open
  1981. 1:00:51which is when the correct code is
  1982. 1:00:52inputed. So public event handler let's
  1983. 1:00:56make on
  1984. 1:00:58correct code ins certain. So we do this
  1985. 1:01:02and then over here we're going to invoke
  1986. 1:01:04this event. So invoke
  1987. 1:01:08with this and event args. empty. So we
  1988. 1:01:11have that one. And for the animator set
  1989. 1:01:12B, we're basically just going to do that
  1990. 1:01:14down here. So on start, let's go on the
  1991. 1:01:17keypad door on the correct code insert.
  1992. 1:01:21Let's attach this one right here. And on
  1993. 1:01:23this one, that is when we are actually
  1994. 1:01:25going to set the binding, which means
  1995. 1:01:27down here is when we need to have this
  1996. 1:01:29constant right here. And we also need a
  1997. 1:01:32reference for the animator right here
  1998. 1:01:34like this. And also make it a serialized
  1999. 1:01:37field so we can set it in the editor.
  2000. 1:01:40So there I go. So that handles the
  2001. 1:01:41animations. And finally for the audio
  2002. 1:01:43source, that one is quite simple. So
  2003. 1:01:45public class keypad door audio. And on
  2004. 1:01:50this one, we're going to do very much
  2005. 1:01:51the same thing. So again, let's first
  2006. 1:01:53grab a reference. So we grab this one
  2007. 1:01:56right here. And then on private void
  2008. 1:01:59start on start, we're going to hook onto
  2009. 1:02:01that one. So keypad door. when the
  2010. 1:02:04correct code is inserted. When that
  2011. 1:02:06happens, then we're going to want to do
  2012. 1:02:08the same thing that we're doing up here.
  2013. 1:02:10So, audio source.play just like this, we
  2014. 1:02:13play it. And then over here, we get that
  2015. 1:02:15audio source. And we're also going to
  2016. 1:02:17expose it. So, over here, also make it a
  2017. 1:02:22serialize
  2018. 1:02:24field like this. And yep, there you go.
  2019. 1:02:28All right. So, yep. So, I think that's
  2020. 1:02:29about it. Now technically we could
  2021. 1:02:30improve this maybe a little bit more but
  2022. 1:02:32for the most part this quite good. We
  2023. 1:02:34could use like struct objects for the
  2024. 1:02:35correct code and a bunch more but for
  2025. 1:02:37the most part this is quite nice. So we
  2026. 1:02:39can see the code that we started with.
  2027. 1:02:41We basically cleaned up the names. We
  2028. 1:02:42cleaned up the access modifiers and we
  2029. 1:02:45made things nicely decoupled. So you've
  2030. 1:02:47got multiple classes as opposed to one.
  2031. 1:02:49And each class does one thing and one
  2032. 1:02:50thing only. So for example if we got
  2033. 1:02:52some kind of bug on the audio we know it
  2034. 1:02:54has to be on this class because these
  2035. 1:02:56other ones are completely decoupled. So
  2036. 1:02:58here we have a much better piece of code
  2037. 1:03:00that if you just looked at this code, if
  2038. 1:03:02you had no context of this project, just
  2039. 1:03:04looking at this code, you can instantly
  2040. 1:03:06see okay this one is handling the logic,
  2041. 1:03:07this one handling the visuals, the
  2042. 1:03:08animations and so on and you can easily
  2043. 1:03:11just by reading the names you can
  2044. 1:03:12understand. Okay, so that is what this
  2045. 1:03:14is doing. So this is much much easier to
  2046. 1:03:16follow. Now if you want to see another
  2047. 1:03:17detailed refactoring tutorial, you can
  2048. 1:03:19go ahead and watch this video. By the
  2049. 1:03:20way, there's a link at the end of the
  2050. 1:03:22slides, but yep, this one 30-minute
  2051. 1:03:24video where I did basically the same
  2052. 1:03:25thing that I covered here. basically
  2053. 1:03:26going through that cleaning up the names
  2054. 1:03:27access modifiers and all those things.
  2055. 1:03:30All right. So a quick recap of
  2056. 1:03:31everything that we covered in this talk.
  2057. 1:03:33So first of all definition what is clean
  2058. 1:03:34code? That is code that is easy to
  2059. 1:03:36understand and easy to modify. So for
  2060. 1:03:38that first we'll learn about that
  2061. 1:03:40definition. What is and importantly what
  2062. 1:03:41is not clean code. Both are very
  2063. 1:03:43important. Then we learned why does all
  2064. 1:03:45of this matter if the code just works.
  2065. 1:03:47So hopefully throughout this entire talk
  2066. 1:03:48eventually convince you that just
  2067. 1:03:50working is not enough if you want to be
  2068. 1:03:51a proper professional. if you want to
  2069. 1:03:53make some proper games. If so, you need
  2070. 1:03:54to write some proper clean code so you
  2071. 1:03:56can actually finish your projects. Then
  2072. 1:03:58we covered naming rules. So basically
  2073. 1:04:00the most important part of this talk. If
  2074. 1:04:01you just follow this one thing, I
  2075. 1:04:02guarantee your code quality will
  2076. 1:04:04massively improve. Then we covered how
  2077. 1:04:06decoupling is a superpower. Trying to
  2078. 1:04:08keep things as separated as possible.
  2079. 1:04:10That helps out your brain quite a bit.
  2080. 1:04:11So you can work on the code base without
  2081. 1:04:13having to keep a massive amount of code
  2082. 1:04:15inside of your head. Then we cover the
  2083. 1:04:17single responsibility principle. how
  2084. 1:04:19each class each function should do one
  2085. 1:04:21thing and one thing only. This is
  2086. 1:04:22another guideline that is relatively
  2087. 1:04:24easy to follow and if you just follow
  2088. 1:04:25this your code quality will increase
  2089. 1:04:27massively. After that we covered
  2090. 1:04:29information hiding. So always about
  2091. 1:04:31trying to minimize your mental load,
  2092. 1:04:32trying to minimize how much code you
  2093. 1:04:34have to keep in your head and if you
  2094. 1:04:35hide things as much as possible. If so
  2095. 1:04:37then helps out by quite a lot. Next, one
  2096. 1:04:40of the better debugging tips, one sort
  2097. 1:04:42of truth. Every piece of that in your
  2098. 1:04:44game should exist in one place and one
  2099. 1:04:46place only. So if you focus on that it
  2100. 1:04:48won't solve so many possible bugs. Then
  2101. 1:04:51we covered some documenting code. So how
  2102. 1:04:53comments they should be used very
  2103. 1:04:55sparingly. For the most part the code
  2104. 1:04:56itself should be very clear what exactly
  2105. 1:04:58it represents what exile it's doing and
  2106. 1:05:00comments should explain the why and not
  2107. 1:05:02the what. Then we covered how magic
  2108. 1:05:04numbers and how you should not use them.
  2109. 1:05:06And instead of using magic numbers you
  2110. 1:05:08should use some proper name variables.
  2111. 1:05:09That way the meaning is much more clear.
  2112. 1:05:12Then we also covered strings and
  2113. 1:05:13identifiers and why you should not use
  2114. 1:05:15them. It is very easy to make mistakes.
  2115. 1:05:17You can write strings that look very
  2116. 1:05:18very similar but they are very different
  2117. 1:05:20and you are only going to find that
  2118. 1:05:22difference once you try to run the code.
  2119. 1:05:23You are going to come up with all sorts
  2120. 1:05:25of errors. Next we covered composition
  2121. 1:05:27over inheritance. So these are two
  2122. 1:05:29interesting patterns and for the most
  2123. 1:05:31part I would say composition is what you
  2124. 1:05:33should be using even though inheritance
  2125. 1:05:34sometimes might be the right answer. And
  2126. 1:05:37finally refactoring how everything that
  2127. 1:05:38we're doing here this meant to be a
  2128. 1:05:40constant never- ending process. You are
  2129. 1:05:41constantly meant to be thinking about is
  2130. 1:05:43there a better way for me to do this?
  2131. 1:05:44And if so, take the time to refactor and
  2132. 1:05:46make it better. But at the same time,
  2133. 1:05:48remember how these are just guidelines.
  2134. 1:05:50These are not unbreakable rules. So
  2135. 1:05:52always think, does this code does this
  2136. 1:05:54make the code easy to understand and
  2137. 1:05:56easier to modify? If so, follow the
  2138. 1:05:57guideline. If not, then don't follow it.
  2139. 1:06:01The ultimate goal behind everything that
  2140. 1:06:02we learned in this talk is this. The
  2141. 1:06:04entire goal behind writing clean code is
  2142. 1:06:06it helps you make more complex, more
  2143. 1:06:07interesting, better games. And
  2144. 1:06:08importantly, actually finish them.
  2145. 1:06:10Actually take them to the finish line. I
  2146. 1:06:12want you to have lots of very
  2147. 1:06:13successful, very complete projects as
  2148. 1:06:15opposed to just a mountain of unfinished
  2149. 1:06:17prototypes. So for that, focusing on
  2150. 1:06:19clean focus on focusing on clean code
  2151. 1:06:21quality will help you achieve the final
  2152. 1:06:23product much better. Now if you want to
  2153. 1:06:25learn a bit more, here are some more
  2154. 1:06:27resources. So first of all, this is my
  2155. 1:06:29favorite book of all time, code complete
  2156. 1:06:312. It is a massive book. It is like 500
  2157. 1:06:33pages long. It is insanely detailed,
  2158. 1:06:35insanely deep, insanely massive, but it
  2159. 1:06:37contains a lot of very good information.
  2160. 1:06:40This is the book that when I read it
  2161. 1:06:41massively increased the quality of my
  2162. 1:06:43code by like a thousandx. So I highly
  2163. 1:06:45highly recommend it. Definitely take
  2164. 1:06:47your time to go through it because it is
  2165. 1:06:48very technical, very deep. But if you
  2166. 1:06:50take the time to go through it, it will
  2167. 1:06:51massively increase the quality of your
  2168. 1:06:53code. Then of course I've got my YouTube
  2169. 1:06:55channel where I've got a ton of
  2170. 1:06:56tutorials and a ton of courses. So on
  2171. 1:06:58all of those, I always try to teach you
  2172. 1:07:00how to write proper clean code. I don't
  2173. 1:07:02teach the quick and route. No. So if you
  2174. 1:07:04want to see a very practical application
  2175. 1:07:06of everything that we covered here, go
  2176. 1:07:07ahead, watch these videos. watch these
  2177. 1:07:09tutorials.
  2178. 1:07:11And also on that, if you go onto this
  2179. 1:07:13link, if you scan this QR code, you can
  2180. 1:07:15go to a place where you can find all
  2181. 1:07:16these slides. So, you can basically
  2182. 1:07:17refer back to it. You can also find that
  2183. 1:07:20video that I mentioned a while ago. And
  2184. 1:07:21in there, actually, there's some nice
  2185. 1:07:23bonus coupons. So, I've got my C#
  2186. 1:07:25course, my complete C# course, which has
  2187. 1:07:27a free 12-hour video over here on
  2188. 1:07:29YouTube. That free video contains all
  2189. 1:07:31the video lectures. So, if you just want
  2190. 1:07:32the video, you can watch that for free.
  2191. 1:07:34But if you can afford them, you've got
  2192. 1:07:35the premium version. And that one
  2193. 1:07:37contains a bunch of awesome bonuses.
  2194. 1:07:39Specifically, contains freely last
  2195. 1:07:40questions, quizzes, and interactive
  2196. 1:07:42exercises. Those basically require you
  2197. 1:07:44to actually write code in order to solve
  2198. 1:07:45the exercises. That means you are
  2199. 1:07:47actually learning the contents of each
  2200. 1:07:48lecture. And for that, for the premium
  2201. 1:07:50version, you can use the bonus coupon
  2202. 1:07:52clean to get a bonus discount off. Same
  2203. 1:07:55thing for my problem solving course.
  2204. 1:07:56Basically, programming is all about
  2205. 1:07:58experience. Trying to gain as much
  2206. 1:07:59experience as possible. That is the
  2207. 1:08:00closest thing there is to achieve code.
  2208. 1:08:02The more code you write, the better you
  2209. 1:08:03become. And my problem solving course is
  2210. 1:08:05basically meant for that. It is a
  2211. 1:08:06collection of over 200 exercise that you
  2212. 1:08:08can solve. And if you solve all of them,
  2213. 1:08:10I guarantee your skills as a programmer
  2214. 1:08:13will massively improve. And also
  2215. 1:08:15recently I started my game that practice
  2216. 1:08:16lab that is my weekly game mechanic
  2217. 1:08:18challenge. So basically every two weeks
  2218. 1:08:20I give you a new challenge like
  2219. 1:08:21implement the CCV camera system or
  2220. 1:08:23implement a safe system. I give you the
  2221. 1:08:25starting project files. I give you a set
  2222. 1:08:27of objectives and you basically have to
  2223. 1:08:28download those project files and try to
  2224. 1:08:30achieve those objectives. try to
  2225. 1:08:31actually implement those and with that
  2226. 1:08:33one you can use the coupon clean in
  2227. 1:08:35order to get the first month off. So go
  2228. 1:08:38ahead check out all these check out all
  2229. 1:08:39these resources and yep I hope you enjoy
  2230. 1:08:41the song. Now go ahead write some clean
  2231. 1:08:43understandable and easy to modify code.
  2232. 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.