How to express and maintain large-scale logic

Source: Internet
Author: User

EnCircle of wood, born in margins, nine-storey platform, from the tired soil;   let us temporarily put aside the platform, framework, technology, design patterns, object ideas, agile development theory and so on. Questioning the nature of the procedure.  Essentially, a program is a set of commands that are ordered to execute. It is a question of how to organize the collection of instructions into reliable and trustworthy software (the Tower of Logic).  program = logic + control.   What does + when to do.   from a programmatic point of view, the developer is dealing with logic, logical expression, organization, and maintenance. Logic is an acceptable sequence of things from this and others. A directive is a concrete form of implementation of logic.    That means:1. What logic to express;2. How to express the logic;3. How to maintain the logic.    The complexity of software is manifested in how to express and maintain the large logic of complex interaction.  The design pattern embodies a method of decoupling logic blocks .The application framework solves the problem of the control of the general logic flow of the application, allowing the developer to focus more on the specific business logic;The development technique is to explore the method of solving the specific problem in the specific application situation according to the established general idea.   What we're going to solve is a common problem: how to express and maintain large logic in a more error-prone way?   The ultimate trick to expressing and maintaining large logic is to cut large logic into small chunks that are easy to digest and "Eat without rush".       1. Large-scale logical or interface implementations with no interaction Independent, non-interactive logic often manifests itself as a common library that solves common or public daily tasks and has no dependencies or interactions with other logic, that is, self-contained logic. the first way to deal with the large logic of independent non-interaction is to break down into several small pieces of logic that are easy to implement, test, and reuse, written and rigorously tested. the second is to use the mature programming model to express the logic, as far as possible to reuse the rigorous testing of reliable libraries. The large logic independent of interaction can obtain sufficient testing and reliability by slicing logical blocks and rigorous unit tests.  programming languages and Reusability explains how programming languages support highly flexible reusable expressions.    2. Time-consuming logical or interface implementations with independent, no interactionFast response: The contradiction between a user's demand for a short wait time and a lengthy request processing time. solving the time-consuming logic of independent interaction can still be made easier by using the method of slicing logic blocks and rigorous unit testing;In addition, there are two design ideas to consider: concurrency and asynchrony. The concurrency idea is to assign separate logical blocks of the shards to the different control threads, thus reducing the request processing time, and the performance gains from the concurrency scenario depend on the time-to-total operation of the serial operation. asynchronous thinking is "first response, post-processing, final notification" of the "first play after Chop" scheme. Separating one step into three steps, in order to give the user the first answer, adds new problems: the development and deployment of the message middleware component, the asynchronous message sending and receiving, the change and adaptation of the programming model. If the whole process works well, it will achieve a good experience and is easy for users to accept. If one of these steps goes wrong, it can lead to various problems, such as inconsistent data, message stacking, and requests not being processed. The end-user wait time does not degrade, but it makes the experience worse. Of course, if the success rate is 95%, is also "acceptable", so users may blame themselves "bad luck", but not too much blame system imperfections.  concurrent and asynchronous schemes are more difficult to debug than synchronous schemes. Each new design will have its advantages and disadvantages. Weigh the pros and cons, accepted.             Note that the concurrency scenario is for the service-side to actually process the request logic, whereas the asynchronous scheme is a way to respond immediately before the request is processed, and in combination with sequential, asynchronous, and synchronous 22, there are four ways:Sequential synchronization: The initial programming model, the advantages are simple, safe, easy to maintain and debug, the disadvantage is low performance, response time and throughput are not high, if the request processing time is very short, the use of sequential synchronization scheme is good;concurrent synchronization: Improved programming model; The advantage is to improve the processing speed and throughput of the server through concurrency, but if the request processing time is long, the response time is still not high, affecting the client experience, if the request processing through the concurrency scenario is very short, or the client experience requirements are not high, you can use concurrent The program of the step;Sequential Async: A programming model for improving the client experience, with improved response time and client experience, with no improvement in throughput due to the sequential way in which the processing of the requests is still in sequence. is a good compromise, if the request processing time is long, affects the client experience, and the request processing logic is complex, the use of concurrency scheme is error-prone or difficult to concurrency, can use sequential asynchronous scheme;Concurrent Async: Improves both the client experience and the server-side processing speed, with the advantage of increased response time, client experience, and processing speed and throughput. Disadvantage is error-prone, and difficult to debug, if the client needs high response experience, request processing logic simple (such as simple data pull and summary), the use of concurrency can effectively improve processing speed, can adopt concurrent asynchronous scheme;             3. Small block logic or interface implementations with interactive coupling The complexity of software is truly reflected in the continuous and long-lasting interaction of logical blocks. This is the most challenging part of software development and maintenance. logical blocks that have interactions are often reflected in access to mutable shared resources. Interactive coupling is actually caused by shared variability. One approach is to implement concurrent mutex, which is secure but low performance, and the other is to implement the sharing of immutable resources through acceptable replication overhead.   The interaction between logic should be given to the interactive decoupling module instead of being implemented in its own interface. In other words, only the interactive decoupling module knows the interaction between all interfaces, and the interface only does what it knows. Otherwise, interface A and interface B must know exactly what each other is doing to be able to do their own thing correctly. assume that both interface A and interface B modify the state of a resource. Interface A must perform an if (Conditionx) do something in doing an operation, domyownthing; interface B also executes the IF (conditiony) do anotherthing according to the logic of a. Domyownthing. The greater the number of interfaces coupled, or the more coupling resources between the coupling interfaces, the later maintenance and expansion will be a nightmare to cope with.   for the interaction between logical blocks decoupling, or in layman's words, the module decoupling, you have what kind of opinion, please ask!    The logic in the program is mainly three categories: 1. Get the value: Gets the value from the database, network, or object. If the database or network access is stable enough, it can be seen as a simple fetch value, database access and network access are transparent to the obtained value; 2. Detection value: Whether the detection value is legal, is usually the precondition check, the intermediate state check and the post-result check; 3. Set (copy) value: Set the value in the database, object, or send data and instructions to the network. If the database or network access is stable enough, it can be seen as a simple setting value, database access and network access are transparent to the set value;   These three types of logic can be called logical elements. The specific business logic is to encapsulate logical elements into logical blocks, and effectively control the temporal interaction and resource allocation of logical blocks. Timing control is unreasonable and resource scarcity leads to errors and anomalies. Network communication error is due to limited network bandwidth resources. Both programs update a shared variable at the same time, which results in an incorrect result if the timing is not controlled.  how to deal with exceptions? Please refer to "How to make error log more convenient to troubleshoot problems ", and carefully summarize the various causes of software errors and how to prevent and locate. Of course, there are some complex software errors, such as transactions and concurrency, limited to the development experience is shallow, can not give effective solutions and measures, need to learn and deepen according to practice.    in the case of identified design scenarios and business logic, how to write code with fewer bugs:   Concise notes + contractual programming + shorter logic blocks + multiplexed Public Libraries + rigorous testing   1. Write a concise comment before the method: method use, receive parameter, return value, note, author, time. 2. Contractual programming: Write the pre-condition check at the entrance of the method and write the post-result verification at the exit of the method; 3. Writing and maintaining short logic blocks, easy to test for human brain capacity disposable processing; 4. Reuse a reliable public library that has been rigorously tested; If the library is not well tested, it has good use to help it add tests;5. Rigorous testing is performed on the code you write, if it is not a logical element.    

How to express and maintain large-scale logic

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.