From Macroscopic to microscopic understanding of coding, macroscopic and microscopic coding

Source: Internet
Author: User

From Macroscopic to microscopic understanding of coding, macroscopic and microscopic coding

Macro thinking is to have a clear understanding of what we do and understand what we are pursuing. After understanding the whole, we need to have a deep understanding of what we are interested in. This is the so-called micro-thinking. The old saying "not seeking the whole, not seeking a domain" is the same.

I personally think that interfaces are the core of understanding from the macro to the micro. What is the interface of a program? Why is she the core?

Program Interface

 

Figure 1 function entity and its input and output

A function is an entity that completes a specific function. An interface is the input, output, and completed function of a function. The benefit is that as long as the input, output, and functions of the function remain unchanged, that is, the interface of the function remains unchanged, the specific implementation method of the function is independent of how to use the function, this achieves function implementation and application divestification. In this way, the application can be more focused on the application, and the specific implementation can be focused on better implementation.

 

The following is a simple example of the above program interface and its advantages

Integer multiplication 1

Int multiply (int a, int B) # The input is an integer a and B, and the output is an integer.

{

Int result = 0;

For (I = 1; I <= B; I ++)

{

Result = result +;

}

Return result;

}

Integer multiplication 2

Int multiply (int a, int B) # The input is an integer a and B, and the output is an integer.

{

Return a * B;

}

These two methods differ in computational efficiency and readability (redundant in integer multiplication), but they share the same feature of multiplying two integers, in addition, the input and output formats are identical, that isConsistent functions, the same form, or the same interface. Suppose we need to use the multiplication operation defined above in the application, that is:

Int a = 2;

Int B = 3;

Int result;

Result = multiply (a, B );

By selecting an appropriate implementation (Implementation 1 or implementation 2), you can get the expected results of our application after compilation. If we select Implementation 1 first and find that the running speed cannot meet our needs, we only need to select implementation 2 and re-compile it, so there is almost no need to modify the application code.

Conclusion: function functions are consistent, and the same input and output forms are the key (that is, the interfaces are consistent). The implementation can vary by time, location, and user.

Program interface is the core from macro to micro

One of program interface instances

In Linux, there is no essential difference between reading and writing hard disk files and U disk files for applications. Follow the following steps to open, read, write, and close a file. Open, read, write, and close can be understood as interfaces for file operations in Linux. For application writers, file operations only need to follow specific procedures, and all the implementation details of these operations can be ignored.

Program interface instance 2

 

Figure 2 network layering

In network application programming, we seldom involve the physical transmission layer, data link layer, and network layer. As shown in 2, the network adopts a bottom-up structure, and the lower layer only provides interfaces for the adjacent high-level layers (Please review our discussion about interfaces ). Take the data link layer as an example. The data link layer provides the defined interfaces and their usage specifications for the network layer. These interfaces are used to complete the work of the data link layer. The advantage is that when the data link layer interface is extremely standard, we have great freedom to choose the implementation method of the data link layer. The same principle applies to other layers of the network. At present, our increasingly rich network applications benefit from the design of this architecture.

Sublimation of program interface understanding

What is the essence of interfaces? What is the philosophical thought behind her? My views on this issue vary from person to person. Next I will share some of my experiences with my experience.

To implement a complex system, we usually divide the system into several simple functional modules, and then define the functions and interfaces of each functional module. Of course, relatively simple functional modules can be further divided according to the above method until the module is easy to implement.This is a top-down design paradigm, and the interface is the adhesive that assembles each module.. (I used to know that such a design paradigm exists, but I am still confused about how to assemble each module, and the interface can understand a way to assemble each module)

 

To implement a specific functional module, we usually look for some library functions or source code that implement similar functions. Next, we need to understand the data structures required for the existing library functions or source code, clarify the order of their function calls, function input and output, then add our specific processing based on this, and encapsulate it as an interface according to the interface specifications. (This can be understood as a bottom-up design paradigm. The interface provides methods for using this module)

 

An inappropriate example is that each underlying module can be understood as an atom, and the interface is the specification for connecting these atoms. Atoms connected according to specifications can form molecules with specific functions. If molecules are also considered as atoms with special functions, the combination of molecules and molecules, molecules and atoms, and atoms can create more complex functions. From this point of view,An interface is a way to combine existing resources (such as programs, molecules, and atoms )..

 

Summary: Each module is as simple as possible and easy to implement (philosophical charm). It also provides a mechanism (Interface) for constructing more complex modules from simple modules, which is of great significance for building complex systems.

 

Summary: interfaces and their corresponding functions can be understood as tasks, allowing programmers to decide how to complete the tasks themselves. This mode can be called the program-type management method.

 

Let the people who hear the fire call for the fire! Let the frontline make decisions directly! --Ren zhengfei

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.