How to write high quality code

zhaozj2021-02-08  283

First, improve the reuse of the code

The reuse of the code can be implemented by two aspects: function level package and object level package

1, function level package

Reuse is not only reflected in the encapsulation of the object, not only a layer or multi-layer function level package for repeated business, can reduce the complexity of the implementation process.

Features of a functional level package: This process has a simple process, that is, there is only one line, no related branches or multiple portions.

Level: That is, when writing a function, you need to pay attention to the application width of the write function, it is necessary to consider writing a universal function function, then write a correlation function function, universal function may be used in other aspects, while functional correlation functions However, private functions can make the current application simply, that is, the reuse is guaranteed, but also ensures the simplicity.

2. Object-level package

Object-level packages can be safer, more complete package, concealed unrelated processing branches, so that the upper layer calls only the atomic feature of the object.

Object-level packaging features: object effects have persistent, there may be multi-port or multi-exit, there are many related features, business-independence, etc.

Second, the consistency of code

Internal consistency: When implementing the package, whether it is a function level package, or an object-level package, you must pay attention to the consistent style. For example, the first-line entry parameters will not be used, and the first column should not appear. The form of the inlet parameters of the line.

Upper and down: For continued private grade packages, you should pay more attention to the consistency between the following levels, should not let the tiger have a cat, it should be elid. At the same time, it has to ensure that the interface function of the parent class is not changed (special considerations).

Third, the integrity of the code

The object's package must provide a complete interface. How to ensure access and operation of internal objects is provided in the premise of ensuring internal objects, you must provide full interface support, provide part of the interface, such as internal properties objects. Provide GET and SET interface.

Additional: Provide a complete error handling mechanism, because this is not all components or objects, but is only more important when needed.

Fourth, the user-friendly interface

Humanization mainly reflects several aspects such as transaction processing and parameter calls. Do not implement external implementation, providing as many default parameters as possible, that is to say, objects are life. If the object is created, you need to assign a series of memory. If you don't have to create an additional interface of the object when you create an object; the object needs to release memory when you destroy, you should consider itself (avoid memory leakage, etc. More serious problem)

Take advantage of the polymorphism of the subject, use the simplicity of the caller, such as a package object, we can use an interface to package multiple types of data, namely the interface naming no longer reflects the structure of packaging data. Or characteristics.

The user-nature of the interface is naming. When you are named, consider the simplicity of its meaning. If the caller does not need to care, let the user only know that he calls just an object, but not to call multiple objects. At the same time, the interface should reflect the feature information of the interface, so that the caller has an experience that is called from the interface naming.

V. Grasping the degree of packaging

It is not a lot of packages, we must grasp a package limit, but ultimately may realize the related components needed by the user, but the actual package process may be a pour (gold) triangle mode, that is, from the most original multiple components A plurality of primary components are implemented in the secondary package, and the final component may be composed of multiple advanced components. That is, the encapsulation of each component should be considered as simple as possible (complex components may be composed of multiple logical concepts, unless a high component may need to consider a variety of factors for performance requirements). Under normal circumstances, the performance of the simple implementation is higher than the performance of a single component that directly uses complex implementation. Another role or benefit of a finite package: Every implementation is a simple package, logic is simple, not easy to have big problems, easy to expand and maintain.

Sixth, the uniqueness of nature

The package is not a big gathering, try only to reflect the interface interface, other needs of the interface can be done in other components, then call the current components, try to avoid multiple usage of an interface, resulting in a large to create a big Objects (Slims).

7, prevent coding from changing

The current coding specification still stays in changing naming and partial coding rules, but a large number of specifications are still limited to programmers's own coding habits.

1. Require programmers to minimize the use of global

The use of global variables is mainly because it is easy to use programs to make confusion in logic, so that the program forms a mesh structure, which is difficult to perform status analysis. That is, it is easy to make problems analysis and control, and the program may lose security.

For peer conditions, write an object (component), inside the internal definition of a large number of member variables, in fact, for the member functions in the object, they are global variables, the member variables are more, and the structure can be blurred, not easy to maintain, control, and debug Wait.

2, ask the programmer to minus or do not use the GOTO statement

There is a considerable number of programs that are often delivered between objects, not through a certain mechanism, but a member privilege of open objects to obtain direct data. Thus, the memory leak provides a machine, which is no different from GOTO.

The above is a summary of programming experience over the years, if there is a supplement or new idea, please give criticism and advice: satchmo2000@163.net

Satchmo

April 25, 2003

转载请注明原文地址:https://www.9cbs.com/read-1027.html

New Post(0)