About do one thing
Do one thing at a time – As stated in our earlier posts like DRY Principle (Computer Science), KISS Principle (Computer Science), or even Comments – What you should keep in mind, readability should be crucial to anyone’s code.
Something that lastingly improves the readability of your code is a technique called “do one thing”. It is often confused with the single responsibility principle (SRP), and the names are indeed confusing, in my opinion.
The main concept behind this is to reduce the code’s complexity by diminishing the things statements and functions do, to exactly one simple thing.
To show you an example of how it works and how significant the improvements can be, look at the following (garbage) code:
Sieve of Erastothenes
What does this code do? Rarely anybody can tell that after just reading it. If you read it thoroughly or inspect the output
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113]
You might find out that this is actually the sieve of Eratosthenes or at least any algorithm dealing with prime numbers. But if you never heard of the sieve of Eratosthenes, you will most likely search a while until you figure out what this code does and why.
And even if you wrote this and are fully aware of what it does, you might forget all of it in just a few days. What this code actually should do is the following:
Why is that code garbage?
Some people might argue that the code works perfectly fine, and some might even argue, “but it is optimized!”. Besides someone telling you that most likely never heard of broadcasting in Python, he or she also didn’t respect the first rule of optimization:
The first rule of optimization is: Don’t do it.Bjarne Stroustrup – C++ Coding Standards
And the reason for that is that you never really know what your compiler or interpreter actually does. Things that look complex to calculate might be faster than some “optimized” branch of code. To effectively optimize code, you need profound insights into the language at hand and the compiler or interpreter.
This is a job for experts that are specialized in this kind of work. As a developer, you should always rate readability over optimization. However, refactorings regarding optimization also should only be done if the time required for computation largely exceeds the expectations.
The second rule of optimization (for experts only) is: Don’t do it yet.Bjarne Stroustrup – C++ Coding Standards
With the concept of the sieve at hand and the idea of a simple job, let’s rephrase our code in a more meaningful way.
Isn’t this, in contrast, way easier to read and get? Even if your code is still garbage, using the name of the concept as the class name, you can reduce someone else’s research effort from hours to minutes as they can easily find what this should be about. The functions themselves are easy to read as the function name represents exactly the one thing the function does.
Thank you for reading this article! I hope I was able to make the point. I am aware that the improved sieve code isn’t perfect. If you come up with an even better to read solution, please e-mail us or post a comment. The smartest solution you can come up with will be permanently displayed here.
I am a developer and entrepreneur from Germany. I chose studying computer science because I love building things. I am of the opinion that there isn’t one truth (especially for computer science concepts) and whoever claims so isn’t that trustworthy in my eyes. I constantly try to improve things and myself. In my spare time I support businesses by offering them most of my services for free. Beside that I am kind of addict to e-learning courses.
Book Recommendations on Clean Code (Affiliate)