YTread Logo
YTread Logo

Clean Code - Uncle Bob / Lesson 3

May 31, 2021
rest of the world, the other ear is fine, yeah, I mean, what do you mean you're sending? This is the first thing we have to do. got over it and realized that the phrase is a bit of an exaggeration, but I want you to think about what has happened over the last 30 years. Do you realize that people download beta versions regularly now? Do you understand what a beta test is? is when you carefully select some trusted users and tell those users that all hell will break loose when you use this

code

. Please be careful, but it would really help us if you used this

code

to give us feedback and now of course we only configure people. download them, yes it's beta, hard, hard, bananas, guys, and you know, we never upload anything, but the bait is that there will be betas from now on, this is the attitude of people who are uploading code to the Internet Do you think That code must have errors?
clean code   uncle bob lesson 3
Be very careful, since you know you are doing it right. You know you think the coat must have bugs. I mean, when I write code, it has bugs. All code must have errors. I don't want my expectations. I have to say that my expectation is that when you release the code you know it works, you know it works, don't guess it works, has anyone done the guessing game? I think it works, the deadlines are approaching, should we send it? Oh, you know, I think. it works, i mean it worked yesterday on my laptop, when you post the code.
clean code   uncle bob lesson 3

More Interesting Facts About,

clean code uncle bob lesson 3...

I hope you know that it works, you will know it to the best of your ability to know that it may not be a perfect skill, but I want it to be the best of your ability to know that it works when you publish the code. I hope it is the highest quality code possible within the time frame needed. I don't want you to just post things you think work. I don't. I don't want you to release things that you think are okay, it has to be

clean

, it has to be tested, it has to be well organized, it has to have a good solid shell and you know who expects everyone to expect that except you, everyone expects it if I buy a product. from someone I expect a high quality product if I'm using a service from someone I expect a high quality service I don't expect them to come to me and say well, you know, there are always mistakes.
clean code   uncle bob lesson 3
I'm buying a car from someone, right? I hope that car to work I hoped that they had tested me and put me to the test. I expect that car to work and I'm going to complain bitterly if even the smallest thing doesn't work, that's what all our customers expect from us. I hope it will always be ready now, what does this mean? who is doing agile look at the hands go up all of you are doing agile I want to bet you have an iteration size the duration of the sprint what is the duration of your sprint two weeks who has two weeks anyone has more time who has more than two three three who has more than three who has less than two anyone has one a week a week what do you have back there a week a week that's good the programmers of the mercury space capsule the avionics software in mercury space the capsule had a duration of one-day iteration, they wrote their unit test in the morning and ran them in the afternoon.
clean code   uncle bob lesson 3
We are now happy with a two-week iteration duration. Well, two weeks probably isn't that bad. I really like a week. can go wrong in two weeks, so I like one week, but okay, two weeks, that means I want to be ready to deploy every two weeks. What do I mean by ready to deploy? I mean from your point of view from a technical point of view. From the point of view, the system is ready to deploy, it may not have enough features to deploy, it is a business decision from a technical point of view, it is ready to deploy every two weeks, okay, you can log out, but You can't log in, but you can log out. works fine and you're ready to deploy if the company comes to you and says, "Hey, can we deploy it?", you say yes, you can't log in, you can log out, but deploy it because it will work, that's what I want and what it does. means ready to deploy means all testing is done but all documentation is done all for now said if masters are done every two weeks now if you are doing agile you already know this because what is supposed to be done at the end of the sprint, what should be the system states at the end of each sprint? if you are doing it with agile if you have been studying scrum, deliverable, deliverable, deliverable, that is exactly right, if it is not deployable at the end of each sprint you are not doing something agile you are doing something else non-agile and agile you are ready to deploy at end of each sprint and that's not too big of an expectation you should be able to expect you should be able to expect that you will work for two weeks and at the end of those two weeks we will have a system that is ready to deploy even if it is still feature-light, okay , does anyone here have a period of operation, a period of stabilization, do you know what it is?
I already have the release, the code is ready for release. QA says it works and everything, and then you put it in a system and you turn that system on, but it's not connected to anyone and then you watch it for a long time. The time you don't smoke seems to still work. You know, maybe if we let it run like this for another month we could ship it. This is irresponsible. The time run here does not tell you that the system is stable. You should already know that the system is. stable, you should have a set of tests that show you that the system is stable, that small period of operation is a false hope, oh if it works for a month then it's probably fine, no it's not, it should be ready every two weeks or one week.
It would be even better. I expect stable productivity. What does that mean? It means that the longer the project lasts, the slower it doesn't get. It does not slow down as the system ages. It can produce functions at the same rate and at the same time. at the beginning of the project and at the end of the project and in the middle of the project the feature production rate does not decrease you are stable II productive this is a reasonable thing to expect the rest of the world expects this to add new features to a The system should not make you slow down.
The reason we slow down and I told you this before is because we make a mess and as the mess increases we become slower and slower. I don't expect disaster. I don't expect that. we're going to get slower and slower as we go how slow can you get yeah I see the right answer pretty slow really fucking so I worked for a company once and the minimum estimate for any thing didn't matter what it was. It was six months, James, the name of a menu item, six months and there was a good reason for this and the reason was that the programmers had been so handicapped by the missing estimates and the cost of confusing the estimates for them personally and professionally was so high that they just rejected any estimate less than six months, they just wouldn't do it, plus the system was so poorly coupled that changing the name of a menu item could break something somewhere because we actually had other systems that read our menus, yes, we talk about that. a mass and then everything that happened in that system was six months.
I hope for economic adaptability. It should be cheap and easy to make changes to the system. Economic adaptability. It shouldn't be expensive for me to make a change or rather I should say this. the cost of the change should be proportional to the extent of the change if it's a big change it will probably cost a lot if it's a small change it shouldn't cost much that's what I expect. I don't expect you to say when I propose a small change to a client. I don't expect you to say Oh God, that destroys all our architecture, ruins all our design.
He doesn't understand that we are going to have to redirect all communications in the system to do this. Don't expect to hear why software is a compound word the first word is soft the second word is where the word where means product the word soft means soft changeable software means changeable product software was invented so that we could easily change the behavior of our machines if We would not have wanted to change the behavior of our machines, we would have stayed with the hardware, but we did not stay with the hardware, we wanted our machines to be easily modified and that is why we invented the software to the extent that its software is difficult to change, it just has reinvented hardware you have frustrated the very reason why software exists if someone comes to me and says that changing the requirements ruins our architecture my answer is well your architecture socks because you have frustrated the very reason why we invented software in First of all your software must be modifiable.
I hope for economic adaptability. How do you get modifiable software? Well, you keep it perfectly

clean

for thing number two. You better have a really good test suite because when you change one part of the code, I don't expect another part to do the same. break I want you to be able to change the code at will. I hope well, that is an expectation that comes. Continuous improvement. I expect continuous improvement of what part of the code system the code should get better over time, not worse. The code shouldn't. degrade or rot code should improve over time because we are human beings that's what human beings do human beings improve situations over time they don't degrade situations over time humans are not bacteria, we don't rock things , we improve things but most of the time our code rots.
I don't expect the code to improve over time. Every day the code improves a little. I hope the design of the systems improves over time. The architecture of the systems. improve over time because I hope that there is a group of highly competent people working within that system continually improving it, that is a reasonable expectation for me and it is the expectation that the entire world has of us now. I hope fearless competition fearless competition you are looking at your screen there is a code on your screen someone else wrote it when this code appears on your screen the blood comes out of your face oh god this code is a mess and you think briefly in your mind the thought wanders into maybe maybe I should clean it up and the next thought is immediately quashed.
I'm not going to touch it because you know that if you touch it you will break it and if you break it it becomes yours so you leave. I won't be the one to clean. this code, someone else can clean up this code, but I propose to you that if that is your reaction to the code, the only thing that can happen to that code is that it rots because you will change it from time to time, but you will. change it in a way that minimizes your personal risk, not to improve the overall structure of the software, you will change it to minimize your personal risk and that will always be the wrong choice for the system, the system will just rot and no one will ever clean it up.
This will get worse and worse over time and the entire team will slow down by a factor of 10 or 20 over a two-year period. I don't expect fearless competition. I don't wait for fear. I don't expect I want you to be afraid of the code. Are you afraid to touch the code? Are you afraid to improve the code? Do you have that little echo in your ear that says Ava isn't broken? Don't fix it. I hope you improve the code. time fearless competition how do you get confidence without fear likes what if I had a button, a little button that I could press on my keyboard and some lights would flash and sci-fi sounds would come out of my laptop for a few minutes and then a little bit ?
The green light came on and that green light told me that the system was working and I believed it given that little light and I believed it I looked at my screen it's a mess I think oh I should clean it and my next thought is I think I'll change the name of that variable, press the button , oh yeah, green, well that feature is a little big. I think I'll split it in half, press the button. Oh, green, well, I didn't break it, okay, I'm going to move this function. to that class over there press the red whoo button put it again press the green button well oh oh oh I see put it here press the green button if you had that button and if you believed it you could clean the code you would clean the code, it would be trivial to clean the code, you wouldn't have to spend a lot of time on it either, you could just say, well, you know, don't spend five minutes cleaning to go a little, press the button a few times and do a little cleaning log it a little better than before and everyone could do the same little dance record it a little better than before we call this the Boy Scout rule record it a little better than You discovered that you always check the code a little better than you would if you had the evidence and if you think that if you react with fear you will you become fearfully incompetent if you conquer fear with testing you become fearlessly competent you maintain control over the code I want you to think about how wildly irresponsible it is to fear what you've created, respond to it, react to it, and allow it to dominate you instead. that you dominate it and it would be sodifficult to do that set of tests, yes.
Do I want you to write unit tests for code that other people have written first? I want you to write unit tests for the code that you have written. Conquer that problem first. Hey, now the easy part is who's doing that right now testing each line. of the code that they write, okay, there are not many people, so this is not the easy part, let's do that first and then we can talk about the code that other people write and I want you to write unit tests for that, go ahead. of course. I want you to write tests for each part of the code.
I want the code to be tested. I want you to have that button. I have that button. The systems I write. I have that button. I can press that button. and I have absolute control over that code. I can do anything to that code that I want and I can do it without fear because I know that step by step I haven't broken anything when you experience that for the first time. My socks off you mean I can change this is a remarkable experience there will be more to say about that in the next hour but yeah go ahead yeah yeah broken unit tests that are absolutely useless yeah it's a question so the question It was him.
We've seen over the years that unit tests can be created so tightly coupled to the system that if something in the system changes, a bunch of tests break. This is called the brittle evidence problem. The brittle testing problem is a problem experienced by people who If you are new to test-driven development, if you have only been doing test-driven development for a year or two, there is a good chance that you will encounter the brittle testing problem. tests and reason. What you find is that you have not yet realized that tests are part of the system and must be designed as part of the system; they can't just be thrown in there, since a bunch of tests are an integral part of the system that must be designed with the same rules that we have always used to design any part of the system, so let me explain what I mean by that, if there is a part of the system here and another part here, they are not tests.
They're just parts of the system and you make a change here and it breaks a thousand things here. What can you say about the design of that system? It's about conversations, yes, it's terrible design, which is the definition of bad design, by the definition of bad. design is when you make a change in one place and a thousand things break in another good place, now turn this into tests, you make a change there and a thousand tests break, you have a bad design, how do you fix that? Well, you design the test. you isolate the tests from the system, you decouple the tests from the system, you build APIs for the tests, you use the same techniques you use for normal system design for the tests, and you end up with tests that don't fail that way.
I'm not going to go any further, although you told me a very scary thing and I'm going to dwell on that for a moment, is anyone measuring code coverage? Oh yeah, well do you have a target number? What is the target number 85? So it's okay if 15 percent of this code doesn't work correctly, there's no target number that makes any sense other than one hundred percent correct, you can't arrest it. You know we have 80 percent code coverage, so I guess 20 percent doesn't work. so the only goal that makes any sense is one hundred percent and you can't achieve it, you can't get to one hundred percent, so you have to see it as an asymptotic goal, a goal that you keep in the tribe, it's like losing weight.
You know, I'll keep trying, sometimes I lose a pound or two, so it's a lot like you keep working on reducing code coverage, but you'll never get to 100%, number two, don't let the managers see that. number never is not a management metric managers don't understand what the metric means code coverage means nothing outside of the context of the team the team understands what it means the team knows that those are the lines of code that are executed and the team understands how many assertions are in the tests if you make this a management metric and by god it will help you if you actually put it in the build and you fail the build, if you don't meet the number then what happens is the Programmers to make the number will start pulling assertions out of the tests and the tests will stop making sense and the code coverage number will shoot through the sky.
That way you can get one hundred percent, don't do that, this is not a management metric. it's a team metric it's a personal introspection metric you get code coverage you're working on the team gets code coverage they're working on they look at it privately evaluate what it means in the context of the system and act accordingly , but they do not publish the number and it is not a goal. Sorry, that was a complaint. I hope we don't do quality control. Okay, let me get that off the screen. This is disgusting. I hope we don't. QA QA is not the group that finds bugs.
Did I miss one? Well, the same quality is fine, they are all part of the same. I hope we don't dump them into QA QA is not the tool we are going to use to find. errors in our system QA should not find anything Do you have a QA group? Do you use quality control? Who uses quality control? Oh, some people do it right. Why do we have quality control departments? What would motivate a company to invest in a separate group whose job it is to detect if the developers hadn't done their job apparently the developers hadn't done their job, but why would a company create a QA group well because they got burned? so much for not having one?
Why did they get so burned because the developers didn't? They don't do their job, it's the developers' jobs to make sure the system works. QA can serve a useful function, that's fine, but the role they must serve is not at the end of the process waiting for the load to be delivered, the function of QA is to specify how the system should behave. QA people have been telling us this for decades. Nobody pays attention to him. We don't want a group of people behind writing. You know, running tests. Running tests as if they were computers. I want to get a bunch of people back there, you know, has anyone looked at a manual test plan?
Do you know what kind of hell that is? And she X Y Z Z Y in the password field, click OK, did the login called screen appear? Check here yeah this is your job for the rest of your life right that's horrible and by the way what about quality control? Do you think the beast really delivers the cargo on time? Do you think the developers give the QA people enough time to test the system? No, do you think the date changes? just because QA doesn't have enough time to run all their tests, no, who is under more pressure in this environment? which team is under the most stress, the one that has the most pressure to do something and it's always the QA that's always sitting at the end.
Everyone is always under infernal pressure and in that context we are going to guarantee quality. It's stupid. Quality control does not belong at the end of the process. Qi belongs to the beginning of the process. Developers guarantee the quality of their code, not quality control. Quality control should. they don't find anything the QA tests shouldn't find anything you run release a system you hand it over to QA you hope they don't find anything if they find something your reaction should be how the hell did that get loose how the hell did that error happen there we have to do something to prevent that again because we never want QA to find that QA should wonder why the hell they are stuck at the end of the process because they want to go to the beginning of the process anyway and how is it done.
Well, I hope test automation. Every test in the system must be automated. Every QA test should be automated. Yes a computer can do it. A computer should do it. Yes it is the manual test. If you have a batch of manual tests, I hope they do this. be automated because look what programmers, that's what we do, we automate things, so it would be very expensive to automate testing. Know that it is expensive to run the test. See this image here. Hands. There are the hands of the quality control manager. The years 2008. You are giving me the index of your manual testing plan.
You have 80,000 manual tests that you need to run once every six months. He sends them to an army of testers in India. It costs a million dollars every six months to run them. QA test suite he is offering it to me because it is the year 2008 the financial crisis has become his boss he just came back from the CFO's office telling him that his budget is being cut by 50% he is offering it to me asking me which You shouldn't run half of these tests. I told him that he can cut the document this way. You won't know if half your system works.
This is the inevitable result of manual testing. Manual testing always leads to losing tests. you will leave or loot, or you will lose them this way, in the most obvious way when some accountant finally says this is too expensive, or you will lose them in a much more insidious way and the insidious way is that the quality control group does not has enough. It's time to finish your tests because the developers are late and the QA group now has to determine which tests to run. I see myself on that screen. I didn't expect to see myself on that screen.
Oh boy, quality control is hot on my tail. Grove, I'm distracted now, the QA group has to decide which tests to run to cover the changes, so they look at the changes that were made and then guess which tests to run, do an impact analysis and discard. the rest of the tests, if the programmers knew they were doing that, the programmers would tap them on the shoulder and say, "you really need to run all the tests because you know we change virtually every line of code in the system in one way or another." ". you are doing manual testing, you will lose those tests.
There is a disease that infects software development organizations and it is very interesting how do you know if QA is doing a good job if you are managing the QA team, how do you know? They are doing a good job, what metric could you use? Could you what? Oh yes, getting complaints from end users. Yes, use defects. The number of defects that passed through QA or regression testing time. How long does it take to do regression testing? Well, if it is manual, it will take a lot of time. Quality control should find defects. If QA finds a lot of defects, they must be doing a good job.
This gives a positive value to the defects. Now someone appreciates the defects that I would like to have. defects because now they can announce that they found them and that's good for them now, who else wants defects? There is an old rule in programming. I can meet any schedule you set for me if the code doesn't have to work, name the day. I'll be there now, that's interesting, because that implies that if there are enough defects I can meet with the programmer programmers who are under programming pressure, they love defects, especially if they can move them to QA and QA loves defects, then they can say "hey, look." In the face of the defects we find, no one has to say a word, but the economy of defects is established.
Both sides love flaws and no one knows this exchange is going on. I hope, as your new CTO, that we have each other's back and behave as a team, how does a team behave? you have a team of people there moving a ball across the field and there, as they pass the ball back and forth and move it across the field, one of the players trips and falls without a flag builds up the play the play continues what do they do the other players cover the hole they change their position on the field they keep the ball moving around the field this is how a team behaves the teams cover each other when a player goes down if you are on a software team they should be able to cover each other if someone fails and people fail on software teams all the time if someone fails someone else needs to be able to take on that role, how do you make sure someone can cover?
For you it is your responsibility if you fall, someone should be able to cover you, how do you make sure someone can cover you well? It would be helpful if you sat them down at their terminal in front of their screen and walked them through what you were doing, you could do this by pairing. I could pair again, right? You can do this by stopping. This is why we want to see people matching, we want people to match so we know they can cover. It's your responsibility to make sure someone can cover them, so you'll invite people to join you so they can learn what your source files look like and where you keep them, what your build scripts are and how you handle them. git and all that so that if you go down someone can step in and say yes, it'sWell, I can take care of it.
Has anyone worked on a team where Bob was the database guy and Bob went down and nothing happened to the database for a month? Oh, I don't know anything about the database, that's Bob, this is stupid, right, we are a group of programmers, we know how to do these things, we just have to cover each other, we have to behave as a team, the teams They cover each other. I'm hoping that if I have a team of 15 people and I decide I need to split that team in half and take seven people that way and eight people that way, the remaining eight people will still be able to operate because they know enough about the entire system to continue. behaving as a team, that means you'll have to share a lot of knowledge, probably through pairing, so let's talk a little about this pairing thing.
People are very afraid of pair programming because they think. They don't want to sit next to a guy forever and I would be horrible and what if he has bad breath or something? It's always a good idea to have some mints in your pocket just in case, but that's okay, I don't do that. I want them to pair 100% of the time I don't need one in a pair 50% of the time I want them to pair enough so they can cover each other and can review each other's code how long should you pair an hour just half an hour something like that? couple, you know, a couple of times a week a couple for an hour hour and a half something like that maybe a couple of times a week enough to have enough knowledge to cover each other and they're reviewing each other's code.
I also think you should be paired with a lot of people, so people should be paired on the work they are doing and you should be paired on the work other people are doing. doing on a relatively regular basis and all of this should be very informal. I don't want a manager to put a big chart on the wall saying that today Bob is partnering with Bill, that's stupid, everyone should just be able to strum time for time, say, you know what I'd like? I'd like to work with you for half an hour every hour and we can learn what each other is doing, that's a smart thing for a team to do and if you did that you could probably eliminate most of the code reviews you do because pairing It's a much better way to review code, so keep this in mind.
I expect honest estimates. What is the most honest estimate? You can tell me three little words. I don't know what the most honest estimate you have is because you don't know well. Anyone who claims to know how long something is going to take is lying, and by the way, if you estimate a date, you're lying if someone comes up to you and says, can you? You finish this on Tuesday and say yes, you're lying, you don't know you can do it on Tuesday unless you're absolutely sure for some reason, maybe it's a one line change and it's eight weeks until Tuesday, that would be an interesting week, No?
Hmm, so how do we deal with this? Because the answer I don't know is the correct answer, but it doesn't contain enough information. Managers are going to need better than that because they have to be able to manage. You know, managers have to manage things. So what can we do right? What can we do about it? We can define? the way we don't know, we can put a limit on what we don't know and doing so is actually very simple. This is an old idea that dates back to the 1950s. It was called real-time project evaluation. it was used in the Minuteman missile system it was very successful old idea great idea and the estimation part works like this you don't give an estimate of one number you give an estimate of three numbers the first number is the best case someone says Okay, here There's this task and you have to estimate it and you say, Well, okay, if everything goes well and I mean everything you know, eat the right breakfast cereal every morning, every day, you go to work, your co-workers work are really polite to you, right? watch the right TV shows at night you sleep a full eight hours every night that's all good everything's going well it'll take me a week then the next number is the nominal case this is when things go as they normally go well if things continue as They usually go.
I'm going to spend half my time in meetings and arguing with Mike's workers and my wife and I will fight half the time and it won't be useful to code for a few days and it'll probably take me three weeks and then the last issue is worst case scenario worst case scenario is anything but nuclear war right, everything goes wrong right, there are production outages every day and it takes you all day to fix it and your coworkers are rude and throwing balls at you of saliva and half of them are sick and you have to deal with all their crap anyway and you say, okay, if that happens, it will be eight weeks, one week, three weeks, eight weeks, that's a really honest answer, It's probably Right now no manager wants to hear that, by the way, just management is a number, give me a number.
No I can not. I can give you three. No no. I want a number. Now I know I can give you three. I can not give you. a number would have to lie and I don't want to lie, I just give you three numbers and then because you're giving them three numbers, the managers have to do something that well, I hope they're used to doing it, they have to do it. manage because the managers job is to manage that type of risk now, of course, they would like to limit that risk as much as possible by having you absorb it, but you shouldn't absorb it if you don't know that you can make it what it is. in hmm, okay, yes, it's 3:15, well, time for a break.

If you have any copyright issue, please Contact