Can you bear the bad code? Excellent code vs bad code

Sustainable development is not only related to project architecture design, but also closely related to code quality. The cleanliness and quality of the code are directly proportional. —— Robert C. Martin, “Clean Code”

If you haven't found the difference in code quality, if you've never seen good code, or have never seen bad code, then this article will visually compare and tell you how much the code quality will differ.

[Code size]

We know that the amount of code shows the complexity of the function, such as Windows XP with more than 20 million lines of code and the Linux kernel with 15 million lines (2012). However, there is a non-linear relationship between the amount of code and the number of functions.

In a good design, the relationship between the amount of code and the number of functions is like this:

In a bad design, the relationship between the amount of code and the number of functions is like this:

Excellent systems tend to have excellent structural design: clear hierarchy, single responsibility, modularity, and easy expansion or reuse. Adding features is often just adding a small amount of code to an existing framework.

However, in a bad design, the layers are chaotic, mutually coupled, and difficult to read, which is difficult to reuse and difficult to expand. Whenever a feature is required to be added, the entire functional flow has to be implemented almost completely and all compatibility issues with the original system are fixed.

[ Notes ]

In excellent code, the comments are like this:

In the bad code, the comment is like this:

The most stupid programmers can write code that the machine can read, and good programmers can write code that people can read.

The notes to the program are for people to read. In most good code, the comments are almost half the number of lines of code, describing function functions, interpreting parameter configurations, and pointing out traps. The bad code not only does not contain these comments, but even retains a lot of residual code, which is poorly readable and difficult to refactor.

[name]

In excellent code, the naming is like this:

In bad code, the naming is like this:

Naming is to make the code easier to read, and the terminology of the specification is not only easier to understand, but also a representation of the developer's level of knowledge and development experience. If it is strange to name it in Chinese Pinyin, then it will be hateful if it is directly named a, b, c, d. Who remembers that your a is a god horse thing!

Look at a classic example, string replacement:

A little bit of Javascript common sense will think of regular expressions:

Why copy this bunch of code to the boss? There are indeed many times in development, direct copy code can not only complete the function immediately, but also does not affect the original function. However, if there is more such code, you will need to re-debug all the copies in case of adjustment. It's worth spending some time learning more elegant usage.

[function]

Keep the function short and make your code more readable, for example:

Complete the complex logic in steps. If written together would be like this:

Believe me, once you write such a function, it will get longer and longer over time, until one day, you forget what a piece of code is doing.

In fact, complex functions can be divided not only by steps, but also by levels. Don't do different levels of operation in a function, otherwise it will become very difficult to understand.

Industrial Router

Industrial Router,Industrial 3G Router,Industrial Cellular Router,Industrial Router Din Rail

Shenzhen MovingComm Technology Co., Ltd. , https://www.movingcommiot.com