The foreign gods said: the potential danger of using If statements in programming, foreign if

Source: Internet
Author: User
Tags to domain

The foreign gods said: the potential danger of using If statements in programming, foreign if

In most programming languages, if statements have two main functions: verifying input to protect the domain from the impact of wrong data, and processing the business logic in the domain. However, Udi Dahan pointed out in his recent speech at the Amsterdam DDD European conference that we generally seldom manage the risks of using if statements to process logic from the business or domain perspective.
When we shop online, we browse different products and carefully read the details of some of them. When you find the item you want to buy and add it to the shopping cart, we also move from the interactive query function to the command function. Dahan believes that one of the important questions we should ask is what causes the command to become invalid. He also stressed that we must differentiate technology failures (for example, network server crashes or deserialization errors, which should be addressed by the infrastructure) and logical invalidation (for example, adding items that have been deleted from the product directory to the shopping basket ).
A common case of Dahan's cooperation with customers is to check whether any project has been deleted. For him, one step to deal with problems such as deleted projects is to differentiate private data and public data and compare them with the content management system. In the content management system, you can edit pages and content, and then press the "publish" key to publish information. When processing private data, you can add, update, or delete a project at will until you are satisfied with the project.
In terms of processing public data, we should use business logic to replace and check and delete, and we should carefully consider why a project should be deleted. Taking a product that is no longer sold as an example, it may be better to create a specific command to mark the product as not sold, rather than using some form of temporary or real deletion.
The potential problem with this solution is race condition ). That is, after the customer adds the item to the shopping basket, the item is marked as not for sale. Then, when the customer wants to settle the bill, the product cannot be purchased. For Dahan, looking at the problem in this way is very narrow, data-centric, and limited to a certain point in time. On the contrary, he describes this problem as a typical scenario where multiple participants operate on the same object simultaneously.
Dahan believes that CQRS should be considered when multiple participants in the Collaboration field operate on the same data at the same time. When CQRS is applied to domains, it is recommended to simplify it as much as possible and design a solution to ensure that commands applied to domain logic after verification will almost never expire. This means that when processing commands, we must be prepared to fail in the race condition but still meet the overall business objectives. Generally, this can achieve final consistency from a commercial perspective, but it is not technically consistent (for example, the final synchronization between the read model and the write model ). For example, a customer adds a non-sold item to the shopping basket.
One solution to this problem is to use the long-term operation process behind the scenes. When the user no longer operates the shopping basket or times out, remove the items that have been discontinued from the shopping basket. Eventually, the item is removed from all shopping baskets and therefore not sold.
When we look at the system, we may find many if statements that check the status in some form. For each if statement, we should check whether other participants may change the status checked by the if statement. In this way, we can find potential collaboration situations and requirements for long-term operation processes. However, Dahan pointed out that we should be careful not to use too many long-term running processes. It is not omnipotent.

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.