Writing foolproof code

Writing foolproof code

[[154940]]

Over the past few months, I've developed a method for writing code. Whenever I write a new function, class, or method, I ask myself, "Is this code foolproof enough?" If it's not foolproof, then it's not done yet, so I try to make it as foolproof as possible.

However, foolproof code does not mean "code that does not work". Foolproof code should run exactly as expected, but in the simplest, most direct, and "foolproof" way possible.

Anyone who can write it, and anyone who reads it should be able to understand it. It shouldn't make the person reading the code think about the code itself, but about the problem at hand. It shouldn't take too long, it shouldn't be too complicated, and most importantly, it shouldn't try to be clever. It should just do the job, and that's it.

What does dumb code look like? That depends on the problem it's trying to solve. Take metaprogramming, for example, which is often seen as complex and "black magic". Does asking yourself "is this code dumb enough" mean "does it not allow metaprogramming"? Not necessarily, really. In some specific cases, a problem can be solved in the simplest way possible using metaprogramming. However, there are also many cases where metaprogramming is not necessary, and the extra baggage on the solution gets in the way of understanding the code as it should be.

The goal is to strip away the baggage, to chip away at it, until the most foolproof, yet working, tested code is produced.

Remember the word "idiot-proof" here: "it works" is not good enough. Overly complex, "look at this clever trick", overly abstracted, hard-to-read code that works and passes the tests is not what I want. It has to be idiot-proof: not clever, not complicated, and not hard to understand.

In addition to being called "idiot-proof," the resulting code has also been called "elegant," "clean," and "concise." But "writing idiot-proof code" is not as elusive as "writing elegant code," and the former seems easier to achieve, making this approach more valuable to me. In addition, I find it easier to start with "writing idiot-proof code" and end up with an elegant solution that is superior to other approaches.

Not every elegant solution is straightforward, but every “idiot-proof” solution by definition is and should be elegant.

Notes

Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or do some of the work that should be done at compile time at runtime. In many cases, programmers can be more efficient than writing all the code by hand, or give the program more flexibility to handle new situations without recompiling.

<<:  If you want to get involved in the technology circle, you must first understand these technology buzzwords

>>:  A programmer's epiphany: The ideal programmer is only 6 more than you

Recommend

Apple 2017 Fall Conference: A sneak peek at new features of Apple Watch

[51CTO.com original article] At 1:00 am Beijing t...

Wedding photography advertising promotion case!

After the epidemic, the May Day holiday is approa...

Gradle for Android Part 1 (Starting with Gradle and AS)

As you can see, this is an English book. Since th...

How can an application app avoid becoming a hit and then dying out?

Face Meng has become popular. Whether it is prais...

0 Basic Tarot Master Crash Course

Basic Tarot Master Crash Course Resource Introduc...

MSN's farewell to China is not a pity; its decline is inevitable

Recently, Microsoft announced via email that the ...

Why Mars is the first choice for deep space exploration

Human exploration of Mars began in the 1960s. The...

I dug a big hole in the city, and what I found was...

When you walk on the streets of a city, you will ...

User growth ≠ fission and new user acquisition!

Fission and attracting new users should not be th...

iOS 16 was revealed to have an abnormal Apple ID login bug

It has been more than a month since the release o...