10 Tips For Clean Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm the CTO Tokyo gay design I'm doing web application development since the days of HTML 1.0 Perl 3 and CGI when web pages were first painted on cave walls and over the years I've learned some things I will try to share with you in the next 10 minutes and that pokeo key design we do work in WordPress we also do work in in Ruby on Rails and those are two very different platforms using two very different languages but there are techniques and approaches that really apply across languages across platforms and really that's what I'm here to talk to you about so that might sound nice clean code but I have a deadline I don't have time for clean code so you know we run into this all the time we're into deadline pressure our project managers our clients are telling us that we've got to get this done by Friday you're working long hours and the common outcome of this is the big ball of mud code the fast and dirty code to get something done so we're asked to go faster but what happens is we actually end up going slower there are more bugs we have to go back and fix them we're finding needles in haystacks getting the work done actually takes longer than if we adjust written clean code to begin with slowly the idea here is a an ounce of prevention is worth a pound of cure so how do we know this there's actually been studies that have done studies that have been done watching actually programmers work how are they spending their time and the ratio of times and reading code versus writing is well over ten to one people are actually spending most of their time scrolling up and down in a file looking for something no it's not they're open another file oh it's not there either what am i where is this thing I'm trying to fix the actual time you spend typing is a lot less than the time you spend just looking around so if you take the time to make your code easy to read you will be make the code you will make the code easier to write this is a great quote from Douglas Crockford some of the people create a JSON he said we like to think we spend our time power typing but we actually spend most of our time staring into the abyss and this is what I was just talking about spend a lot of our time fighting the messy code so with that as an explanation as to why it's important here are 10 quick tips for making your code clean number one you are responsible for the quality of your code not your client not your boss if you think about other professions doctors lawyers accountants they all have professional standards that they adhere to you don't go to the doctor and say you know what doc I'm in alright want to just skip washing your hands you know go to your account and say why don't you just skip the double-entry bookkeeping why are you bothering with that let's let's go you know we have certain respect for people in these professions and respect how they work so if we want to be treated as professionals we have to act like professionals we have to be advocates for the quality of the code that we write and advocate for doing it the right way and not accept the option to do it the wrong way to where a young feel them to me that's very exciting to be part of defining what these professional standards are and it's up to us to create them not our clients not our project managers that's number one number two when you're writing your code use meaningful names so for example with variables this is an example of a variable name that's not very good dollar sign gee what does that mean by itself you've got a little comment it tells you what it does but once you start getting to the code you know looking at all these variables with all these cryptic names the mental translating you have to do going back and forth between some cryptic little symbol and its actual meaning starts to get in your way in the way of actually understanding the work that you're trying to do so here are some better names that actually tell you what the variable does Grady Booch the former chief scientists at IBM said that clean code should read like well-written prose so if you look at variable names like this the variable names nouns in your prose number three we get to our verbs writing code that expresses intent these are our functions our methods if you're objecting only in programming however if you can see the code on the screen merits by a little small but the idea here is that we have method names just like our variable names that clearly explain what it's for and as you read the code you can sort of you know understand what it's doing and I'm not even giving you any context you don't know what class this came from but you can read it and sort of get a sense of what's going on and you can do that without something there's something that's not here and the thing that's not here is comments so this will be kind of a controversial comment on comments but comments are often lies waiting to happen code should speak for itself whenever possible so what I mean by this often we write comments because the code the intention of the code isn't clear so we think okay let's write a comment that will explain it but when you create the comment you've now written something else that you have to maintain in addition to the code and what often happens once people are working on a project they come in they fix a bug they update the code maybe they don't notice the comment now the comments wrong and now it's a lie and it's going to take a future developer down the wrong path and they come down and look at this so I'm not saying never write comments often they are important for example if you're dealing with like a third party API you need to explain some behavior you know you can't necessarily do that in the code so there are good cases for writing comments I'm not saying never use them what I am saying is when you're about to write a comment step back and think for a moment and think you know can I write code that expresses itself a little better and then maybe I don't have to write a comment number five the Boy Scout rule in the case of code when we talk about the Boy Scout rule we mean leave the code better than you found it the Boy Scouts leave the campsite better than you found it so not only am i saying should you write clean code but if you're going in or hang on some existing code to take your new stuff write it well but take a few minutes and look at the surrounding code can I improve a variable name can I break up a method is it that's too big so instead of the common scenario of bit rot where code just gets Messier and Messier over time we can instead have koba gets cleaner including air over time this is possible and I've worked with people who've done it and it's a great way to work number six a single responsibility principle this is a one of five principles coined by ax Bob Martin who actually wrote a book called clean code and many of the ideas my talk come from they're so single responsibility principle this is the idea when we're talking about say a function or method that it does one thing it does it well and it does it only so what do we mean by that a good way to think about it is if you're passing four or five arguments to a function that's probably doing more than one thing if you think about all those variables you're passing in you're now sort of creating a matrix of possible outcomes that can be quite large and it can be hard to debug if there's problems with it because there's so many things going on inside it so that's a good way of getting familiar with this principle when it comes to functions and methods with classes if you're doing object-oriented programming what you're looking for is what's called cohesiveness and the idea there is are most of the methods in the class using most of the properties that all sort of hangs together if you've got one subset of methods using half the properties and another subset of methods using the other half you're probably doing more than one thing in that class maybe it actually needs to be two classes so they each have a clear conceptual responsibility and when you come to work on the coding and it's clear what they're doing when they have one responsibility as opposed to many number seven right if you saw Carl Alexander's talk yesterday he did an excellent presentation on this wood much deeper dive that I can take in 30 seconds but there's really two kinds of tests one type is called integration tests which actually test the functionality you know how a user will experience your code and these have saved me so many times where I've changed something over here and I've run the test and found what do you know it broke something over there and it's much better than I find it with my tests and an angry customer finding it two weeks from now and they're also unit tests which test your functionality and isolation intensive and development is a great way to do this where you first come up with a test and then write your code to pass the test and it leads to better design I am running out of time I'm going to move a little faster I'm going to skip membrane and hit you with number nine independent architecture why am i subjecting you to this awful image this is a quote from Bob Martin says it's software architectures are structures that support the use cases of the system frameworks are tools to be used not architectures to be conformed to so what's he talking about here you can make a wordpress as a framework so you can say WordPress is a tool to be used not an architecture to be conformed to the idea here is that they're good practices that can be applied across different frameworks and languages so for example WordPress plugins that I've written it's very clear from looking at the files and the variable names what functionality this plug-in implements and I could even with a few changes take it outside of WordPress and use it my connections to WordPress aren't any greater than they need to be so the use cases are clear in the plug-in as opposed to so it's not sort of yelling at you that is WordPress plug-in is yelling at you that it's a photo display or something like that number 10 practice practice practice musicians don't only play when they're on stage in front of an audience and really the same thing applies to programming Duke Okada's go to contributor day learn new techniques when you're not performing for client who's paying you and that's a way you can really improve your skills and I am done thank you very much [Applause] [Music]
Info
Channel: Coding Tech
Views: 569,743
Rating: 4.8768783 out of 5
Keywords: coding, programming, software development, javascript, code review, clean code, learn javascript, php, wordpress, coding tech
Id: UjhX2sVf0eg
Channel Id: undefined
Length: 11min 9sec (669 seconds)
Published: Tue Aug 15 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.