preface
For experienced programmers, the readability of the code doesn’t seem to have a consistent understanding, recently this period of time is often inconsistent with readability understanding situation, just want to first talk about this topic, this article is not a science, or to the conclusion of the article, you can add your own code readability in comments to understand, In the body of the article I will simply explain some of the conflicts I have encountered and my current understanding of code readability
The body of the
‘Code’ and ‘readability’
Usually if you’re new to the job, if you write code like this
const a = 1
Copy the code
Your experienced colleague at the next table will probably point out that your variable name is unreadable because it doesn’t make sense. What is a and why is a 1?
You might wonder, what’s wrong with a? Or is a long variable name always a readable way?
Before we get into this confusion, let’s just try to explore the three words’ readability ‘
readability
When I was a child, I remember the teacher taught us to write sentences smoothly and clearly. Ancient Chinese literature, especially classical Chinese, has always been known for its downsizing. A suspected Milky Way falling nine days can reflect the majestic picture sense, the romance of The Three Kingdoms with so many words to describe Zhuge Liang, and then in Chen Shou’s annals of The Three Kingdoms only 96 words
I remember 96. Don’t hit me if I’m wrong.
Look at pictures and speak, poetry, articles, romance novels, handed down the method, the text is inevitably popular, classics will be handed down forever, on this point, these contents have very good readability should not be too much?
So since the readability of Chinese can be long or short, it can be concise or rich and detailed.
Is there only one standard for code readability? Such as:
if(a === '1') {return true
} else {
return false
}
if(a === '1') return true
return a === '1'
Copy the code
Which do you think is more readable?
Talking about readability alone is not objective, but subjective
After all, we are in the engineering business, and as far as software engineering is concerned, the emphasis on code readability must have a purpose. Whatever the purpose is, the background of readability discussion is whatever.
When we leave aside the purpose of emphasizing the readability of code, we can just stand on our own side and discuss what is readable and what is not readable in the background of our own experience and technical knowledge. Clearly there is a problem.
For example, is the above code readable to the machine? If you were talking to a computer, would he see a difference in the readability of the code? But what if you’re talking to a programmer who’s been writing code for years? What if it’s a product, or even a person who doesn’t know anything about technology?
I think everyone will have different ideas, so discussing code readability alone is a very subjective thing. It’s up to you not objective criteria
Can pinyin be used as a variable name?
const libraryBooksTotalCount = 0
const tuShuGuan_shuJi_zongShu = 0
Copy the code
The total number of library books, if translated in English, should be more than I wrote the first kind, different people to translate, I guess the answer is different, but if used pinyin, as long as it is a Chinese programmer, the answer is the same.
So is pinyin less readable than English naming? What if the people who maintain the code are All Chinese? When we emphasize code readability to better understand the meaning behind the code, pinyin readability better, or English readability better?
If this code is not to develop a library management system, this time pinyin readable? Is English readable?
My understanding is that the code is written around the library as a business object, if the team clearly has the business background for the code, or if you already have the business background before you read the code. Pinyin is better. It is less likely to cause variable redefinition problems. Technical experts and business experts are more likely to agree on understanding
But if you don’t know the business background behind this code, tuShuGuan_shuJi_zongShu could also be the total number of library secretaries. Pinyin lacks accurate definition compared to words.
Therefore, I understand that code readability is not a simple objective standard, but a comprehensive product of business background, readability maintenance goals, technical specifications and a series of factors.
In other words, as software engineers, we should not only regard code readability as an objective and established standard or constraint, but as an indicator of engineering maintenance. Only by giving this indicator practical business value and technical connotation can we correctly understand and discuss the significance of code readability.
The latter
This is an open topic and you can leave your thoughts in the comments section 😁