When dealing with one module, you should not rely on the internal work of another module.
Cohesion principle means that in a given module, all code should only accomplish one goal.
There is a famous slogan in IT field: strong cohesion and loose coupling.
Even the most elementary programmers have realized the meaning of this slogan in their own continuous teaching: our program should be modular, the module should complete a clear set of related service functions, its various parts should be interrelated and organically combined into a whole (the external program looks like a black box), and the correlation between the internal components of the module should be as high as possible (strong cohesion); And modules are required to be separable and less dependent (loosely coupled).
It is easy for people to realize a module with strong cohesion, for example, a function realizes an independent function, which is strong cohesion.
It is not easy for people to realize loose coupling, because isolated modules are meaningless, and only when modules work together can the purpose of the system be realized. However, without certain experience, it is difficult to grasp the design of the relationship between modules. The strength of coupling depends on: (1) the call of one module to another module; (2) the amount of data transmitted from one module to another; (3) How much control one module exerts on another module; (4) The complexity of the interface between modules. Wait a minute.
Of course, "strong cohesion and loose coupling" are also contradictory. For example, the stronger the cohesion, the more functions are needed (each function only does one thing). In this way, the more complicated it is to combine them into "big" functions, and it is impossible to achieve loose coupling. Therefore, it is necessary to make a balanced and compromise choice between the two, which also reflects the level of programmers. From the perspective of system theory, the system is hierarchical, that is, the system can be divided into subsystems and modules can be divided into sub-modules. The degree of "strong cohesion and loose coupling" should be considered in combination with the sub-level of the system, that is, a compromise should usually be made at the level, for example, the subroutine in a module (at the next level) should * * * enjoy the data (with a certain degree of coupling) and reduce it.
Object-oriented language further strengthens "strong cohesion and loose coupling". The encapsulation of classes not only emphasizes the cohesion of related content (data and its operations), but also emphasizes the independence and privacy of classes. The inheritance and friendship of classes standardize the relationship between classes under the principle of loose coupling. Classes usually implement the service provider/service requester pattern through the contract of the interface, which is a typical loose coupling.
"Strong cohesion and loose coupling" has an important relationship with programming, program maintainability and division of labor in testing. For example, from the design point of view, the program modules designed under the guidance of "strong cohesion and loose coupling" meet the requirements of WBS (work breakdown structure) of project management, and their relatively independent modules can be assigned to specific programmers for development. In addition, the outsourcing of program coding must also be based on this principle. From the point of view of program life cycle, it is beneficial to improve the program quality, especially to facilitate the future maintenance of the program, that is, the relative independence of the program module is the guarantee of maintainability; From the point of view of testing, it is easy to carry out black-box testing on components (modules), and write "piles" and "drivers" for testing according to "strong cohesion and loose coupling" programs.
"Strong cohesion and loose coupling" is also a requirement for organizational structure. The project team is divided into several groups (formal or informal), and the work of each group should be highly related. The work between each group should be as little related as possible or have a clear interface, which can reduce the communication cost. In fact, "strong cohesion and loose coupling" is a universal principle that the system should abide by, and we can find its application in many fields.
"Strong cohesion and loose coupling" are the "three-character classics" that we have to read, so we must read them well.