Clean Ruby: A Guide to Crafting Better Code for Rubyists
()
About this ebook
Learn how to make better decisions and write cleaner Ruby code. This book shows you how to avoid messy code that is hard to test and which cripples productivity. Author Carleton DiLeo shares hard-learned lessons gained from years of experience across numerous codebases both large and small. Each chapter covers the topics you need to know to make better decisions and optimize your productivity. Many books will tell you how to do something; this book will tell you why you should do it. Start writing code you love.
What You Will Learn
- Build better classes to help promote code reuse
- Improve your decision making and make better, smarter choices
- Identify bad code and fixed it
- Create quality names for all of your variables, classes, and modules
- Write better, concise classes
- Improve the quality of your methods
- Properly use modules
- Clarify your Boolean logic
- See when and how you refactor
- Improve your understanding of TDD and write better tests
This book is written for Ruby developers. There is no need to learn a new language or translate concepts to Ruby.
Related to Clean Ruby
Related ebooks
The New Frontier In Web Api Programming Rating: 0 out of 5 stars0 ratingsBuild Your Own Car Dashboard with a Raspberry Pi: Practical Projects to Build Your Own Smart Car Rating: 0 out of 5 stars0 ratingsBeginning DotNetNuke Skinning and Design Rating: 0 out of 5 stars0 ratingsSwift 5 for Absolute Beginners: Learn to Develop Apps for iOS Rating: 0 out of 5 stars0 ratingsClojure for Java Developers Rating: 0 out of 5 stars0 ratingsDesign Patterns in C#: A Hands-on Guide with Real-world Examples Rating: 0 out of 5 stars0 ratingsModern Programming Made Easy: Using Java, Scala, Groovy, and JavaScript Rating: 0 out of 5 stars0 ratingsLisp (programming language) Complete Self-Assessment Guide Rating: 1 out of 5 stars1/5ModSecurity 2.5 Rating: 0 out of 5 stars0 ratingsPractical Machine Learning with Rust: Creating Intelligent Applications in Rust Rating: 0 out of 5 stars0 ratingsHyperparameter Optimization in Machine Learning: Make Your Machine Learning and Deep Learning Models More Efficient Rating: 0 out of 5 stars0 ratingsVirtual Machines A Complete Guide - 2021 Edition Rating: 0 out of 5 stars0 ratingsLearning Puppet for Windows Server Rating: 0 out of 5 stars0 ratingsImplementing SSL / TLS Using Cryptography and PKI Rating: 0 out of 5 stars0 ratingsApplied Reinforcement Learning with Python: With OpenAI Gym, Tensorflow, and Keras Rating: 0 out of 5 stars0 ratingsReverse Engineering A Complete Guide - 2021 Edition Rating: 0 out of 5 stars0 ratingsThor's Microsoft Security Bible: A Collection of Practical Security Techniques Rating: 0 out of 5 stars0 ratingsJob Ready Go Rating: 0 out of 5 stars0 ratingsMastering Sublime Text Rating: 0 out of 5 stars0 ratingsPattern-Oriented Software Architecture, On Patterns and Pattern Languages Rating: 5 out of 5 stars5/5Beginning Mobile Application Development in the Cloud Rating: 0 out of 5 stars0 ratingsHands-on Booting: Learn the Boot Process of Linux, Windows, and Unix Rating: 0 out of 5 stars0 ratingsPrivileged Attack Vectors: Building Effective Cyber-Defense Strategies to Protect Organizations Rating: 0 out of 5 stars0 ratingsInfoSecurity 2008 Threat Analysis Rating: 0 out of 5 stars0 ratingsWindows to Linux Migration Toolkita: Your Windows to Linux Extreme Makeover Rating: 0 out of 5 stars0 ratingsThe Oracle Hacker's Handbook: Hacking and Defending Oracle Rating: 4 out of 5 stars4/5Summary, Analysis & Review of Gene Kim’s, Jez Humble’s, Patrick Debois’s, & John Willis’s The DevOps Handbook Rating: 0 out of 5 stars0 ratingsSOA Security Rating: 0 out of 5 stars0 ratingsJavaScript Programming: 3 In 1 Security Design, Expressions And Web Development Rating: 0 out of 5 stars0 ratings
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5C# 7.0 All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsLearn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5C# Programming from Zero to Proficiency (Introduction): C# from Zero to Proficiency, #0 Rating: 0 out of 5 stars0 ratingsRaspberry Pi Cookbook for Python Programmers Rating: 0 out of 5 stars0 ratingsPython QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsHTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Linux Command Line and Shell Scripting Bible Rating: 3 out of 5 stars3/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratingsPython Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5C# Programming from Zero to Proficiency (Beginner): C# from Zero to Proficiency, #2 Rating: 0 out of 5 stars0 ratingsHacking: Ultimate Beginner's Guide for Computer Hacking in 2018 and Beyond: Hacking in 2018, #1 Rating: 4 out of 5 stars4/5Mastering Windows PowerShell Scripting Rating: 4 out of 5 stars4/5Python GUI Programming Cookbook - Second Edition Rating: 5 out of 5 stars5/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5
Reviews for Clean Ruby
0 ratings0 reviews
Book preview
Clean Ruby - Carleton DiLeo
© Carleton DiLeo 2019
C. DiLeoClean Rubyhttps://doi.org/10.1007/978-1-4842-5546-9_1
1. The Qualities of Clean Code
Carleton DiLeo¹
(1)
Boulder, CO, USA
So what is clean code? If you ask three different developers, you will get three different answers. It’s a hard concept to define. You know clean code when you see it, as if written by a programming wizard from a distant land. We all want to write good code that others admire. Sometimes we don’t know where to start. How do we take our messy, unorganized code and turn it into something beautiful?
Many factors determine clean code, but we will focus on these:
Readable
Easy to change
Straightforward
Examine code you’ve written. How many of those questions can you answer yes? It’s unlikely you said yes to all of them. It’s more realistic you said no to at least one or more. Even the most experienced developers have a hard time achieving this, so don’t worry. The skills required to create clean code take time and patience to learn. As you learn new techniques, try them in your code and pick the ones that work for you. There is trial and error involved, but this book will be your guide along the way.
Readability
Code readability describes the effort required to understand what a piece of code does. Readable code should be like a well-written book; it doesn’t expect you to dig into the details to discover its purpose. It should be easy to understand a variable’s meaning, or what a method will do when [^^Invoked]. With unreadable code, you must read multiple times before you get the meaning. After reading unreadable code, you have many questions. You will spend more time understanding it than changing it.
Let’s start with a simple code example of code that is not readable:
1 def method1(t, b)
2 c = t + b
3 return c
4 end
Examine the code and ask yourself the following questions:
Is the code easy to understand?
What does this code do?
Several design decisions have made this small method hard to read. The method name is ambiguous, and the variable names are vague. These issues prevent us from understanding what the code is trying to do. This example may be extreme, but finding unreadable code like this in the wild is possible. It’s OK because with a little effort, this code will be so much better.
Before we continue, let’s talk about variables. They are the storage containers of our application, which allow us to keep bits of information in memory for computation. A single application can have thousands of variables, but none should be superfluous. The earlier example has an extra variable, c, which is both nondescript and unnecessary; it decreases the readability of the method because it adds bloat. Code bloat is bad because a developer has to read more code than necessary.
This is how we could rewrite the earlier example:
1 def sum(a, b)
2 a + b
3 end
This example is much easier to read. It does the same thing, but it has a better method name and better variable names that give clues on the code’s intent. The name sum tells the reader it will calculate the sum of its parameters, a and b. The method arguments, a and b, are short, but descriptive enough in this context. We removed the c variable and instead returned the result of our operation to the calling code. Ruby returns the last expression of a method, which means storing the result of a + b in a separate variable is redundant and unnecessary.
What did we accomplish with this refactor? The new method is easy to read and comprehend with little effort. As a result, you spend less energy trying to understand the code and more effort adding new functionality. Next time you are programming, make sure you take the time to proofread what you wrote. Does the code need much effort to understand? Do variable names describe the data they represent and method names inform the reader of their intent? Make sure you don’t leave any room for doubt.
Extensibility
Code can change for various reasons: new product requirements, gems need upgrading to fix bugs, and old code gets refactored. As developers, we must adapt because code we are writing now will change. It’s important to prepare. The steps we take to make our code extensible will pay dividends later.
During my career, I’ve used shortcuts to save
time. I’ve convinced myself there wasn’t enough time to do it the right way. I often regretted my decision later. Taking more time to design an extensible solution, I would have produced code that was not so fragile and hard to change. Don’t make the same mistakes I’ve made. Think how your code might change and plan for