隔世的雕像吧 关注:29贴子:2,457
  • 0回复贴,共1

How to make programs and society simple

只看楼主收藏回复

We often heard of the “KISS” principle, which means “Keep It Simple, Stupid”, but simplicity is easier said than done. Ironically, there doesn’t seem to be a simple way of following this seemingly simple principle. Over the years, I gained some experiences of keeping programs simple, so now I’m going to share the most important trick I have learned.
What makes programs complicated
To understand the trick of making programs simple, it may be a good idea to understand what makes programs complicated.
Complexity means “entropy”, and entropy means “number of choices”. If you write a function which handles too many kinds (types) of input, you are very likely to make it complicated.
A good source of complication is trying to make sense out of nonsense. For example, if somebody calls your function with a null pointer, what do you do? If your first reaction is to “gracefully” handle the null pointer, then you are very likely to make the function complicated.
Many modern programming languages provide an “exception mechanism”, which allows you to “recover” from exception conditions such as null pointers. Be very careful about using those recovery mechanisms. Whenever not sure, don’t try to recover. By recovering from such nonsensical conditions, you are allowing more and more nonsense to come through!
Insist strictly on your principles, crash when violated
So what’s a better way of handling this situation of nonsensical input? CRASH! Don’t do anything, just crash as bad as you can. In our example, crashing will make the caller understand that you will never accept null pointers as input, and this will effectively reduce the likelihood that they call the function with nonsensical input.
Your function should keep its “principles” and insist on accepting only the things that make sense. It is NOT your responsibility of paying for your caller’s mistake.
That’s it? Right, that’s the most important trick of keeping programs simple I have learned over the years.
Applications to the society
The programming trick above also has an interesting implication of how to keep a society simple and carefree.
As functioning parts of the society, it is important that we keep our principles. Some societies are overly complicated because their people tend to be lenient on their “input”. The result is a society where nobody obeys traffic rules and laws are hard to enforce.
So the same trick in programming should be applicable to societies. By bravely keeping your principles and not cooperating with people who violate them, you are making the society simpler and better.


IP属地:北京1楼2013-12-16 20:41回复