In a previous article, I talked about a common error tendency in programming language design: one-sided pursuit of short, which led to a series of historic design mistakes. Today I'm going to talk about another kind of wrong tendency, which also leads to a lot of mistakes, and continues to lead to the creation of errors.
The wrong tendency I'm going to talk about today is called "trying to hold the world." This error leads to a series of problems in "dynamic language" such as Python,ruby and JavaScript. I wrote a static parser for Python, so I basically realized the semantics of the whole python, which is pretty clear about Python. In designing this static analysis, I found that Python's design makes it difficult to statically analyze exceptions, Python programs are difficult to find errors, Python programs execute slower than most programming languages, which is actually a design problem from Python itself. Most of these design problems come from the same design tendency, which is "trying to hold the world."
In Python, each "object" has a "dictionary" (dictionary). The mapping between the field that contains this object and the value of the dict is actually a hash table. The general language requires that you define these names in advance and specify their types. And Python is not like this, in Python you can define a person, this person's field includes "name", "Head", "Hand", "foot", ...
But Python feels that the program should be able to create or delete these field at any time. So, you can add a field to a particular person, such as a "third hand." You can also delete one of its field, such as "head". Python thinks this is more in line with how the world works, some people can have no head, and some people have a long hand.
Well, that's very convenient. And then you have this problem and you have to wear a hat for everyone in the world. When you write this code, everyone in your consciousness has a head, so you write a function called putOnHat
, its input parameter is any person, then it will give him (her) head to put on a hat. Then you want to put this function map
into a collection of all the people of a country.
What you didn't expect, however, is that because of this "ability to describe the world" provided by Python, other people who write code create a variety of freaks you don't even think of. For example, a headless person, or a man with three hands and six eyes, ... And then you find that no matter how you write it, it's putOnHat
always an accident. You're surprised to find someone without a head! The most tragic thing is, when you spent a few months and a considerable amount of energy, to hundreds of millions of people wearing a hat, only suddenly encountered a headless, so the program was dropped. But even if you know that the program has bugs, it's hard to find out where the headless people came from, because they came to the country with a lot of twists and turns around the road. In order to reproduce the bug, you have to wait for several months, it will not necessarily appear ... This is the so-called Higgs-bugson bar.
What do we do? So you come up with a way to put "normal" in a single list, and the other freaks deal with it. So you want to have a way to make it impossible for others to put those freaks in the list. What you want is actually the "type" in Java, like this:
List<有一个头和两只手的正常人> normalPeople;
Unfortunately, Python does not provide you with this mechanism, as this mechanism is not enough to accommodate the vast and profound changes in the world, according to the "philosophy" of Python. Having programmers manually write types on parameters and variables is considered "excessive labor".
This problem also exists in JavaScript and Ruby.
Language designers should understand that programming language is not used to "construct the world", but simply to simulate it. The tendency to try to accommodate the world, without a lot of benefits, does not save the programmer a lot of effort, but makes the code completely free of rules. It's like living in a world without rules, no systems, no laws, often unpredictable things, running around with no head, three hands, and six eye freaks. It is a waste of endless trouble and time and energy.
Common design errors for the "Go" programming language (2)-try to accommodate the world