Guido Henkel's Blog
April 4, 2023
Zen of Defensive Programming: A word about constants
It is time to tackle one of the key subjects of defensive coding practices—the use of constants.
Why so many programmers ignore this important language feature, I am not sure, particularly in Java, C, and C++. Python makes it a little harder to use because the language lacks intrinsic support for constants, but with a simple-work around, Python can also create pseudo-constant values.
For argument’s sake, I will refer to constants as const in a C++ context in this post. In java, look for t...
June 1, 2022
Zen of Defensive Programming: One operation per line
After discussing the evils of ternary operators in my last installment, let’s extrapolate a little from that last example. Just as a reminder, I had exploded a single line of code into a much more readable and maintainable code block. It is probably best expressed in a simple, single rule.
Only have one operation per line!
To me, this almost goes without saying. However, as I look at other people’s code, it reminds me again and again, just how blatantly programmers will abuse their languag...
May 18, 2022
Zen of Defensive Programming: Ternary Operators
In my previous installments, I showed you how a little bit of cosmetic change in the layout of your source code can dramatically improve readability and how tiny changes in your habits can truly affect overall code safety. In that vein, let’s continue and take this a little bit further by visiting Ternary Operators.
One, or the otherTernary operators exist in almost every programming language in one form or another. They are essentially an expression that says “If it’s this, do the follow...
May 9, 2022
Zen of Defensive Programming: Clear Intentions
As I pointed out in my previous post, making your intentions clear is crucial in defensive programming. You never want anyone to guess what your code does. Instead, without ambiguity, you want to make sure everyone understands what your code doing.
Here is another example of a coding practice that I see everywhere.
int newValue = value >> 3;Most seasoned programmers probably know immediately what is being done here. A bit-shift to the right by 3 is the same as a division by 8. Only, it...
May 2, 2022
Zen of Defensive Programming: Source Code is Cheap
Now that we’ve covered the preliminaries, it is time to dive into specific examples to illustrate what defensive programming habits you can build to improve your code. More importantly, I’ll also explain, why they are a good choice.
Perhaps the single most important thing to remember is that source code is cheap!
This takes us straight back to the readability pillar. I have worked with a lot of third-party source code throughout my career. From reference implementations to tutorials and ex...
April 25, 2022
Zen of Defensive Programming: Code Safety
We are now reaching the part that people usually associate with Defensive Programming: Code Safety. As I mentioned previously, often considered to be the only subject in defensive programming, as you have seen, it represents only a small subset of the subject as a whole.
Code safety generally consists of two steps, typically: Assertions and Guard Clauses.
AssertionsAssertions are available in most modern languages and are used to ensure that assumptions are met and enforced when a func...
April 14, 2022
Zen of Defensive Programming: Compilers and Compatibility
Modern compilers are extremely powerful tools that have built-in optimization logic far outpacing the abilities of ordinary programmers. They are intimately familiar with the internal states of the processor at any given time and can optimize register usage, cache pipelines, core states, and performance bottlenecks better than most of us mere mortals.
Also, remember that your job is not to try and outsmart the compiler. Your job is to ensure you get the implementation right. The compiler’s jo...
April 11, 2022
Zen of Defensive Programming: Readability and Maintenance
In the previous installment, I provided a preamble to outline what elements Defensive Programming encompasses. It is a lot more than the general coding mainstream typically associates with it. Some of the key tenets of Defensive Programming are readability, maintainability, compatibility, debugability, code safety, and security. Because they are the foundation for the techniques I will cover later on, please allow me to first explain why these tenets are so important.
What is Readability?...April 6, 2022
Zen of Defensive Programming
This is the beginning of a series of posts on the subject of Defensive Programming. It will take a look at coding practices to explore what steps software engineers and programmers can take to improve the quality of their code. This is achieved by maximizing their programming logic, utilizing tools and mechanisms that are already built into their compilers and interpreters, and by adopting a particular mindset that creates good coding habits.
Improved quality of the resulting software product...
December 20, 2018
My Christmas present for you…
To celebrate the Season of Giving, I have decided to release a novella I wrote some time ago, in the hope that you might enjoy the eBook.
I originally conceptualized this short story, Magic of the Glass Moon, as a supplement for a computer role-playing game I had been involved with some time ago. As part of the process, I had created an entire game world, conjured up the world’s history and lore, along with civilizations, languages, maps, locations and other content to fill that world with. A...


