WordCamp US: Clean Code

  • Published on
    15-Apr-2017

  • View
    3.246

  • Download
    0

Embed Size (px)

Transcript

<ul><li><p>December 5, 2015 Wordcamp U.S.</p><p>Clean CodeMichael Toppa @mtoppa toppa.com</p><p>Source</p><p>http://toppa.comhttp://abstrusegoose.com/432</p></li><li><p>www.pokayoke.design@pokayokedesign</p><p>http://www.pokayoke.cohttps://twitter.com/pokayokedesign</p></li><li><p>Clean code</p><p>That sounds nice, but I have a deadline</p><p>I don't have time for clean code!</p></li><li><p>* The more we rush, the more the code turns into a big ball of mud, making it harder and harder to work with.* Keeping the code clean lets us go faster, because clean code is flexible code.* Why is it exactly that trying to go fast ultimately makes us slower?</p></li><li><p>The ratio of time spent reading code versuswriting is well over 10 to 1.</p><p>Therefore, making code easy to readmakes it easier to write.</p><p>paraphrased from Clean Code</p><p>If youre working quick and dirty, youre not writing code that is readable</p></li><li><p>We like to think we spend our time power typing,but we actually spend most of our time</p><p>staring into the abyss.</p><p>- Douglas Crockfordprincipal discoverer of JSON,</p><p>creator of JSLint</p><p>* But were not really aware of how were spending our time.* Going forward, think about the hours you spend debugging, or trying to figure out messy code.* When youre trying to decide whether you should spend a little more time writing code more cleanly, remember an ounce of prevention is worth a pound </p><p>of cure</p></li><li><p>10 ways to make your code clean</p></li><li><p>#1</p><p>Youare responsible for the quality of your code</p><p>* A lot of whats in my talk comes from Bob Martin, who wrote a book called Clean Code. He provides some good analogies.* Its not up to the project manager or your boss - they are almost always asking you to go faster.* What would your doctor say if you told her to skip washing her hands, because youre in a hurry? What would your accountant say if you told him to save </p><p>some time by no doing double-entry bookkeeping?* If we want to be treated as professionals, we need to act like professionals. And part of being a professional is being honest about how long it takes to do </p><p>something well, and not accepting the option to do it wrong.</p></li><li><p>#2 </p><p>Use meaningful </p><p>names</p><p>The name of a variable, function, or class should tell you why it exists, and what it does</p></li><li><p>Not good</p><p>$d; // elapsed time in days</p><p>* Imagine a script full of variable names like this - it would be very difficult to understand.* Mental translating of obscure names to their real meaning distracts from our ability to get an overall understanding of the code* The days when it was important to use short variable names to save memory space are long gone* Automated minification tools can shrink javascript code for you when dealing with code being transmitted over slow connections</p></li><li><p>Good</p><p>$elapsed_time_in_days;$daysSinceCreation;</p><p>$days_since_modification;</p><p>* Grady Booch, the former chief scientist at IBM said that code should read like well written prose.* So you can think of variable names as the nouns and subjects of your code sentences</p></li><li><p>#3 - Write code that expresses intent</p><p>public function set_numeric_thumbnail_size($requested_size = 'xsmall') { if (array_key_exists($requested_size, $this-&gt;thumbnail_sizes_map)) { $this-&gt;numeric_thumbnail_size = $this-&gt;thumbnail_sizes_map[$requested_size]; }</p><p> else { throw New Exception(__('invalid thumbnail size requested', 'shashin')); }</p><p> return $this-&gt;numeric_thumbnail_size;}</p><p>* Now we get to functions, which are our verbs Take a minute to read this.* Even without knowing the class or the properties, it's clear what this method does.* You should use a 21st century IDE, that auto-completes names for you and makes it easy to rename. I use PHP Storm.* What dont you see in this code?</p></li><li><p>#4</p><p>Comments are often lies waiting to happen.</p><p>Code should speak for itself whenever possible</p><p>* The revelation for me in learning clean code techniques is that code can be expressive. That it really can read like well written prose.* Rather than relying on comments to explain your code, the code should explain itself * Comments become something you have to maintain, and if they become outdated and no longer describe the current behavior of the code, they become </p><p>dangerous lies* But they are sometimes necessary - for example, you may need to explain why you did something a certain way* The point is to stop and think before you write a comment, and ask yourself if instead you can make the code more expressive</p></li><li><p>#5</p><p>Source</p><p>* A common occurrence in software projects is bit rot - the code gets messier and buggier over time.* But how about code that just keeps getting cleaner over time? You can get this by following the boy scout rule.* Keep your new code clean, and while youre working on it, spend a few minutes cleaning up the code that it works with. Improve a variable name, break </p><p>up a function thats too big, eliminate a small bit of duplication, etc.* Its easier to keep your garden healthy and looking nice by weeding it for 10min every day than waiting until its a huge overgrown mess.</p><p>http://brendan.enrick.com/post/Boy-Scout-Rule</p></li><li><p>#6</p><p>Source</p><p>* Do one thing, do it well, do it only. But what does it mean to do just one thing?* For functions, this typically means changing the value of only one variable. If you are passing more than 2 or 3 arguments into a function or method, you </p><p>are probably doing more than one thing.* The more arguments you pass to a function, and the longer it is, the harder it is to test and debug when something goes wrong* Many code review tools will warn you if a function is more than 10 lines long.* For classes, it means having a single conceptual responsibility. When projects get big, this is how you keep them manageable. Code review tools will </p><p>typically warn you if classes are more than 200 lines</p><p>http://serkankaraarslan.com/post/123830104675/single-responsibility-principle</p></li><li><p>#7 - Write tests!</p><p>Copyright Lucasfilm Ltd</p></li><li><p>Test driven development (TDD)</p><p>Source</p><p>* Learning TDD completely changed how I code. It forces you to think about how your new code will talk to the existing code first. This leads to better design than just diving in to solve a problem, and only afterwards trying to figure out how all the pieces fit together.</p><p>* Having repeatable tests has also saved me literally hundreds of times, when a change I introduced *here* broke something over *there* without me realizing it.</p><p>* Better to find out from a test that you broke something than from an angry customer.</p><p>http://www.energizedwork.com/weblog/2005/11/how-not-to-do-it-test-driven-development-microsoft-style</p></li><li><p>#8 - Work in short cycles: incremental and iterative</p><p>Source</p><p>* Develop systems in small portions at a time (incremental), through repeated cycles (iterative), write tests to verify functionality, and take advantage of what was learned in each cycle (for both features and implementation).</p><p>* What Im describing is an Agile workflow, which would require a whole other talk to explain in detail* But for now Ill just say that an Agile workflow complements the clean code style.* Work on one thing, do it well, then move on to the next thing.</p><p>http://www.infoq.com/news/2008/01/iterating-and-incrementing</p></li><li><p>#9 - Independent Architecture</p><p>Why am I subjecting you to this awful image?</p></li><li><p>Software architectures are structures thatsupport the use cases of the system...</p><p>Frameworks are tools to be used, not architectures to be conformed to</p><p>- Bob Martin</p><p>http://blog.8thlight.com/uncle-bob/2011/09/30/Screaming-Architecture.html</p><p>* WordPress is essentially a framework, so this could read WordPress is a tool to be used, not an architecture to be conformed to. This is from a blog post called screaming architecture</p><p>* I wrote a plugin for displaying photos from Picasa, Youtube, and others in WordPress. Its files and code scream photo management and display!, with names like PicasaSynchronizer and PhotoDisplayer. It does not scream WordPress plugin. The fact that it is a WordPress plugin is incidental to its design. I use most of the same practices in my Rails work as my WordPress work.</p><p>* What Im actually talking about here is community, and growing our sense of community. The WordPress community is so big, its easy to talk only amongst ourselves. Theres a lot we can learn about maintainable design from other platforms and languages.</p><p>http://blog.8thlight.com/uncle-bob/2011/09/30/Screaming-Architecture.html</p></li><li><p>#10 - Practice, practice, practice</p><p>Source</p><p>* Musicians dont play only when theyre on stage. To get good at something you need time to refine your craft, when youre not under pressure to perform</p><p>* [my working on bus example]</p><p>http://flickrhivemind.net/Tags/blister,fingers/Interesting</p></li><li><p>December 5, 2015 Wordcamp U.S.</p><p>Michael Toppa @mtoppa www.toppa.com</p><p>http://www.toppa.com</p></li></ul>