Elements of programming style
Recently, I finished reading The Elements of Style. Even for me, a non-native speaker, this little book brought me some good moments.
This book is better than many technical books that try to teach you how to code
“well” (go to hell programming coaches). There is a lot of parallels between effective writing and programming.
TL;DR
If you know how to write well, you know how to organize some code blocks.
Your are dump: Accept this and go ahead
If you need to organize your ideas and create drafts to write an essay, why you think code is different? Use your brain, rewrite you code. You should be adult enough to recognize that you previous 10min code session was shit and you need a refactoring session.
The Paragraph as the Unit of Composition: Modularity in Code
The Elements of Style emphasizes the paragraph as a fundamental unit, each dedicated to a specific topic. In programming, this concept mirrors modularity.
Just as a well-structured text is organized into cohesive paragraphs, a successful program is divided into independent, reusable modules or functions. We can see this applied successfully when we think in Domain Driven Design.
Concise and Direct Sentences: I Hate poems
The Elements of Style advocate for conciseness, recommending that writers eliminate unnecessary words and keep sentences short and direct. This principle applies to programming, where concise and small code is the state of art. Your code is not a poem, it is a haiku.
Active Voice: Intentional Code
You need to have active voice for direct, vigorous writing. In programming, this is akin to writing code that clearly expresses intended actions. There is no need to have small names, small names are the enemy of an understandable code.
Thanks, bye
I like to talk about coding, but I’m not any source of truth. Just try to not mess your code base, is painful to work with shitty code.
References:
- STRUNK, William; WHITE, E. B. The Elements of Style. 4. ed. Boston: Allyn & Bacon, 2000.
- HUNT, Andrew; THOMAS, David. The Pragmatic Programmer: Your Journey to Mastery. 20th Anniversary ed. Boston: Addison-Wesley Professional, 2019.
- EVANS, Eric. Domain-Driven Design: Tackling Complexity in the Heart of Software. Boston: Addison-Wesley, 2003.