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 clang-tidy. After detection of the
error by clang-tidy, 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:
- What does
^main.cppunderfilesmean? (hint: check https://regex101.com/) - What is the regular expression for running the hooks on “main.cpp” and on the “my_library” subfolder
- What are the implications of
language: system?