Skip to content

Exercises related to Improving Software Quality

Find out when precondition is missing in a interface

An example of a precondition is when parameters of a function are expected to meet some requirements, e.g. being positive or non zero. Static analysis tool may detect such conditions and are thus helpful in preventing unexpected software behaviors.

From this perspective, what is the precondition for the following function:

int divisor(int x) {
  return 10/x;
}

Exercice 1

Given that this precondition is not tested, code a use of the divisor() function that is detected as an error by cppcheck. After detection of the error by cppcheck, add a precondition check that prevents the unexpected behavior.

Configuring .pre-commit

Exercice 2

For configuring pre-commit, you must first create a “.pre-commit-config.yaml” file. Explain the content of the file:

  1. What does ^main.cpp under files mean? (hint: check https://regex101.com/)
  2. What is the regular expression for running the hooks on “main.cpp” and on the “my_library” subfolder
  3. What does [--std=c++14] mean?
  4. What are the implications of language: system?