Search code examples
unit-testingtestingproof

(Broad Question) How can you be certain a piece of code works correctly?


An interviewer asked me how I can be sure that a piece of code works as intended to. I said to test the code through all the possible test cases. Are there any other ways you can be certain a piece of code would work? I was also thinking of writing a mathematical proof for the code but I'm not sure if that would apply to every type of code.


Solution

  • First, there are two aspects of "code works as intended to": It works as specified (verification, building the product right) and works in way that is useful (validation, building the right product).

    You have mentioned proofs, which belong to verification. In addition, in the realm of verification are also certain types of testing, for example unit-testing. Then there are less formal verification methods like static code analysis. You can also count in reviews (if they are against a specification). And many more.

    Also for validation there are many approaches, like building prototypes of acceptance testing. In the end, this always goes in the direction of "show something to the customer to see it goes in the right direction." You can even put incremental development from the agile development world in this category.

    I very much like this article about the topic: http://www.easterbrook.ca/steve/2010/11/the-difference-between-verification-and-validation/