Ways to Improve Code Quality: Domain Models, Design Principles, Design Patterns

Introduction:

The author believes that the most critical thing about the code difference is the internal problem. There are two root causes: low self-requirement; no feedback channel. If you don’t have high requirements for yourself, you will stop just by meeting the requirements and developing it. It is difficult to write high-quality code, and it is difficult to improve your skills without external feedback. To improve code quality, I like to use three methods: domain modeling, design principles, and design patterns.

Code Quality:the root cause of poor code

Code Quality:Embodiment of poor code
We can list the manifestations of a lot of poor-quality code, such as unintelligible names, super-large classes, super-large methods, repeated code, code that is difficult to understand, and difficult to modify. The two manifestations that most affect code quality are misnamed names. , poor logical scalability. When a newcomer reads the code, sometimes it is found that the method naming does not match the actual logic, which makes people very confused. This phenomenon is not uncommon in normal work; the other is the poor logical scalability, a After the new business requirements were put forward, it was found that there were many changes to be made, and there were many business logics that needed to be returned, resulting in low R&D efficiency.

Code Quality:problem induction

Summarize and sort out the problems mentioned in Section 1, roughly sort out 6 types of problems, and explain them separately.

Code Quality, Naming Problems: Naming problems can be quite a headache, and it is not easy to come up with a real and understandable name. When it comes to variable naming, method naming, and class naming, there are two common naming problems: one is unintelligible; the other is a misnomer. Naming is unintelligible at first glance, and one does not know what it means. The fundamental reason is that he has not thought of a suitable vocabulary to abstract the problem; the naming is not true because the meaning of the naming and the actual logic is different. Such a naming will mislead.

Code Quality, Code structure problem: When a person looks at the engineering code for the first time when he has not looked at the code logic in-depth, he can feel the code quality from the module division, class division, and method division as a whole. If a class has thousands of lines of code, There are hundreds of lines in the method. I believe that not many people are willing to look at such logic, and the complexity is relatively high. Good code hierarchies are so clear that it is as pleasing to the eye as reading a beautiful book.

Code Quality, Programming paradigm problem: There are three programming paradigms: table mode, transaction script mode, and domain design mode. The most commonly used is transaction script mode, which is most in line with the way people do things, step by step, the biggest problem with this mode is to assume the responsibility that should not be undertaken by oneself. It seems more logical, but in fact, there are many problems. Usually, people like to call it "spaghetti code".

Code Quality, Readability issues: In addition to implementing business functions, the code must also have good readability. Some codes do not have any comments; some code formats are not uniform; It's not that Lambda expressions are bad, the key is to control the depth of the hierarchy), such code looks concise and not very readable.

Code Quality, Scalability problem: The scalability problem is a common problem. It is not so easy to achieve good scalability. Generally, there is no abstract problem. For example, if a store displays a Tab in the store, the noodle code is to directly define a List. , and then add a Tab object to it. What if you need to add another Tab? Typically, the open-closed principle is not satisfied.

Code Quality, No design problems: The whole code looks bland, and others can't learn from it after reading it. Generally, this kind of problem is that there is no in-depth analysis of the problem, and only the problem is solved, without considering how to better solve the problem, such as whether the work of the repeated processing process can be abstracted into a general template class, and whether different processing classes can be processed through the factory class. Get specific strategies, whether asynchronous processing can be processed using the event model, and whether new capabilities can be discovered through automatic registration...

Code Quality, root cause analysis

Code Quality, Next, analyze the reasons why the code difference occurs. This problem has external reasons and internal reasons. The main external reasons are: an urgent project schedule, not much time to design; shortage of resources, lack of manpower, can only come as soon as possible; urgent problems are repaired, and temporary solutions are quickly dealt with... The main internal reasons are: own low skills, how to master the skills, such as Lamda expressions, commonly used tools, advanced framework usage, etc.; without the spirit of ultimate pursuit, just fulfill the requirements, stability, scalability, performance, Data consistency, etc. are not considered...

The author thinks that the most important thing is the internal problem. There are two root causes: low self-requirement; no feedback channel. If you don’t have high requirements for yourself, you will stop just by meeting the requirements and developing it. It is difficult to write high-quality code, and it is difficult to improve your skills without external feedback. The author's previous supervisor was very strict, and he reviewed the code written by everyone carefully. A variable name, a logical writing method, and repeated revisions were actually the fastest way to improve your skills.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00