Psychology, let's talk about code.

Source: Internet
Author: User

What code is good code? This is really a topic that can be used to unplug your teeth. Well, let's repeat this boring time of sandstorm. Good code is easy to read code, separation of duties, a single responsibility, and high execution efficiency ....

Wait, and so on. This is abstract. It is too concise. I am just a cainiao. I have been writing code for a few years and have never read any books. Can I make it easy to understand?

Well, I stopped and thought, this is really a tough guy. I said, Well, I recommend a few books for you to see. Restructuring the bear's Day has recently been released. I suggest you buy one. Well, wait, there's a money-saving trick. It's nice to go to the Turing Club discussion group and register it. Haha.

However, you haven't told me what code is good code yet? I know you have no good answers.

Let me omit the sweat from my heart at this time. The following is a description of cainiao:

1. consistent

I found myself slightly obsessive, And I was impulsive when I ran into the following code.
Code before impulse:
Def imgname
If (XXX ){
Imgname = "meigui"
} Else {
Imgname = ""
}
Code after impulse:
Def imgname = xxx? "Meigui ":""

Although the two sections of code have the same functions, once I find the code before the impulse, I will feel uncomfortable and uncomfortable, as if I saw that the parade is not moving properly. The same is true for method names:
Before impulse:
Def testxxx (){}
After Impulse:
Def should_xxx_when_xxx (){}

The same is true for variables:
Before impulse:
Def imgnode = resouce. adoploud (node)
After Impulse:
Def node = resouce. adoploud (node)

In short, I don't want to see two ways to implement the same thing. If the function is similar, I will force consistency and uniformity whether it is logic, variable, or method name.

With regard to consistency, from the perspective of debugging code, sporadic inconsistencies are worse than a large number of inconsistencies, because the consistency in most places will be careless. When implementing the query paging function, we have the following code:
Nodeiterator. Size
This line of code is used to obtain the total number of query results. In most cases, it works well, but in a special case it returns-1. This was almost disastrous to me at the time, because we always believed that this line of code had the same behavior during the debugging process, and it took an afternoon to find this problem.

2. Concise

I like short code. For me, short programs are always easier to understand than longer code. In elementary school, I learned the text like this, as soon as I see a large paragraph, I always get dizzy (especially in Classical Chinese, I lose confidence in my understanding of this paragraph ). Annotations are mentioned here, that is, These annotations are made clear to improve the readability of the code and increase the difficulty for me to read the code. Therefore, I will not add annotations anywhere in the method, we recommend that you add methods before declaration, and avoid this situation as much as possible. If this class does contain important and obscure algorithms, I will only add comments before the class declaration.

There is an empirical conclusion about natural language called Zipf's law, that is, the length of the word most commonly used in natural language tends to be shortest.

When I write code, I can use short words as short as possible, for example, variable name, imagenode, I will certainly write it as imgnode; Method Name procedurexxx, I will certainly write it as procxxx, just like the annoying code section, I hate naming very long method names and variable names. Although these names are so long to better increase readability, they are not so readable.

In my first code work, we used pinyin to name methods and variables (Fortunately, class names are not included ), the reason why I hate this naming method is not because my Chinese teacher is so bad that I don't know what I say before and after, but because it does not eliminate the possibility of shorthand, or even to avoid ambiguity, sometimes it has to become longer.

3. Perception and Sequence

With regard to memory, humans have two important memories: Joint Vision and sequential memory.

The following is an example: You can always remember a person's face at a glance, such as Fan Bingbing. Although I still don't know whether she has single or double eyelids, it is unclear whether she has thick lips or thin lips.

In the code, the performance here is partial, that is, all the Code related to a function is concentrated in one place. The code I hate most is like this: At first I opened a file. During reading, I found an unclear method, so I press Ctrl and click the mouse, so I jumped to another file. Next, I read another method and found an unclear method again. So I pressed CTRL again and clicked the mouse. Wow, the new file is opened .... after so many times, when I opened the last file, I found that there were several lines of files in the IDE file bar, so I moved the mouse, right-click, in the pop-up menu, I chose close others. In an instant, the whole world is quiet. But what did I plan to do at first?

Therefore, please concentrate all the associated code in one place. Oh, by the way, if you don't need interfaces, please don't use interfaces. This is always the case where several lines of files are opened, and interface files are half of them. I rely on them and a few interfaces will die. By the way, this may be a bit of your consistent psychology. Sorry, sorry.

For the local part, an example is also related to debugging. During a debugging a long time ago, we could not find the cause of a variable error because no errors were found in this Code, after a long time, I finally found out that this variable was actually a global variable. Hey hey, let me tell you, this variable was very popular in servlet in, the title is: Do not use global variables in servlet!

The most typical example of sequence appears in high school chemistry. I can't say anything about 12th or 13 chemical elements in an instant. I usually remember it like this: hydrogen, Helium, Lithium, bismuth, boron, carbon, oxygen, fluoride, sodium, magnesium, aluminum, silicon, phosphorus, aha, 12th elements are magnesium, 13th elements are aluminum, and together they are beautiful women!

Therefore, in the Code, place the methods called to each other in order. method 1 calls method 2 first, so put method 2 behind method 1. I hate this configuration: open method 1, find that it calls method 2, click method 2, the scroll bar in the editor instantly rolls from the top to the bottom, followed, scroll from the bottom to the middle, and then to the bottom, and then to the top .... life cannot withstand such ups and downs. Really, what a heart it takes, McGrady once, 13 seconds ....

Also, you know why goto is so notorious.

4. Nature

This makes the code easy to express and minimizes the error rate. One of the most important ways is to make the code "natural", that is, move closer to the natural language. Because the code is not only about communicating with machines, but more importantly, communication between people is required.

Machine language to advanced language, process-oriented language to object-oriented language, JDBC to hibernate, Java to dynamic language, all of which make the code more natural.

Ruby has an inconspicuous feature, that is, there is no need to write parentheses for method calls. This feature is so insignificant, but many people are talking about it, because it is more natural, closer to our natural language. As a result, I saw my colleague xiaona, in groovy, getting rid of all the brackets she was capable.

In addition, there are differences between programming languages and natural languages. Apart from using Emotional Words in code to express emotions (I think, if you can, you will certainly see a lot of Feng Te ), the program language is not spoken. We seldom see such exchanges between programmers. Come on, let's talk about code segments (and of course, Xu Hao can, haha). They will use whiteboards and pens, or directly use editors. So, do you need to take a written examination at the end of recruitment? I am ashamed of companies that do not directly recruit people after a written examination, because they do not understand the programming language at all.

The gorgeous split line is omitted here.

In this article, I would like to thank our project team for the heated discussion of wgsn and for the wonderful comments from Xu Hao during the discussion.

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.