Refactoring Method 41: Introduce assertion (introducing assertions)

Source: Internet
Author: User

A sectionCodeNeedProgramStatus to make certain assumptions.This assumption is clearly expressed with assertions.

Motivation: there is often a piece of code that can run normally only when a condition is true.

This assumption is usually not explicitly presented in the code. You must read the entireAlgorithmTo see. Sometimes programmers write such assumptions in comments. Assertions can be used to clearly indicate these assumptions.

Assertion is a conditional expression and should always be true. If it fails, it is not a programmer who has made a mistake. So the failure of assertion should lead to an uncontrolled exception. Assertions cannot be used by other parts of the system. In fact, the final product of the Program often deletes the assertions. Therefore, marking "Something is an asserted" is very important.

Assertions can be used as an aid for communication and debugging. From the perspective of communication, assertions can help the program reader understand the assumptions made by the Code; from the perspective of debugging, assertions can grasp them from the closest to the bug.

Practice: If the programmer does not make mistakes, the assertions should not have any impact on the system, so joining the assertions will never affect the behavior of the program.

If you find that the Code assumes that a condition is always true, add an asserted to explicitly describe this situation.

Note: Do not abuse assertions. Do not use it to check the condition that "you think should be true". Use it to check the condition that "must be true. Misuse of assertions may cause repeated logic that is difficult to maintain. Adding assertions to a piece of logic is advantageous because it forces you to reconsider the constraints of this Code. If these constraints are not met, the program can also run normally. The assertions will not help you, but will only confuse the Code and may impede future modifications.

You should always ask yourself: If the assertions indicate that the constraints cannot be met, can the code still run normally? If you can, remove the assertions.

In addition, pay attention to the repeated code in the assertion. You can use the extract method (extract function) to remove repeated code.

 

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.