How to judge code quality

The evaluation of code quality is highly subjective.

From which dimensions to judge the quality of the code

The evaluation of code quality is highly subjective , and there are many words to describe code quality, such as readability , maintainability , flexibility , elegance , simplicity, etc. These words evaluate code quality from different dimensions. They interact and are not independent. For example, good readability and scalability of the code means good maintainability of the code. The quality of the code is a conclusion obtained by combining various factors. We cannot evaluate the quality of a piece of code through a single dimension

The following covers almost all common words we can hear to describe code quality

Flexibility, extensibility, maintainability, readability, understandability, changeability, reusability, testability Testability, modularity, high cohesion loose coupling, high efficiency, high performance, security, compatibility, ease of use Usability, clean, clarity, simple, straightforward, less code is more, well-documented, well-documented layered), correctness (correctness, bug free), robustness (robustness), robustness (robustness), availability (reliability), scalability (scalability), stability (stability), elegance (elegant), good ( good), bad (bad)...

Some words are too general and abstract, and tend to describe the whole, such as elegant, good, bad, neat, clear, etc.; ; Some may not only be limited to coding, but also related to architectural design, such as scalability, availability, stability, etc.

It is difficult for us to comprehensively evaluate the code quality through one or a few of them. Because these words are from different dimensions. The different evaluation dimensions are not completely independent, and some of them have containment relationship, overlapping relationship or can influence each other.

It is precisely because of the subjectivity of code quality evaluation that the accuracy of this subjective evaluation has a great relationship with the engineer's own experience. The more experienced the engineer, the more accurate the evaluation will be.

What are the most commonly used evaluation criteria?

1. Maintainability

What is code "maintainability"? What is the so-called "maintenance code" to do? When it comes to coding development,
the so-called "maintenance" is nothing more than modifying bugs , modifying old codes , and adding new codes .
The so-called "code is easy to maintain" means that the code can be quickly modified or added without destroying the original code design or introducing new bugs .
The so-called "code is not easy to maintain" means that modifying or adding code requires a great risk of introducing new bugs, and it takes a long time to complete.

How to judge the code maintainability is good or bad.

Maintainability is an evaluation criterion that is difficult to quantify and is biased towards the overall code.

The maintainability of the code is the result of the synergy of many factors. If the code is readable, concise, and scalable, it will make the code easy to maintain; on the contrary, it will make the code difficult to maintain.

More specifically, if the code has clear layers, good modularity, high cohesion and low coupling, and follows the design principles of interface-based rather than implementation programming, etc., it may mean that the code is easy to maintain.

The ease of code maintenance is also related to many factors such as the amount of project code, the complexity of the business, the complexity of the technology used, whether the documentation is comprehensive, and the development level of the team members.

It is a little difficult to analyze whether a code is easy to maintain from the front. However, we can give a more subjective but more accurate feeling from the side. If bugs are easy to fix, and modification and addition of features can be done easily, then we can subjectively think that the code is easy to maintain for us . On the contrary, if it takes a long time to fix a bug, modify and add a function, then we can subjectively think that the code is not easy for us to maintain. Whether it is easy to maintain is originally aimed at the maintainers. People at different levels have different maintenance capabilities for the same code. For the same system, senior engineers who are familiar with it will feel that the maintainability of the code is not bad, but because some newcomers are not familiar with the code, it takes a long time to modify bugs, modify and add code, they may feel that the code is maintainable Sex is not so good.

2. Readability

Any fool can write code that a computer can understand. Good programmers can write code that humans can understand.

How important is code readability? Because code is read far more than it is written and executed. Code readability should be one of the most important indicators for evaluating code quality.

When writing code, always consider whether the code is easy to read and understand. The readability of the code greatly affects the maintainability of the code. After all, whether it is modifying bugs or modifying and adding function codes, the first thing we need to do is to understand the code. If the code is not well understood, it is very likely that new bugs will be introduced due to inconsiderate consideration.
Since readability is so important, how should we evaluate the readability of a piece of code?

We need to see whether the code conforms to the coding standard, whether the naming is expressive, whether the comments are detailed, whether the function is of appropriate length, whether the module division is clear, whether it conforms to high cohesion and low coupling, and so on . You should also be able to feel that, on the positive side, it is difficult for us to give a list covering all evaluation indicators. This is why we cannot quantify readability.
In fact, code review is a good way to test the readability of code . If your colleagues can easily understand the code you wrote, it means that your code is very readable; if your colleagues have a lot of questions when reading your code, it means that the readability of your code needs to be improved.

3. Extensibility

Scalability is also a very important criterion for evaluating code quality. It represents the ability of our code to cope with future changes in requirements. Like readability, whether the code is easy to expand also largely determines whether the code is easy to maintain.

The extensibility of the code means that we can add new function codes through expansion without modifying the original code or modifying the original code slightly. To put it bluntly, the code reserves some function extension points. You can directly insert new function codes into the extension points without having to go to war and change a lot of original code just because you want to add a function.

4. Flexibility

Flexibility is a very abstract evaluation criterion, and it is quite difficult to define flexibility.

Under what circumstances will we say that the code is well written and flexible?

When we add a new function code, the original code has reserved extension points, we don't need to modify the original code, just add new code on the extension point. At this time, in addition to saying that the code is easy to expand, we can also say that the code is written flexibly.

When we want to implement a function, we find that many underlying reusable modules, classes and other codes have been abstracted from the original code, which we can use directly. At this time, in addition to saying that the code is easy to reuse, we can also say that the code is written very flexibly.

When we use a certain set of interfaces, if this set of interfaces can cope with various usage scenarios and meet various needs, we can not only say that the interface is easy to use, but also that the interface design is flexible or the code is well written. Very flexible.

If a piece of code is easy to expand, easy to reuse or easy to use, we can say that this piece of code is written more flexibly. Therefore, the word flexible has a very broad meaning and can be used in many scenarios.

5. Simplicity

There is a very famous design principle, the KISS principle: "Keep It Simple, Stupid". What this principle says is, try to keep the code as simple as possible. The code is simple and logical, which means it is easy to read and maintain. When we write code, we often put simplicity and clarity first.

However, many programmers with little programming experience feel that simple code has no technical content, and like to introduce some complex design patterns into the project, thinking that this can reflect their technical level. In fact, the real master can use the simplest method to solve the most complex problems in a calm manner. This is also one of the essential differences between a programming veteran and a programming novice.

6. Reusability

The reusability of code can be simply understood as minimizing the writing of repeated code and reusing existing code.

One of the purposes of object-oriented features, inheritance, and polymorphism is to improve code reusability; the
single responsibility principle in design principles is also related to code reusability;
decoupling in refactoring techniques, high Cohesion, modularity, etc. can all improve code reusability.
Reusability is also a very important code evaluation criterion, and it is the final effect that many design principles, ideas, patterns, etc. must achieve.

Code reusability is closely related to the design principle of DRY (Don't Repeat Yourself),

7. Testability

The testability of the code can reflect the quality of the code very accurately from the side.
The testability of the code is poor, and it is more difficult to write unit tests, which basically means that the code design is problematic.

How can we write high-quality code?

Asking how to write high-quality code is equivalent to asking how to write code that is easy to maintain, easy to read, easy to expand, flexible, concise, reusable, and testable.

To write high-quality code that meets these evaluation criteria, we need to master some more detailed and practical programming methodologies, including object-oriented design ideas, design principles, design patterns, coding specifications, and refactoring techniques. The ultimate goal of all these programming methodologies is to write high-quality code.

For example, object-oriented inheritance and polymorphism allow us to write reusable code;
coding standards allow us to write readable code;
single responsibility in design principles, DRY, based on interface rather than implementation, Li-style replacement principles, etc., allow us to write reusable, flexible, readable, easy-to-extend, and easy-to-maintain code; design
patterns allow us to write easy-to-extend code;
continuous refactoring can keep code maintainability.

key review

  1. How to evaluate the level of code quality?
    The evaluation of code quality is highly subjective, and there are many words to describe code quality, such as readability, maintainability, flexibility, elegance, simplicity, etc. These words evaluate code quality from different dimensions. They interact and are not independent. For example, good readability and scalability of the code means good maintainability of the code. The quality of the code is a conclusion obtained by combining various factors. We cannot evaluate the quality of a piece of code through a single dimension.
  2. What are the most commonly used evaluation criteria?
    The most commonly used criteria for judging code quality are: maintainability, readability, scalability, flexibility, simplicity, reusability, and testability. Among them, maintainability, readability, and scalability are the three most mentioned and most important evaluation criteria.
  3. How can we write high-quality code?
    To write high-quality code, we need to master some more detailed and practical programming methodology, which includes object-oriented design ideas, design principles, design patterns, coding specifications, refactoring techniques, etc.

Guess you like

Origin blog.csdn.net/rd_w_csdn/article/details/115165147