What kind of code is good code. Measuring the quality of the code or the dimensions have a lot, such as good performance, good structure, high cohesion, and so on, the focus of these indicators are not the same, the focus of different developers are not the same. I personally prefer simple, readable code, and I measure the good code from the following dimensions:
The code is available to work
The purpose of writing code is to solve a particular problem, so in any case, the code is first and foremost able to work to solve a particular problem. Can work contains two layers of meaning: first, from a functional point of view to meet the needs of users, the second is from the performance point of view to meet the current basic performance requirements. Therefore, the work is to measure the quality of the precondition of the code, only consider the code itself does not consider the work of the home to take the end.
The code is highly readable
Code is developed and maintained by developers and is typically maintained by different developers during the long lifecycle of the software, and future maintenance will be a nightmare if the code is poorly readable. The code we write is for the developer, and it's not for the machine (the compiled code is for the machine, the compiler will help us get rid of meaningless blank lines, etc.), so the code must first be readable.
So what's the readable code? From the coding style point of view, meaningful naming, adding necessary documents and comments, classes and methods not too long, each row is not too long, add the necessary empty lines and necessary indentation, etc., you can refer to the "C + + Programming Specification" and "refactoring to improve the existing code design 》。 Another aspect is from the structure of the code to define, such as code is high cohesion, low coupling, the code is simple, these three aspects will be described in detail below.
The code is simple
Let's take a look at what is a complex code, for example, the name said for the expansion of the code, the use of a lot of design patterns and software development principles, the result is clearly can use a few lines of code to get things done, the result used dozens of lines of code or even more, and the code used a variety of cool technology, But in fact most of the extensibility may not have happened in a lifetime, from the agile development perspective, this is very typical of the excessive design. Agile development is not about design, just not advocating excessive design, such as considering 10 years after the expansion of the system is meaningless, another scenario is simply to do a simple background management system, but a lot of effort to consider high concurrency is meaningless, overly designed code is often complex.
So in the appropriate consideration of the scalability of the Code on the basis of the design pattern can not be used without design mode, can not use new, complex technology without new, complex technology, technology enough, the simpler the better, some people say the code is too simple is not a bit low, In fact, writing high quality simple code is far more difficult than writing complex code, especially when the system is more complex, it is very difficult to maintain the simplicity of the code is very large.
So the simple code is: the code everyone can read, especially the new, but also have a certain degree of extensibility and maintenance, simple to say is simplicity but not simple. Complex code is very high on the person who reads the code first, resulting in the code being difficult to maintain. Code is simply one aspect of the high readability of the code.
The code is high cohesion.
Cohesion is from the functional point of view to measure the link within the module, code correlation is strong code should be put together to form a separate function module, can be a separate class, can also be a micro-service. In fact, to determine whether the code to gather a relatively simple way is to see if you can give the code or service to an appropriate name, if the code function is not poly, we are very difficult to use a short name to express its meaning.
The code is low coupling
Coupling (coupling), also called coupling degree, is a measure of the degree of association between modules. The coupling depends on the complexity of the interface between the modules, the way the module is invoked, and how much data is transmitted through the interface. The coupling degree between modules refers to the dependencies between modules, including control relations, call relationships and data transfer relationships. The more connections between modules, the stronger the coupling, and the worse the independence of the module.
Coupling relatively high code harm is relatively large, the most common performance is to change a module of the code will affect many other modules, the final inevitable cause everyone dare not modify the old code, can only keep adding new interfaces, the system is very poor maintainability.
Reproduced in the Red-Black Alliance