Four Principles of Beautiful Code

by ajaxcms on 2009-06-20 16:18:17

**Original Chinese Source:** Website Construction Expert Aojie CMS Download Beach Landing 2009-6-12 18:04:09

Actually, sometimes programs and essays share the same characteristics. For essays, what readers care about most is — what is the main content of this article? And for programs, what others care about most is — what functions can the program achieve? An important point about both essays and programs is to see how they are written. Even if the content you want to express is good, if the essay or program is not easy to read, it will be very difficult to convey the article or program to the reader. Therefore, when writing, style and intent should be treated with the same attitude. Whether it's a program or an article, the key issue is to make people understand your meaning.

The vast majority of programs will not be successfully written in one attempt. The lifecycle of a program always involves constant reorganization and rewriting. Modifications or additions to functionality are made based on the current program. At this point, programmers need to have the ability to read the original code. Therefore, it is very important to follow coding standards during the initial stages of program development. For a set of rules governing code writing, it can better improve work efficiency and make it more comfortable for others to read.

What kind of code is considered beautiful code that makes reading easier? In fact, different people have different criteria for judgment. Beautiful code is not an abstract advantage that exists independently of a programmer's effort. The true meaning of beautiful code is to help programmers feel happy and improve work efficiency.

Concise code is also one type of beautiful code. In programming terminology, conciseness is a virtue. Because when people look at code, they will certainly spend some time on it, but readers do not want to see code that achieves the same function but is unnecessarily complex. Conciseness also means eliminating redundancy — redundancy is defined as repeated information. When a program has a lot of repetitive information, the cost of maintaining consistency in the code becomes very high. Due to the large amount of time spent maintaining consistency in the program, development speed will naturally decrease.

To eliminate redundancy, our solution is to consolidate multiple places where the same code is called into a specific method for invocation.

When writing programs, excessive innovation should also be avoided because, in such cases, the readability for common programmers may differ. Therefore, when writing code, one should adopt a conservative mindset.

Code flexibility is also very important. Here, I define flexibility as not requiring tool enhancement. When programmers are forced to violate their own ideas due to tools, the result is increased stress, which significantly lowers the efficiency of program development.