Original English:What makes code readable: Not what you think
As a developer, you may often hear"CodeThe first is to write "and" writing readable code is very important.
Developers often have their own opinions on how to make the code more readable. So you have carefully thought about what can truly enhance the readability of the Code.
Some standard answers
No matter what you useProgramming Language, You may agree with the following suggestions to enhance the readability of the Code:
- Good variables, methods, and class names
- Only one thing is required for a variable, class, or method.
- Consistent indent, consistent format
- Reduce the nesting level in the code
Of course, there are many other standard answers, such as in the book clean code (by Robert Martin) and Code complete (by Steve McConnell). You should read them first.
I know all these things. So,Below are some deeper things about code readability that you may not consider.
Reader's experience
Give me a piece of code. I can tell you whether the code is well written and highly readable within 2 seconds (at least I will tell you my opinion ).
At the same time, if I give a novice programmer the code that I have written with the best readability, they may not find any difference between the Code and other code.
Although my code has good descriptive variable names, short naming methods, and a small number of parameters, and they only do one thing, each function structure is clearly combined,These new users did not find that my code is better than other code without considering the structure.
In fact, I often hear other people complain that there are too many methods in my code that are hard to understand, and the variable name is too long and confusing.
Experienced developers and new users have fundamental differences in reading code.
An experienced developer does not pay attention to the vocabulary of the programming language when reading the code, but focuses more on what the code actually expresses-what is the purpose of the code, rather than how it is done.
Experienced developers will try to understand the actual structure of the code when reading the code. Beginners will focus more on the actual language vocabulary, that is, what the programming language in the Code tries to convey.
For them, a long variable name is not descriptive and confusing, because these variable names hide some facts, such as numberofcoins literally representing an integer value, in fact, it is not just an integer value. They would rather see the variable value named X or number.
An experienced developer does not care about integers, strings, and other types of variables. They only want to know what variables represent in the logical context of systems and methods, instead of the variable type and how it works.
Learning and reading
Learning to read code is like learning to readArticleSame. When they learn to read, they will not pay attention to grammar and the ideas conveyed in articles, but will only pay attention to the structure of words.
For example, an experienced musician can easily understand music scores, while beginners often need to find the corresponding keys on the piano or the number of corresponding products on the guitar Based on music scores.
Readability Constraints
When you express your thoughts and opinions, it may be limited to the vocabulary and reading volume you have mastered. Similarly, the readability of your code may also be limited to your programming language and programming context.
Let's look at an example. The following assembly code will output "Hello World!" on the DOS screen !" :
. Model small. Stack 100 H. datamsg DB'Hello world! $'. Codestart: mov ah, 09 h; display the message Lea dx, MSGInt21 h mov ax, 4c00h; terminate the executableInt21 h end start
The complexity of the language makes it difficult to read the written code. No matter how good you areProgramClerk, the readability of this code will always be limited to you.
Now let's take a look at Hello world in C:
Public ClassHello1 {Public Static VoidMain () {system. Console. writeline ("Hello, world!");}}
The following is Ruby's:
Puts"Hello, world"
You know why Ruby is so popular! If you understand a lot of vocabulary and syntax structures in Ruby, you will find that you can clearly display a thing.
What I want to say is that the larger the vocabulary you have, the more concise your expression will be, and the higher your readability will be. Of course, this is for those readers who also have a high vocabulary.
What can we get?
From the above content, we can see that our vocabulary and experience will affect the readability of the Code. We also need to consider who will read our code, their vocabulary and experience.
In C #, we usually argue whether to use conditional operators. For example, we should write such code:
VaRNextaction = dogishungry? Actions. Feed: actions. Walk;
Still.
VaRNextaction =Actions. NoneIf(Dogishungry) {nextaction=Actions. Feed}Else{Nextaction=Actions. Walk ;}
I used to say that 2nd methods are better, but I found that I often write 1st methods. Now, if someone asks me which one is better, I will tell him "depending on the situation ". I mean, you need to check whether the person reading your code understands the conditional operators.
Programming languages are also like ordinary languages, such as English. Some people think that there are a lot of vocabulary and absurd syntax in English, so it is easier to learn. However, it is difficult to convey information in a concise manner.
In your opinion, there should be a complex programming language in development to enhance developers' expression capabilities and enhance code simplicity and readability (of course, this requires a sufficient learning cost ), or should we keep a simple programming language to write complicated and long code?