1 00:00:06,600 --> 00:00:07,710 - Code Reviews. 2 00:00:07,710 --> 00:00:10,760 Alright, since we're going to be focusing 3 00:00:10,760 --> 00:00:13,190 on reading code in this class, 4 00:00:13,190 --> 00:00:15,840 we've got to have a measuring stick, 5 00:00:15,840 --> 00:00:18,310 we've got to have priorities in reading code. 6 00:00:18,310 --> 00:00:20,320 And I do these things in my head while I'm coding, 7 00:00:20,320 --> 00:00:22,050 we do these in code reviews, 8 00:00:22,050 --> 00:00:25,470 and this is also from my perspective goes priority 9 00:00:25,470 --> 00:00:27,320 when we're reading and writing code. 10 00:00:27,320 --> 00:00:29,740 Number one is integrity. 11 00:00:29,740 --> 00:00:32,580 And if integrity isn't your number one, 12 00:00:32,580 --> 00:00:34,500 I'm counting right now, you can't work for me. 13 00:00:34,500 --> 00:00:36,100 Integrity is everything. 14 00:00:36,100 --> 00:00:40,180 Integrity is about being serious, about reliability, 15 00:00:40,180 --> 00:00:41,890 and it's really important. 16 00:00:41,890 --> 00:00:44,220 In fact, I love asking this question. 17 00:00:44,220 --> 00:00:45,650 This is my cell phone, 18 00:00:45,650 --> 00:00:47,650 you've got one, I have one, 19 00:00:47,650 --> 00:00:49,170 everybody you know has one, 20 00:00:49,170 --> 00:00:51,150 we can't live without these things. 21 00:00:51,150 --> 00:00:52,930 So, here's an interesting question, 22 00:00:52,930 --> 00:00:56,190 if this cell phone stops working for the next hour, 23 00:00:56,190 --> 00:00:57,380 your phone, my phone, 24 00:00:57,380 --> 00:00:59,000 everybody's phone in the United States, 25 00:00:59,000 --> 00:01:02,090 it stops working for the next hour, 26 00:01:02,090 --> 00:01:04,470 how many people lose their lives? 27 00:01:04,470 --> 00:01:06,320 I know it's a serious question, 28 00:01:06,320 --> 00:01:09,100 but the reality is is that if this phone stops working 29 00:01:09,100 --> 00:01:10,270 for the next hour, 30 00:01:10,270 --> 00:01:12,690 the answer to that question is not zero, 31 00:01:12,690 --> 00:01:14,830 somebody's gonna lose their lives. 32 00:01:14,830 --> 00:01:19,140 Our lives are so embedded in technology today 33 00:01:21,045 --> 00:01:22,480 that if things can go bad 34 00:01:22,480 --> 00:01:23,860 and people are gonna lose their lives, 35 00:01:23,860 --> 00:01:26,320 this is a very serious situation, 36 00:01:26,320 --> 00:01:28,940 this is why integrity has to be number one. 37 00:01:28,940 --> 00:01:31,700 Do you realize that we are one of the very few industries 38 00:01:31,700 --> 00:01:33,550 where people's lives are at risk, 39 00:01:33,550 --> 00:01:36,080 and we're not licensed to do our job. 40 00:01:36,080 --> 00:01:38,550 I mean, how long do you think it's going to last 41 00:01:38,550 --> 00:01:40,110 that we as software developers 42 00:01:40,110 --> 00:01:42,870 are not gonna need a license to do our jobs 43 00:01:42,870 --> 00:01:45,430 because at the end of the day everyone's life is at risk. 44 00:01:45,430 --> 00:01:47,217 Now you might say "Bill, come on man, 45 00:01:47,217 --> 00:01:48,450 "I'm in the gaming industry." 46 00:01:48,450 --> 00:01:51,710 Or "I'm in some industry where people are having fun." 47 00:01:51,710 --> 00:01:53,610 No, the reality is that, 48 00:01:53,610 --> 00:01:56,440 even with the mental illness crisis in this country, 49 00:01:56,440 --> 00:01:59,140 your software, your bug, your problem, 50 00:01:59,140 --> 00:02:02,490 may be the one that puts somebody over the edge, 51 00:02:02,490 --> 00:02:04,360 over the edge, and so I need you 52 00:02:04,360 --> 00:02:06,000 to constantly put into your mind 53 00:02:06,000 --> 00:02:07,360 that regardless of the industry, 54 00:02:07,360 --> 00:02:09,130 and regardless of the software right? 55 00:02:09,130 --> 00:02:11,540 People's lives, their happiness, 56 00:02:11,540 --> 00:02:12,810 everything is being dependent 57 00:02:12,810 --> 00:02:14,370 on the technology that you're building, 58 00:02:14,370 --> 00:02:17,570 and we got to take integrity seriously. 59 00:02:17,570 --> 00:02:20,180 Now integrity comes in two places. 60 00:02:20,180 --> 00:02:22,640 There's a micro level and a macro level. 61 00:02:22,640 --> 00:02:24,580 At the micro level, I want you to understand 62 00:02:24,580 --> 00:02:26,450 that every line of code you write, 63 00:02:26,450 --> 00:02:28,860 every line of code either does one of three things. 64 00:02:28,860 --> 00:02:31,410 It allocates memory, reads that memory, 65 00:02:31,410 --> 00:02:32,930 and it writes to that memory. 66 00:02:32,930 --> 00:02:34,070 What's really interesting is, 67 00:02:34,070 --> 00:02:37,710 all our code is doing is reading and writing to memory. 68 00:02:37,710 --> 00:02:40,970 What's really interesting is all we are doing 69 00:02:40,970 --> 00:02:44,480 in our code is reading and writing to memory. 70 00:02:44,480 --> 00:02:46,680 And yet all of those reads and writes 71 00:02:46,680 --> 00:02:48,580 cause everything around us to happen. 72 00:02:48,580 --> 00:02:51,380 Lights are on, you're watching this video, 73 00:02:51,380 --> 00:02:52,330 phones are ringing. 74 00:02:52,330 --> 00:02:54,090 It's all reads and writes. 75 00:02:54,090 --> 00:02:55,950 What's even crazier is we're really just reading 76 00:02:55,950 --> 00:02:57,580 and writing integers at the end of the day 77 00:02:57,580 --> 00:02:59,770 to really mind-blowing stuff. 78 00:02:59,770 --> 00:03:02,520 So at a micro level, integrity comes from the fact that 79 00:03:02,520 --> 00:03:03,860 those reads and writes 80 00:03:03,860 --> 00:03:06,440 are always accurate consistent and efficient. 81 00:03:06,440 --> 00:03:08,800 When they're not, then we have an integrity issue. 82 00:03:08,800 --> 00:03:11,720 We've got corruption, software now has to shut down, 83 00:03:11,720 --> 00:03:12,770 because if it doesn't, 84 00:03:12,770 --> 00:03:15,120 we're gonna cause more damage than good. 85 00:03:15,120 --> 00:03:18,190 But there's also integrity at a macro level. 86 00:03:18,190 --> 00:03:21,320 Want you to understand that really what we do every day 87 00:03:21,320 --> 00:03:24,310 is solve data transformation problems. 88 00:03:24,310 --> 00:03:27,320 Data or InDesign is gonna be a big part of this class. 89 00:03:27,320 --> 00:03:28,700 We work with data. 90 00:03:28,700 --> 00:03:30,010 And data or InDesign says that 91 00:03:30,010 --> 00:03:31,310 if you don't understand the data, 92 00:03:31,310 --> 00:03:33,590 you do not understand the problem that you're working on, 93 00:03:33,590 --> 00:03:35,980 because the problem is the data. 94 00:03:35,980 --> 00:03:38,190 And our functions and our methods, 95 00:03:38,190 --> 00:03:40,260 and these encapsulations that we write 96 00:03:40,260 --> 00:03:42,510 are all about the data transformations 97 00:03:42,510 --> 00:03:43,870 that we have to perform. 98 00:03:43,870 --> 00:03:46,710 And so if your data transformations are not accurate, 99 00:03:46,710 --> 00:03:48,270 consistent and efficient, 100 00:03:48,270 --> 00:03:50,150 you also have integrity issues. 101 00:03:50,150 --> 00:03:53,030 And we have to apply this idea of data or InDesign, 102 00:03:53,030 --> 00:03:55,420 goes very focused on data or InDesign. 103 00:03:55,420 --> 00:03:57,490 I'm gonna show you this in the language 104 00:03:57,490 --> 00:03:59,160 as opposed to object or InDesign. 105 00:03:59,160 --> 00:04:01,210 Data or InDesign is gonna be very big here, 106 00:04:01,210 --> 00:04:03,370 and it's again all about our data transformations 107 00:04:03,370 --> 00:04:05,850 being accurate, consistent and efficient. 108 00:04:05,850 --> 00:04:07,500 You know, one of the things I like to do is, 109 00:04:07,500 --> 00:04:10,770 I have a friend who's in the data science world, 110 00:04:10,770 --> 00:04:12,617 and I always say to him, "You know man, you guys 111 00:04:12,617 --> 00:04:13,847 "are a trip over there you know, 112 00:04:13,847 --> 00:04:16,027 "you guys came up with two funny words, 113 00:04:16,027 --> 00:04:18,097 "a machine learning and AI, 114 00:04:18,097 --> 00:04:19,027 "and suddenly you guys created 115 00:04:19,027 --> 00:04:20,877 "this whole kind of software industry around, 116 00:04:20,877 --> 00:04:21,920 "just data science." 117 00:04:21,920 --> 00:04:24,147 I'm like "Dude we've been doing data science, 118 00:04:24,147 --> 00:04:26,487 "machine learning and AI for 30 years, 119 00:04:26,487 --> 00:04:28,300 "we're even building predictive algorithms." 120 00:04:28,300 --> 00:04:30,740 Just because we didn't you know, formalize the models 121 00:04:30,740 --> 00:04:32,040 and all that stuff, doesn't mean 122 00:04:32,040 --> 00:04:34,120 we haven't been doing, every is. 123 00:04:34,120 --> 00:04:35,373 What I want you to have in your head 124 00:04:35,373 --> 00:04:37,910 is that you are a data scientist. 125 00:04:37,910 --> 00:04:39,770 If you are a software developer, 126 00:04:39,770 --> 00:04:43,130 you are a data scientist because you're working with data, 127 00:04:43,130 --> 00:04:44,563 you're transforming data, 128 00:04:45,546 --> 00:04:47,440 and it's what you do on a daily basis. 129 00:04:47,440 --> 00:04:48,470 If you don't understand the data, 130 00:04:48,470 --> 00:04:49,620 you don't understand the problem. 131 00:04:49,620 --> 00:04:51,150 If you don't understand the problem, 132 00:04:51,150 --> 00:04:54,220 how can you write a single line of code to solve it. 133 00:04:54,220 --> 00:04:58,810 Now, integrity is going to really come in two places 134 00:04:58,810 --> 00:05:01,290 from a macro level, and things that we're gonna focus 135 00:05:01,290 --> 00:05:02,447 on in this class as well. 136 00:05:02,447 --> 00:05:03,800 But I already told you 137 00:05:03,800 --> 00:05:06,220 before that lines of code is critical, 138 00:05:06,220 --> 00:05:08,540 the amount of lines of code in your project 139 00:05:08,540 --> 00:05:10,367 will determine the health of your team, 140 00:05:10,367 --> 00:05:12,170 and the health of your project. 141 00:05:12,170 --> 00:05:14,430 I said before that the average developer 142 00:05:14,430 --> 00:05:17,320 cannot handle more than 10,000 lines of code, 143 00:05:17,320 --> 00:05:19,300 cannot maintain a mental model 144 00:05:19,300 --> 00:05:21,760 of more than 10,000 lines of code. 145 00:05:21,760 --> 00:05:23,410 Once projects get larger than that, 146 00:05:23,410 --> 00:05:25,230 we really need more developers, 147 00:05:25,230 --> 00:05:26,960 and we keep focusing this idea 148 00:05:26,960 --> 00:05:29,130 that we need to write less code. 149 00:05:29,130 --> 00:05:31,070 And there was a study done 150 00:05:31,070 --> 00:05:35,180 where they identified that for every thousand lines of code 151 00:05:35,180 --> 00:05:36,680 that's written, the average developer 152 00:05:36,680 --> 00:05:38,890 will produce 15 to 50 bugs. 153 00:05:38,890 --> 00:05:42,530 Now I wanna put ourselves in the worst developer category 154 00:05:42,530 --> 00:05:45,350 which basically means that for every 20 lines of code 155 00:05:45,350 --> 00:05:47,300 you write and I write, 156 00:05:47,300 --> 00:05:51,030 we've added a bug to the software whether we like it or not. 157 00:05:51,030 --> 00:05:53,140 Start putting that in your head. 158 00:05:53,140 --> 00:05:55,700 So when you start thinking about let me add setters, 159 00:05:55,700 --> 00:05:57,820 and let me add getters, and let me add abstractions 160 00:05:57,820 --> 00:05:59,200 on top of abstractions, 161 00:05:59,200 --> 00:06:00,480 really what you're saying is 162 00:06:00,480 --> 00:06:02,730 let me throw more code at this project, 163 00:06:02,730 --> 00:06:05,120 and therefore let me throw more bugs at this project. 164 00:06:05,120 --> 00:06:07,250 We need to take this idea in the reverse, 165 00:06:07,250 --> 00:06:09,780 less is always more. 166 00:06:09,780 --> 00:06:11,320 And what's interesting is, 167 00:06:11,320 --> 00:06:13,730 when we have more code than we need right? 168 00:06:13,730 --> 00:06:16,430 Then we've got more places for bugs to hide, 169 00:06:16,430 --> 00:06:19,070 and it also means you've got more incomplete tests 170 00:06:19,070 --> 00:06:20,670 because you've got to write that many more tests 171 00:06:20,670 --> 00:06:22,550 to deal with that many more lines of code, 172 00:06:22,550 --> 00:06:25,970 and eventually a codebase will collapse on itself, 173 00:06:25,970 --> 00:06:27,920 and instead of you maintaining your frameworks 174 00:06:27,920 --> 00:06:30,180 and your patterns, you start to just tack, 175 00:06:30,180 --> 00:06:33,930 and that's when legacy becomes a real threat to the project. 176 00:06:33,930 --> 00:06:36,180 Now another big part of integrity 177 00:06:36,180 --> 00:06:37,970 outside of just writing less code 178 00:06:37,970 --> 00:06:40,040 is also dealing with error handling. 179 00:06:40,040 --> 00:06:43,960 An error handling is a big big part of integrity. 180 00:06:43,960 --> 00:06:45,910 Here's the reality. 181 00:06:45,910 --> 00:06:49,360 Anybody could write a software that works when life is good. 182 00:06:49,360 --> 00:06:52,380 A six-year-old can google today and find code 183 00:06:52,380 --> 00:06:54,420 that works when life is good. 184 00:06:54,420 --> 00:06:57,070 Engineering is not about when life is good, 185 00:06:57,070 --> 00:06:59,120 engineering is about when life is bad, 186 00:06:59,120 --> 00:07:00,770 and things are happening, 187 00:07:00,770 --> 00:07:03,670 and your software has to be able to maintain stability, 188 00:07:03,670 --> 00:07:07,950 and not add to the crisis, but try to recover from it. 189 00:07:07,950 --> 00:07:10,260 Error handling is what we do every day, 190 00:07:10,260 --> 00:07:12,240 the majority of the code you write 191 00:07:12,240 --> 00:07:16,030 is focused on failure, not the good case. 192 00:07:16,030 --> 00:07:17,630 And if that's not happening today, 193 00:07:17,630 --> 00:07:20,180 I need you to reevaluate what's going on. 194 00:07:20,180 --> 00:07:22,230 I love the way Go deals with error handling, 195 00:07:22,230 --> 00:07:23,400 it was how I've been dealing 196 00:07:23,400 --> 00:07:25,350 with error handling for 30 years, 197 00:07:25,350 --> 00:07:27,230 even when I had exception handling 198 00:07:27,230 --> 00:07:29,560 in languages like C++ and C#. 199 00:07:29,560 --> 00:07:33,420 I used it really just to deal with the unhandled stuff. 200 00:07:33,420 --> 00:07:36,140 I would do very much what we're doing in Go today, 201 00:07:36,140 --> 00:07:39,050 and return errors out or codes out, 202 00:07:39,050 --> 00:07:41,340 and handle the errors right then and there, 203 00:07:41,340 --> 00:07:43,720 and I love the way error handling is done in Go. 204 00:07:43,720 --> 00:07:44,870 We're gonna talk about these things 205 00:07:44,870 --> 00:07:47,500 when we get to the error handling section during design. 206 00:07:47,500 --> 00:07:50,720 But there was a study done on some very critical software. 207 00:07:50,720 --> 00:07:52,530 In fact Redis is on this list. 208 00:07:52,530 --> 00:07:55,210 And how many lives today are dependent on Redis? 209 00:07:55,210 --> 00:07:57,350 I mean there's a tremendous amount. 210 00:07:57,350 --> 00:07:59,690 And they identify that out of these projects 211 00:07:59,690 --> 00:08:01,490 they were looking at, and the critical failures 212 00:08:01,490 --> 00:08:03,140 that caused this software 213 00:08:03,140 --> 00:08:05,408 to either go down or have corruption. 214 00:08:05,408 --> 00:08:08,360 92% of those problems could have been avoided, 215 00:08:08,360 --> 00:08:13,040 92% if the error handling was better, was more focused. 216 00:08:13,040 --> 00:08:17,630 92%, see error handling is really really critical 217 00:08:17,630 --> 00:08:19,410 if integrity is important. 218 00:08:19,410 --> 00:08:21,900 So we're gonna focus on that during design as well. 219 00:08:21,900 --> 00:08:24,520 We're gonna be constantly focused on less code 220 00:08:24,520 --> 00:08:27,950 and error handling as we get into design. 221 00:08:27,950 --> 00:08:31,890 The next step here on our code reviews is readability. 222 00:08:31,890 --> 00:08:33,760 Readability is number two. 223 00:08:33,760 --> 00:08:36,460 Doesn't trump integrity, nothing trumps integrity, 224 00:08:36,460 --> 00:08:38,830 you do in a code review and you find an integrity issue, 225 00:08:38,830 --> 00:08:40,300 that has to be fixed. 226 00:08:40,300 --> 00:08:41,830 But readability is number two. 227 00:08:41,830 --> 00:08:44,210 We talked about optimizing for correctness 228 00:08:44,210 --> 00:08:45,770 which is about readability. 229 00:08:45,770 --> 00:08:47,000 Now readability means that 230 00:08:47,000 --> 00:08:48,800 we're going to structure our software 231 00:08:48,800 --> 00:08:50,650 in a way that's comprehensible. 232 00:08:50,650 --> 00:08:52,820 And there's two aspects of readability 233 00:08:52,820 --> 00:08:54,400 that I wannna focus on. 234 00:08:54,400 --> 00:08:57,150 The first one is the average developer. 235 00:08:57,150 --> 00:09:00,870 The average developer on your team should be able 236 00:09:00,870 --> 00:09:05,160 to understand the full mental model of the codebase 237 00:09:05,160 --> 00:09:06,530 that we're working on, 238 00:09:06,530 --> 00:09:08,340 should be able to read every line of code, 239 00:09:08,340 --> 00:09:10,550 have a full knowledge of the mental models, 240 00:09:10,550 --> 00:09:12,590 and should be able to fix almost any bug, 241 00:09:12,590 --> 00:09:16,100 in fact every bug that comes across the codebase. 242 00:09:16,100 --> 00:09:19,130 Now what I love doing is asking you to think about this. 243 00:09:19,130 --> 00:09:20,330 I want you to think about you, 244 00:09:20,330 --> 00:09:22,460 and I want you to think about your team. 245 00:09:22,460 --> 00:09:24,450 Have that in your head right now. 246 00:09:24,450 --> 00:09:28,600 Now ask yourself, am I the average developer on my team? 247 00:09:28,600 --> 00:09:31,530 Do I comprehend the entire codebase we're working on? 248 00:09:31,530 --> 00:09:34,140 Do I have a strong mental model of what's going on? 249 00:09:34,140 --> 00:09:37,740 Can I solve every single bug or problem 250 00:09:37,740 --> 00:09:39,680 that comes across this codebase? 251 00:09:39,680 --> 00:09:40,750 The answer is no, 252 00:09:40,750 --> 00:09:42,700 then you're not the average developer. 253 00:09:42,700 --> 00:09:45,000 Now I want you to think yourself for a second, 254 00:09:45,000 --> 00:09:48,000 I'm I less than average or I'm I more than average? 255 00:09:48,000 --> 00:09:49,680 Now the more than average developers 256 00:09:49,680 --> 00:09:51,410 really have a burden on themselves 257 00:09:51,410 --> 00:09:53,870 because you have to have responsibility 258 00:09:53,870 --> 00:09:56,020 to not write code that is clever. 259 00:09:56,020 --> 00:09:57,040 If you're less than average, 260 00:09:57,040 --> 00:09:59,320 you have the responsibility to come back up. 261 00:09:59,320 --> 00:10:02,520 Now I want you to evaluate everybody on your team, 262 00:10:02,520 --> 00:10:04,450 who is average, who is less than average, 263 00:10:04,450 --> 00:10:05,800 who is more than average. 264 00:10:05,800 --> 00:10:07,280 Are those less than average people 265 00:10:07,280 --> 00:10:09,250 working hard to come up to speed? 266 00:10:09,250 --> 00:10:10,800 And are you helping them? 267 00:10:10,800 --> 00:10:12,370 Or are you a hindrance? 268 00:10:12,370 --> 00:10:14,390 As the average developer you have responsibility 269 00:10:14,390 --> 00:10:15,600 to bring them up. 270 00:10:15,600 --> 00:10:18,680 Are you or who else on your team is more than average? 271 00:10:18,680 --> 00:10:21,200 Is that person writing really clever code? 272 00:10:21,200 --> 00:10:23,490 Have you taken the responsibility to tell them 273 00:10:23,490 --> 00:10:24,930 that their code is too clever? 274 00:10:24,930 --> 00:10:28,220 And are they taking responsibility to not do that? 275 00:10:28,220 --> 00:10:30,040 Look, you put me on a crypto team, 276 00:10:30,040 --> 00:10:32,550 I am less than average, I've got to come up. 277 00:10:32,550 --> 00:10:34,970 You put me on a team building business applications, 278 00:10:34,970 --> 00:10:37,190 Web APIs, I'm gonna be above average, 279 00:10:37,190 --> 00:10:39,740 I've got to make sure my code comes down. 280 00:10:39,740 --> 00:10:41,300 And if you're hiring somebody, 281 00:10:41,300 --> 00:10:44,330 and you don't know whether they're average less or more, 282 00:10:44,330 --> 00:10:47,790 you're really going to cause problems on your team dynamic. 283 00:10:47,790 --> 00:10:49,700 If you're hiring people who are less than average, 284 00:10:49,700 --> 00:10:52,470 do you have the time to bring them up to speed? 285 00:10:52,470 --> 00:10:54,170 The answer is probably no. 286 00:10:54,170 --> 00:10:55,630 But if you do take the time, 287 00:10:55,630 --> 00:10:57,820 think about how an amazing developer 288 00:10:57,820 --> 00:10:59,090 they're gonna be for your team. 289 00:10:59,090 --> 00:11:02,120 If you're hiring people who are more or above-average, 290 00:11:02,120 --> 00:11:05,550 do you have time to teach them how to be less clever? 291 00:11:05,550 --> 00:11:07,420 Are they willing to be less clever? 292 00:11:07,420 --> 00:11:09,370 That's going to cause more problems 293 00:11:09,370 --> 00:11:12,870 than probably the developers that are less than average. 294 00:11:12,870 --> 00:11:15,110 I need you to do these evaluation 295 00:11:15,110 --> 00:11:16,710 for yourself, for your team, 296 00:11:16,710 --> 00:11:20,630 and when you're hiring, this is really important stuff. 297 00:11:20,630 --> 00:11:21,830 But that's subjective right? 298 00:11:21,830 --> 00:11:22,890 And it's based on the team, 299 00:11:22,890 --> 00:11:24,600 and it's based on the software you're building. 300 00:11:24,600 --> 00:11:26,520 And you always wanna make sure 301 00:11:26,520 --> 00:11:28,120 that team dynamic is... 302 00:11:28,120 --> 00:11:30,330 Everybody knows who they are, 303 00:11:30,330 --> 00:11:31,880 what their responsibilities are, 304 00:11:31,880 --> 00:11:34,170 the codebase is clean and healthy 305 00:11:34,170 --> 00:11:35,550 with those mental models. 306 00:11:35,550 --> 00:11:36,740 But that's subjective. 307 00:11:36,740 --> 00:11:39,120 Let's talk about the piece that's not subjective. 308 00:11:39,120 --> 00:11:40,960 What readability also means 309 00:11:40,960 --> 00:11:45,300 is not hiding the cost of the code you're writing, 310 00:11:45,300 --> 00:11:47,920 not hiding the cost. 311 00:11:47,920 --> 00:11:50,510 Now, this one is hard for me to explain. 312 00:11:50,510 --> 00:11:52,850 So, what I'm gonna use is a piece of code, 313 00:11:52,850 --> 00:11:54,430 it's written in C++ 314 00:11:54,430 --> 00:11:56,970 to show you what I mean about not hiding cost. 315 00:11:56,970 --> 00:12:00,000 And even if you've never seen C++ before, 316 00:12:00,000 --> 00:12:02,900 I promise you, you'll be able to read this code. 317 00:12:02,900 --> 00:12:07,023 So, let's take a look at this code that's written in C++. 318 00:12:08,030 --> 00:12:12,290 What you see here on line seven is a type named foo. 319 00:12:12,290 --> 00:12:15,950 Now, what's special about foo is that it has these features 320 00:12:15,950 --> 00:12:18,040 that you find in object-oriented programming languages 321 00:12:18,040 --> 00:12:19,230 like C++. 322 00:12:19,230 --> 00:12:20,760 On line eight there's a constructor, 323 00:12:20,760 --> 00:12:22,710 on line nine there's the copy constructor, 324 00:12:22,710 --> 00:12:24,477 the move constructor, the destructor. 325 00:12:24,477 --> 00:12:27,220 And if you're an object or a programming language developer, 326 00:12:27,220 --> 00:12:28,890 you know what these do right? 327 00:12:28,890 --> 00:12:29,920 Constructors get called 328 00:12:29,920 --> 00:12:32,760 when a value is constructed, destructed. 329 00:12:32,760 --> 00:12:34,920 If you're making a copy, if you're moving something, 330 00:12:34,920 --> 00:12:36,530 we call these features, 331 00:12:36,530 --> 00:12:39,470 because we're able to kind of tuck away code 332 00:12:39,470 --> 00:12:41,410 when these things happen in our codebase. 333 00:12:41,410 --> 00:12:43,520 And you can also see on line 13, 334 00:12:43,520 --> 00:12:46,750 I've operator, overloaded the assignment, 335 00:12:46,750 --> 00:12:49,450 which means anytime we use the assignment operator 336 00:12:49,450 --> 00:12:51,560 against the value of type foo, 337 00:12:51,560 --> 00:12:54,570 this code is gonna get called, we call these features. 338 00:12:54,570 --> 00:12:56,520 Now look on line 16. 339 00:12:56,520 --> 00:13:01,460 On line 16, what we have is a function named f1, 340 00:13:01,460 --> 00:13:04,930 and f1 creates an object of type foo, 341 00:13:04,930 --> 00:13:06,800 makes a copy of it on the return 342 00:13:06,800 --> 00:13:08,630 and brings that copy back out. 343 00:13:08,630 --> 00:13:12,680 The caller gets a copy, it's own copy 344 00:13:12,680 --> 00:13:16,770 of the foo object being created on line 17. 345 00:13:16,770 --> 00:13:19,020 Brilliant, I've shown you 18 lines of code 346 00:13:19,020 --> 00:13:21,430 I haven't hidden any of it from you, 347 00:13:21,430 --> 00:13:24,000 and this is what I wanna ask. 348 00:13:24,000 --> 00:13:26,423 Look on line 23 and 27. 349 00:13:27,620 --> 00:13:29,440 This is super interesting. 350 00:13:29,440 --> 00:13:32,570 On line 23, we're calling the f1 function 351 00:13:32,570 --> 00:13:34,160 which creates an object of type foo 352 00:13:34,160 --> 00:13:35,960 and returns a copy of it back out 353 00:13:35,960 --> 00:13:38,920 and it signs it to a local variable, foo one. 354 00:13:38,920 --> 00:13:41,290 On line 27, we call f1 again, 355 00:13:41,290 --> 00:13:43,380 the same function we call again, 356 00:13:43,380 --> 00:13:45,750 reassigning to our foo one variable. 357 00:13:45,750 --> 00:13:47,380 So here's the question. 358 00:13:47,380 --> 00:13:48,990 Who can look at this code right now, 359 00:13:48,990 --> 00:13:50,620 and I've shown you all of it, 360 00:13:50,620 --> 00:13:53,940 but let's just focus on the two lines, they're 23 and 27. 361 00:13:53,940 --> 00:13:56,360 How many objects of type foo get created 362 00:13:56,360 --> 00:13:58,910 by the time this program runs and completes? 363 00:13:58,910 --> 00:14:01,070 Does the copy constructor get called and when? 364 00:14:01,070 --> 00:14:03,617 Does the move copy, move constructor get called and when? 365 00:14:03,617 --> 00:14:06,950 And does the assignment operator ever get called and when? 366 00:14:06,950 --> 00:14:10,080 Here's the reality is that you have no idea 367 00:14:10,080 --> 00:14:12,480 by looking on line 23 and 27 368 00:14:12,480 --> 00:14:15,140 what the full scope of behavior is on that call. 369 00:14:15,140 --> 00:14:16,740 And by the way, you don't ask me 370 00:14:16,740 --> 00:14:18,850 what version of C++ we're using, 371 00:14:18,850 --> 00:14:22,440 the behavior changes, the semantics change, 372 00:14:22,440 --> 00:14:25,820 also depending on what version of the language you're using. 373 00:14:25,820 --> 00:14:27,740 This is a nightmare. 374 00:14:27,740 --> 00:14:30,580 The fact that I cannot look on line 23 375 00:14:30,580 --> 00:14:33,370 and get an understanding of how that code's gonna behave, 376 00:14:33,370 --> 00:14:34,780 how it's going to perform on the machine, 377 00:14:34,780 --> 00:14:37,130 and what my costs are is a nightmare. 378 00:14:37,130 --> 00:14:40,130 This is why Go doesn't have constructors and destructors, 379 00:14:40,130 --> 00:14:43,050 and these features operate over features 380 00:14:43,050 --> 00:14:46,100 because they hide cost, they don't help us. 381 00:14:46,100 --> 00:14:47,450 Now here's the reality. 382 00:14:47,450 --> 00:14:51,730 When I run this code on the latest version of C++, 383 00:14:51,730 --> 00:14:55,250 you're gonna see that the first call to foo 384 00:14:55,250 --> 00:14:57,490 optimizes the copy away. 385 00:14:57,490 --> 00:15:01,310 On line 23, the optimization by the compiler 386 00:15:01,310 --> 00:15:03,030 says, we don't need to make a copy, 387 00:15:03,030 --> 00:15:04,050 we're creating this object 388 00:15:04,050 --> 00:15:05,600 for the first time won't reassign it. 389 00:15:05,600 --> 00:15:07,920 But on the next call, line 27, 390 00:15:07,920 --> 00:15:12,130 even though calling f1 again and reassigning it, 391 00:15:12,130 --> 00:15:16,500 now constructors, assignment overloads, copy constructors, 392 00:15:16,500 --> 00:15:18,150 two objects were made. 393 00:15:18,150 --> 00:15:20,750 Oh my God, the cost difference 394 00:15:20,750 --> 00:15:23,500 on line 23 to 27 is great. 395 00:15:23,500 --> 00:15:25,630 We cannot write code like this, 396 00:15:25,630 --> 00:15:27,510 these features hide cost. 397 00:15:27,510 --> 00:15:30,070 Go doesn't have it because Go doesn't want you to hide cost. 398 00:15:30,070 --> 00:15:33,340 And when I see code that's hiding the cost or the impact, 399 00:15:33,340 --> 00:15:35,240 it's gonna be having on the machine. 400 00:15:35,240 --> 00:15:37,310 Guess what, we have to fix it, 401 00:15:37,310 --> 00:15:39,120 we have to walk away from it. 402 00:15:39,120 --> 00:15:41,113 So Go doesn't have those features. 403 00:15:42,260 --> 00:15:46,630 And it's very very important that we remember 404 00:15:46,630 --> 00:15:49,970 that we are focusing on the real machine, 405 00:15:49,970 --> 00:15:51,890 that way we get to be able to write code 406 00:15:51,890 --> 00:15:53,760 that doesn't hide the cost, 407 00:15:53,760 --> 00:15:56,070 and we give that developer at every moment in time 408 00:15:56,070 --> 00:15:58,260 the ability to understand the cost they're taking 409 00:15:58,260 --> 00:16:00,610 and the impact it's going to have. 410 00:16:00,610 --> 00:16:02,860 And I love this quote from Peter Bourgon, 411 00:16:02,860 --> 00:16:03,720 and he's another gentleman 412 00:16:03,720 --> 00:16:07,490 whose big part of the Go community, a thought leader, 413 00:16:07,490 --> 00:16:08,910 somebody should be looking up, 414 00:16:08,910 --> 00:16:10,540 somebody should be watching talks from. 415 00:16:10,540 --> 00:16:13,780 Making things easy to do is a false economy. 416 00:16:13,780 --> 00:16:17,210 Focus on making things easy to understand, 417 00:16:17,210 --> 00:16:18,940 and the rest will follow. 418 00:16:18,940 --> 00:16:21,690 Now simplicity comes after readability. 419 00:16:21,690 --> 00:16:23,800 Integrity, readability and simplicity. 420 00:16:23,800 --> 00:16:25,240 Simplicity is interesting 421 00:16:25,240 --> 00:16:27,680 because what simplicity says is, 422 00:16:27,680 --> 00:16:29,560 that we wanna hide complexity. 423 00:16:29,560 --> 00:16:33,780 Remember readability is about hiding, 424 00:16:33,780 --> 00:16:35,200 not hiding cost. 425 00:16:35,200 --> 00:16:37,300 Simplicity's about hiding complexity, 426 00:16:37,300 --> 00:16:39,730 and our encapsulations right? 427 00:16:39,730 --> 00:16:43,100 Our functions and our methods which are trying to read, 428 00:16:43,100 --> 00:16:44,380 reduce the boilerplate code, 429 00:16:44,380 --> 00:16:46,220 is trying to simplify things 430 00:16:46,220 --> 00:16:50,100 if those encapsulations cause our ability 431 00:16:50,100 --> 00:16:52,550 no longer understand the impact our code is having, 432 00:16:52,550 --> 00:16:53,690 we can't have it. 433 00:16:53,690 --> 00:16:55,840 Simplicity is not something you do day one, 434 00:16:55,840 --> 00:16:58,910 it is something that you refactor into. 435 00:16:58,910 --> 00:17:00,450 And one of the biggest things 436 00:17:00,450 --> 00:17:04,380 that I can tell you about simplicity is what Dijkstra said. 437 00:17:04,380 --> 00:17:05,920 You know, it's a great virtue, 438 00:17:05,920 --> 00:17:08,130 it requires hard work to achieve, 439 00:17:08,130 --> 00:17:10,230 education to appreciate it. 440 00:17:10,230 --> 00:17:12,150 But complexity sells. 441 00:17:12,150 --> 00:17:14,010 Well you got to get away from that idea 442 00:17:14,010 --> 00:17:15,370 that complexity sells, 443 00:17:15,370 --> 00:17:18,160 and we've got to refactor into simplicity as we go. 444 00:17:18,160 --> 00:17:20,220 Go is an amazing language 445 00:17:20,220 --> 00:17:22,780 as it comes to simplicity engineering. 446 00:17:22,780 --> 00:17:25,960 I'm gonna try to point those things out to you as we go. 447 00:17:25,960 --> 00:17:29,000 But there's two more big parts of simplicity here quotes 448 00:17:29,000 --> 00:17:30,000 that I wanna say again. 449 00:17:30,000 --> 00:17:32,180 Again we're trying to hide complexity, 450 00:17:32,180 --> 00:17:35,120 but we're gonna focus on this in the class really well. 451 00:17:35,120 --> 00:17:36,710 And Dijkstra said it again, 452 00:17:36,710 --> 00:17:39,630 the purpose of abstraction is not to be vague, 453 00:17:39,630 --> 00:17:42,180 but to create a new semantic level 454 00:17:42,180 --> 00:17:44,830 where one can be absolutely precise. 455 00:17:44,830 --> 00:17:48,420 We're gonna talk about at some point, precise APIs, 456 00:17:48,420 --> 00:17:50,760 and how powerful they are in a language like Go. 457 00:17:50,760 --> 00:17:52,220 Where we have a compiler, 458 00:17:52,220 --> 00:17:53,760 and have the ability to find 459 00:17:53,760 --> 00:17:55,750 and identify bugs at compile time 460 00:17:55,750 --> 00:17:57,630 as a opposed to production. 461 00:17:57,630 --> 00:18:00,170 And I'm gonna be asking you to kind of throw away 462 00:18:00,170 --> 00:18:03,950 some of the API design that you've been taught around, 463 00:18:03,950 --> 00:18:05,680 object-oriented programming here, 464 00:18:05,680 --> 00:18:08,880 and really focus on the cost that that has 465 00:18:08,880 --> 00:18:13,860 over the benefits of this idea of precise API design. 466 00:18:13,860 --> 00:18:18,860 And Joe Beda whose company called Heptio, great company, 467 00:18:19,180 --> 00:18:22,050 said this, and I love this also around simplicity, 468 00:18:22,050 --> 00:18:24,880 computing is all about abstractions. 469 00:18:24,880 --> 00:18:27,020 Those below are just details, 470 00:18:27,020 --> 00:18:31,090 and those above are limiting complicated crazy town. 471 00:18:31,090 --> 00:18:33,320 So anytime I look at a function or a method, 472 00:18:33,320 --> 00:18:35,780 and I'm seeing that this is an abstraction, right? 473 00:18:35,780 --> 00:18:37,200 We're seeing encapsulation, 474 00:18:37,200 --> 00:18:38,940 I wanna make sure that these encapsulations 475 00:18:38,940 --> 00:18:40,290 are not just testable, 476 00:18:40,290 --> 00:18:42,210 I wanna make sure that they're providing 477 00:18:42,210 --> 00:18:46,800 a new semantic precise level of understanding 478 00:18:46,800 --> 00:18:49,880 that is not hiding cost okay? 479 00:18:49,880 --> 00:18:51,543 But hiding complexity.