The way to deal with complexity-abstraction

The reason for writing this article is that abstraction is the most important concept in software design. But the concept of abstraction itself is very abstract. It is necessary for us to spend some time to deeply understand the abstraction and the level of abstraction, and spare no effort to continuously improve our abstraction ability.

Abstract power

Without abstract thinking, there would be no glorious modern civilization of mankind.

Primitives saw a piece of wood and would not give them a name, such as "Pine Tree". They will give each tree a unique name, perhaps "silisiba". Primitive people only knew a specific tree.

With the development of the level of consciousness, humans began to consciously merge things with the same characteristics, from "silisiba" to "pine trees"-to "trees"-to "plants"-to "materials", from concrete Thinking to abstract thinking, we humans have spent tens of thousands of years.

Harari said in "A Brief History of Mankind" that the reason why humans become humans is because they can imagine. The imagination here, the author believes to a large extent also refers to abstract ability. It is this kind of abstract thinking that helps humans to abstract various concepts from concrete things, and then use these concepts to construct various fictional stories. These concepts, including politics (such as nation, country), economy (such as currency, securities), literature, art, and science, are all based on abstraction.

What is abstract

So what is abstraction? "Extracting" means pulling away, and "imaging" means concreteness. To literally understand abstract means pulling away from the concrete. The English abstract-abstract comes from the Latin abstractio, and its original meaning is to exclude and extract.

According to the interpretation on Wikipedia, abstraction refers to filtering the information contained in a concept or a phenomenon for a certain purpose, removing irrelevant information, and retaining only information related to a certain ultimate purpose. For example, for a leather football, we can filter its material and other information to get a more general concept, that is, the ball. From another perspective, abstraction is the process of simplifying things and grasping the essence of things.

To use language to explain abstraction is still too abstract:), we all know that there is a genre in painting called abstractism. The most famous abstract master is Picasso. Let’s use one of his paintings to intuitively feel what abstraction is . How? Is it abstract enough, just a few lines, but these lines are highly abstract lines, filtering most of the details of the buffalo, and only retaining some of the most important characteristics of the cow. The level of abstraction is higher, and therefore, its generalization ability is stronger. It can not only represent water buffalo, but cattle, dairy cows, and wild yaks can not escape these lines.

Abstraction is the foundation of OO

The OO (Object Oriented, object-oriented) way of thinking is that everything is an object, and abstraction helps us abstract real-world objects into classes. Help us to complete the mapping from the concept of the real world to the model of the computer world. For example, there are a bunch of apples, its abstract, we can get Apple this class, through this class, we can instantiate a red apple:  newApple("red"). At this point, if we need to take bananas, oranges, etc. into consideration, Apple's abstraction level is not enough. We need a higher level of abstraction, Fruit, to express the concept of "fruit".

Object-oriented thinking mainly includes three aspects: OOA (Object Oriented Analysis), OOD (Object Oriented Design), and what we often call OOP (Object Oriented Programming)

OOA is to decompose the system according to the abstract key problem domain . OOD is an object-oriented implementation process that provides a symbolic design system. It uses a method that is very close to the actual domain terminology to construct the system into a "real world" abstraction . OOP can be regarded as a kind of idea that contains various independent and mutually calling objects in the program, which is just the opposite of the traditional idea: traditional program design advocates that the program is regarded as a collection of a series of functions, or directly a series of functions. Instructions given to the computer.

It can be seen that abstraction runs through OO and is the premise and underlying ability of OO. It is difficult for people with poor abstraction ability to do OO well.

Level of abstraction

There are different levels of abstraction for the same object. The higher the level, the higher the level of abstraction, the more things it contains, the broader its meaning, and the more details it ignores; the lower the level, the lower the level of abstraction, and it says that the less things it contains, the more detail. In other words, what we often say is that the smaller the connotation, the greater the extension, and the larger the connotation, the smaller the extension. Different levels of abstraction serve different purposes.

Layered abstraction can be seen everywhere in the software world. It is the core of software architecture and the cornerstone of our huge and complex information technology.

For example, our system is hierarchical. The earliest programs run directly on the hardware, and the development cost is very high. Then slowly began to have the operating system, the operating system provides the basic functions required by all programs such as resource management, process scheduling, input and output, and it is enough to call the interface of the operating system when developing the program. Later, I found that the operating system was not enough, so there were various operating environments (such as JVM).

Programming language is also a layered abstraction. What the machine understands is actually machine language, that is, various binary instructions. But the use of binary programming is extremely inefficient, so we use assembly language to abstract binary instructions, use C language to abstract assembly language, and high-level language Java abstracts low-level language.

In software development, we should have all heard of various layered models. For example, the classic three-tier model (presentation layer, business logic layer, data layer), and MVC model. There is a famous saying: "Any problem in the software field can be solved by adding an indirect middle layer." The core of the layered architecture is actually the layering of abstraction, each layer of abstraction only needs and can only focus on the relevant information of the layer, thereby simplifying the design of the entire system.

It is hard to imagine that if there is no abstract layering, someone can deal with the complexity of the software world. For example, Ali’s software system is absolutely as complex as a city. If there is no abstract layering, it is like drawing the details of an entire city on a canvas, from skyscrapers to door handles, from parks to cats on the street. , Unimaginable.

How to abstract

Classification

Simply put, the process of abstraction is the process of categorizing and grouping and summarizing. Classification and grouping is to put together things that have an internal logical relationship, or something that has commonality. Summarizing is to name this grouping or classification, and this name represents the abstraction of this group of classifications.

In our lives, such abstractions are carried out all the time. Our language, as a symbol system, is itself an abstract expression of the real world. For example, when you talk about apples, you are using abstract concepts to refer to all red, watery, sweet and sour fruits. Similarly, flower is also an abstract concept. Faced with ever-changing and diverse flowers, it summarizes the nature of all flowers.

In fact, this ability to categorize and group is our nature. Humans have realized long ago that the brain will automatically organize everything it finds in a certain order. Basically, the brain will think that there is a certain connection between everything happening at the same time, and will organize these things according to a certain logical pattern.

The brain organizes everything that it considers to have "commonality". "Commonity" refers to having some similarities in common or similar locations. Take a look at the picture below

No matter who it is, seeing the 6 black dots above will think that there are two groups of black dots, 3 in each group. This sort of thinking is our innate nature.

However, the abstraction in actual work will not be as simple and intuitive as the picture above.

First of all, many commonalities are not so easy to be discovered, and even if they are discovered, they are not so easy to be classified.

Secondly, naming new abstractions is not an easy task, which is why naming is rated as the most troublesome thing for programmers. Jeff Atwood, the founder of stackoverflow, also said: "Creating good names is hard, but it should be hard, because a great name captures essential meaning in just one or two words".

On the other hand, if you can't name a class, a method, or a variable well, it often means that your abstraction is not good enough. This phenomenon occurs frequently in my actual work. Whenever you have disputes about a naming entanglement, you will find confusion in concepts or lack of abstraction.

Raise the level of abstraction

What should we do when we find that some things can't be classified into one category? At this time, we can make them have a logical relationship at a higher level of abstraction by raising an abstract level.

For example, you can logically classify apples and pears as fruits, or you can classify tables and chairs as furniture. But how can you put the apple and the chair in the same group? It is not enough to just raise one level of abstraction, because the previous level of abstraction is in the category of fruits and furniture. Therefore, you must raise it to a higher level of abstraction, such as summarizing it as a "commodity". What if you want to include E. coli? Then the abstraction of "commodity" = is not enough, we need to raise a level of abstraction, such as "material", but this kind of abstraction is too broad to explain the logical relationship between thoughts. Similar to our top-level parent object (Object) in Java, everything is an object.

In our development work, many times we need to improve the readability and versatility of the code through the promotion of abstraction level.

Pyramid structure

"Pyramid Principle" is a book that teaches people how to think and express in a structured way. The core idea is to build a pyramid structure through classification and grouping, so that we can think more comprehensively and express our opinions more clearly. It is a very useful thinking framework. We usually say that this person is very clear in logic, precisely because he has structured thinking and can say a thing very organized and clear.

The book says that we should think from the bottom up, summarize and summarize; express from the top down, and conclude first. The process of bottom-up summary is an abstract process, and the process of building a pyramid is a process of finding logical relations and abstracting. Regular exercise to deal with problems in a structured way and build your own pyramid can help us clarify the context of the problem and improve our abstraction ability.

The pyramid structure allows us to abstract and summarize the chaotic and disordered information to form different levels of abstraction, so as to facilitate our understanding and memory. This methodology is worthy of each of us to master.

For example, if you go out to buy newspapers, your wife asks you to bring something, and your wife makes a list for you, including grapes, oranges, salted duck eggs, potatoes, eggs, milk and carrots. When you are going out, Your wife said, bring some apples and yogurt by the way.

If you don’t need to write it down on paper, can you still remember the 9 things that your wife asked you to buy? Most people should not be able to, because our brain's short-term memory cannot accommodate more than 7 memory items at a time. When there are more than 5 memory items, we will start to classify them into different logical categories to facilitate memory.

For grapes, oranges, milk, salted duck eggs, potatoes, eggs, carrots, apples, yogurt. We can classify according to the logical relationship, and then build a pyramid structure as follows:

The function of classification is not just to divide a group of 9 concepts into 3 groups with 4, 3, and 2 concepts in each group. Because this is still 9 concepts, what you have to do is to raise a level of abstraction. The 9 items that need to be processed become 3 items.

After doing this, you no longer need to memorize each of the 9 concepts, only the 3 groups to which the 9 concepts belong. In this way, the degree of abstraction of your thinking is increased by one level. Because the thoughts at a higher level can always prompt the thoughts at the next level, it is easier to understand and remember.

In program design, the same is true. If too much content and concepts are involved in a class or a function, our brains will also seem overwhelmed and will feel very complicated and unable to understand. In fact, sometimes, as long as the daily necessities are classified according to the above method, a large method is organized into a set of higher-level small and cohesive subroutines according to the logical relationship, then the entire code logic will appear completely different The style is much cleaner and easier to understand.

In this regard, AbstractApplicationContextthe refresh method for context initialization in the core classes in Spring  gives us a good demonstration:

    public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
            prepareRefresh();
            // Tell the subclass to refresh the internal bean factory.
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
            // Prepare the bean factory for use in this context.
            prepareBeanFactory(beanFactory);
            try {
                // Allows post-processing of the bean factory in context subclasses.
                postProcessBeanFactory(beanFactory);
                // Invoke factory processors registered as beans in the context.
                invokeBeanFactoryPostProcessors(beanFactory);
                // Register bean processors that intercept bean creation.
                registerBeanPostProcessors(beanFactory);
                // Initialize message source for this context.
                initMessageSource();
                // Initialize event multicaster for this context.
                initApplicationEventMulticaster();
                // Initialize other special beans in specific context subclasses.
                onRefresh();
                // Check for listener beans and register them.
                registerListeners();
                // Instantiate all remaining (non-lazy-init) singletons.
                finishBeanFactoryInitialization(beanFactory);
                // Last step: publish corresponding event.
                finishRefresh();
            }
            catch (BeansException ex) {
                // Destroy already created singletons to avoid dangling resources.
                destroyBeans();
                // Reset 'active' flag.
                cancelRefresh(ex);
                // Propagate exception to caller.
                throw ex;
            }
            finally {
                // Reset common introspection caches in Spring's core, since we
                // might not ever need metadata for singleton beans anymore...
                resetCommonCaches();
            }
        }
    }

Imagine the above logic, if it is tiled in the refresh method, the result will be.

How to improve abstract thinking

Having said so much about abstract concepts, abstract thinking is so important. Is there any way we can exercise and improve our abstract thinking?

Yes, abstract thinking can also be learned. Babies and toddlers have no abstract thinking. You play peekaboo with him and blindfold him, and he will think that you are gone. If you let go, he will be happy to see you and giggle. Because he can only be aware of you, a concrete person, and his consciousness is not yet abstract. Understanding concrete content is simpler and easier than abstract, while understanding abstract content is much more difficult and complicated.

Read more

Why is reading a book better than watching TV? Because images are more concrete than words, the process of reading can exercise our abstraction and imagination abilities, and when you look at the images, your brain will be covered, so abstraction and imagination are less needed.

This is why we do not advocate excessive exposure of children to TV or mobile phone screens, because this is not conducive to the exercise of his abstract thinking.

The difference in abstract thinking makes children's academic performance differentiated from junior high school. Many people who cannot adapt to this abstract level of training go to technical school, which is more concrete than university: car milling, milling, parts and components can be seen and touched. .

More summary

When I was young, I didn’t understand why the Chinese teacher always asked us to summarize the main idea and main idea of ​​the paragraph. Looking back now, this kind of thinking training is very necessary in basic education, and its essence is to help students improve their abstract thinking ability.

Recording is also a good summary habit. Take reading notes as an example. It is best not to extract the contents of the book in the original text, but to summarize and summarize the contents of the book in your own words. This will not only deepen your understanding, but also improve your abstract thinking ability.

Our phenomenal world is complicated and complicated, and only with strong abstract thinking ability can we have the ability to grasp the essence of things.

Do more domain modeling

For technical students, we also have a very good means to improve abstraction ability-domain modeling. When we analyze, organize and abstract the problem domain, when we divide and model the domain, we are actually exercising our abstraction ability.

Regarding this point, the author deeply felt that when I started modeling using the modeling methodology introduced in the previous chapter, I felt unable to start and the model I built felt awkward. However, after several exercises, it is obvious that my modeling ability and abstraction ability have greatly improved. Not only is the speed of analyzing the problem faster, but the model built is also more elegant.

Guess you like

Origin blog.csdn.net/significantfrank/article/details/101095292