verification code

During an interview, or when writing code at ordinary times, is it correct that you are not sure about the code you write? How to protect?
After searching the information on the Internet, I got:
1. I want to test the case, and use the input and output of the test case to verify the correctness of the code?
2. For loop structure, we can use loop invariance (ie loop invariant) to verify, loop invariance is somewhat similar to mathematical induction,
after loop invariance is determined:
1. Before the loop starts, the loop invariant is satisfied;
2 . Before a certain iteration (loop), the loop invariant is satisfied, and (after the loop ends) before the next iteration (loop), the loop invariant should also be satisfied;
3. After the loop ends, the loop is not variant is satisfied

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325655849&siteId=291194637
Recommended