Do I really need that semicolon?
Zen Gardening is a practice where, instead of cultivating dirt and plants, the gardener precisely places rocks and rakes sand into patterns. This practice is often used as a form of meditation, as every particle is completely under control. Writing software is, in many ways, similar. While many aspects of programing, such as user input or consistent internet connection, are beyond our control, code style is completely up to the developer.
Code is for humans. If code were only for computers we would all write in their native language: binary. When we write code, we are expressing our intent. Bugs are a side effect of unclear or incomplete expression of that intent.
For a large portion of my career as a software developer I did not care about the “style” of my code. I barely even tried to have consistent indentation. But, after working with larger teams and on long-term projects, I began to understand the importance of code style and consistency. Consistent code style allows me to see the important parts of my code at a glance, even when that code was written months or years ago, saving me time and frustration. As I began sharing my coding style, it has helped other members of my teams to more easily find and squash bugs, or to extend my work to enable new functionality, since the code intent was clear.
“Code is read many more times than it is written.”
-Kathy Sierra, author “Head First Java”
Our brains are designed to identify patterns. We can reduce the noise level in our code by establishing patterns: fanatical consistency with whitespace, variable naming, and other syntax conventions. These patterns allow us to work more quickly, and to more clearly identify our intent. Suddenly problems stand out and we can rapidly address them. Small work in this area often goes a long way. Refactoring for style before committing to version control is a small thing, but it has large effects. Often, when I am stuck on a tough problem I will correct whitespace and add missing semicolons – addressing style, and allowing my brain to focus less consciously on the problem. Sometimes, those small adjustments are enough to reveal the answer!
“If you take care of enough grains of sand, the beach appears.”
My piano teacher often told me that it was possible to learn to play an amazing, expressive, complex song by working on one tiny part at a time. At first, the effects are small and isolated, but as more pieces become clear, the larger picture becomes more focused. Similarly, minor code style improvements can seem trivial, but once the majority of your code is styled, you’ll find that you notice errors and unlear intent immediately.
As a developer, your code can be a Zen Garden too. Your first step toward cultivating it is to adopt a style guide, and to regularly review your code for consistency with your guide. Whenever possible, use tools like linters and plugins like “prettier.”
As professionals, we should all strive to make our codebases Zen Gardens for ourselves, our coworkers, and our clients.