Wang Yin: "smart" in programming (1)

Source: Internet
Author: User

I have long wanted to write such a blog post, but I haven't had time to write it. In school, it seems that time is always not enough, because once you have a little time, you may wonder if you should use it to read more papers. So I am very happy that my work and life have given me real freedom to share my experience.

I want to start writing this series todayArticleThe reason is that manyProgramThere are some incorrect ideas in the employee's mind through "irrational" methods. These ideas are so deep that you cannot tell them clearly. Even if they are clear, it is generally difficult to change their habits.

The world of programmers is a world of "proud", rather than a rational world of "convinced by morality. Many people like to play tricks in their programs to show their uniqueness. Due to the fame and prestige of these people, people tend to think about the small smart, so that they have learned a lot of superficial smart, in fact, leading to unnecessary troublesome ideas, deep-rooted, difficult to remove. Then, they spread these wrong ideas to the next generation of programmers through their "Pride", which leads to a vicious circle. People always say that "intelligence is the opposite of being intelligent." In the programmer's world, there are countless roots planted for such "little smart. So far, we are still struggling to make up for the mistakes made by our predecessors.

So from today on, I plan to record my thoughts on these "smart" ideas. I hope that people who have read these ideas can discover the subtle mistakes in their minds and truly improve their abilities.Code". It may be difficult to record all these mistakes at once, but let's start with it.

Smart 1: one-sided pursuit of "short"

I often use my own code that is very short. Some people once listened to it and said that they also liked to write short code. Then they began to say that the C language actually has many clever designs, which can make the code very short. Then I found out that what these people call "short" is totally different from what I call "short.

My program's "short" is based on clear semantics and concepts. On this basis, I strive to remove redundant, winding, and confusing code, so that the program can be more direct and more efficient to express the "model" I have imagined ". This is a kind of optimization at the conceptual level. In fact, it only indirectly leads to short and refined programs. This kind of short is usually at the "semantic" level, rather than just a few lines of code at the "Syntax" level. I will never make the program "short" hard to understand, or error-prone.

On the contrary, the "short" pursued by many others is a blind, principled little trick. In many cases, these tricks are only at the syntax level. For example, you can write two lines of code into one line. It can be said that this kind of "one-sided pursuit of short" error tends to create a number of language design errors and a number of programmers who "are good at" using these errors.

A simple example is the "auto-increment" operations I ++ and ++ I in many languages. Many people like to use these two items in code because it can "Save a line of code ". As everyone knows, the lines of code in the saved area are actually one of the nine cows compared to the obfuscation and error caused by the use of auto-increment operations.

Theoretically, I ++ and I are incorrect designs. Because they have two fundamentally different operations on variables: "read" and "write", they are merged without principle. This ambiguity about read/write operations leads to very hard-to-find errors, and sometimes even low efficiency.

On the contrary, I = I + 1 is equivalent to a "stupid" method, which is not only easier to understand, but also more in line with a subtle "philosophical" principle inherent in the program. This principle actually comes from an old saying: You cannot step into the same river twice. That is to say, when you step into the "River" for the second time, it is no longer the previous River! This sounds a bit mysterious, but I hope I can explain its relationship with I = I + 1 in one sentence:

Now let's imagine that you are the Superman Kakashi, and you have a clear "Write wheel eye". You can see the tiny operations of each step of the processor and the flow of every electron. For you now, I = I + 1 means to let I and 1 enter the "multiplier ". The information contained in I and 1, in BIT size, is decomposed and combined by the line of the divider. After such a complex conversion, a "new" integer is displayed at the "output end" of the generator, and its value is 1 larger than that of I. Next, the new integer is put into another variable through an electronic line. The variable name is also called I. Pay special attention to the quotation marks. Can you imagine the flow of information in electronic lines?

I am telling you that the first I and the second I in I = I + 1 are actually two completely different variables -- they are just the same name! If you change their names, you can write them as I2 = I1 + 1. Of course, you need to replace all the I after this statement with I2 (until I is "assigned" again "). After this transformation, the semantics of the program will not change.

Are I talking nonsense? What is the significance of changing the name in this way? If you understand the design of the compiler, you will find that the philosophical thought I just told you is sufficient for you to "re-invent" an advanced compiler technology, it is called SSA (Single Static assignment ). I just used this simple example to make you realize that I ++ and I not only bring about program obfuscation, but also delay or even impede people from inventing technologies like SSA. If people first realize the meaning of I = I + 1 (in fact, the two I in it are completely different variables), then SSA may be invented many years in advance.

(Well, Here I admit that the attempt to clarify the problem with a passage failed .)

Therefore, some people care about the differences between I ++ and I. It is futile to investigate the meaning of expressions such as (I ++) + (++ I. "Proficient" these subtle questions cannot make you a good programmer. The true practice is that I ++ or ++ I is not used at all. Of course, due to people's conventional habits, in a very fixed and simple way, everyone knows that you can still use I ++ and I. For example, for (INT I = 0; I <n; I ++ ). However, it is best not to use it in any other place.

If you place them in the middle of the expression, or the parameter location of the function, such as a [I ++], F (++ I) and so on, then the program will become hard to understand. If you put more than two I ++ in the same expression, it will cause an "uncertain" error. This error may cause different results in different compilers.

Although I have a clear understanding of these issues, I do not want to continue to discuss these issues. Instead of remembering this, we should completely forget the existence of I ++ and ++ I.

Well, a small example may have made you realize the huge cost of one-sided pursuit of short programs. Unfortunately, programmers are still making similar mistakes. Some "new" languages have been designed with many similar skills designed to "shorten code" and "Reduce typing. One day, you may find that, after a short period of excitement, these capabilities are actually endless troubles.

Questions:

1. google's "code specification" stipulates that curly braces must be written after the for statement and if statement under any circumstances, even if C and Java allow you to omit them when they only contain one line of code. For example, you cannot write

 
For(IntI =0; I <n; I ++) Some_function (I );

And must be written

For(IntI =0; I <n; I ++) {Some_function (I );}

Please analyze: Is this code specification good or bad? Describe the reason.

2. When I went to Google for the second internship, I found that many of my code was changed a year ago. Almost all code with the following structure:

 
If(X>0){Return 0;}Else{Return 1;}

Changed:

 
If(X>0){Return 0;}Return 1;

I have omitted one "else" and two curly braces here. What are the advantages or disadvantages?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.