Summary
This lesson closes the eighth unit and, with it, the course. It gathers what was achieved in the three lessons of the unit; it rebuilds the chain of concepts that runs from the interface of a module to debugging as a method; it separates what of all this holds in any language from what is specific to C++; it shows the violation of the one-definition rule that nobody diagnoses and gathers in a list all the undefined behavior of the course, with what detects each case; it repeats in its exit assessment the skills of the entry assessment; it proposes six challenges that combine the unit; and it ends with the final project of the course, specified precisely, with its rubric, in four stages, each with a checked reference solution.
Learning objectives
- Organize a program into several modules with headers, guards and contracts, compile it separately and diagnose the messages of the linker.
- Test a module from its specification, with an automated test program run under the sanitizers, and check with assertions what cannot happen.
- Locate a defect with a method (reproduce, reduce, formulate hypotheses, bisect) and with the debugger, and document the investigation.
- Recognize the undefined behavior that the course has shown, with what detects each case, and the violation of the one-definition rule that nobody diagnoses.
- Integrate the whole course into a project: specify, decompose, build, test, measure the cost and debug a program that manages tabular data.
What the unit achieved
The unit set out to teach how to organize, verify and debug programs that no longer fit in one file or in one's head. The purpose has been fulfilled by relying on what the introduction declared as known: declaration and definition, compilation and linking, the contract, the invariant and binary search. The book catalog program with which the unit opened, which fit in one file, copied parseInt once more and silently omitted “Cien años de soledad” because it wrote < where the specification said “both included”, is rewritten in the last section of this lesson as the final project: four modules with contracts, a main program that answers commands and a test program with twenty-five cases. Two of those cases, “between, boundaries” and “between, one year”, fail with the condition of the original program, and therefore the defect could not come back without anyone noticing.
Multiple files and custom libraries
The lesson Multiple files and custom libraries divided a program into a header that declares, an implementation file that defines and a main.cpp that uses. It presented include guards, which prevent double inclusion within a translation unit; the one-definition rule, which explains “multiple definition” and “undefined reference” and gives inline its current meaning of a permission; separate compilation with -c, which recompiles only the objects whose sources or headers changed; and a custom library with functions from earlier lessons.
Testing
The lesson Testing taught how to derive the cases from a specification, by regions, boundaries and special cases, with the expected result computed another way; how to write an automated test program that reports only the failures and ends with exit code 0 or 1; how to use assert for what cannot happen, knowing that -DNDEBUG erases it together with its condition; how to test at compile time with static_assert and constexpr; and how to run the tests with the sanitizers (the memory error sanitizer, AddressSanitizer, and the undefined behavior sanitizer, UndefinedBehaviorSanitizer). Everything rests on Dijkstra's warning: a test shows the presence of defects, never their absence.
Debugging
The lesson Debugging presented debugging as an experimental method: reproduce the failure, formulate falsifiable hypotheses, decide between them with experiments and bisect the suspect stretch. It developed the instrument (-g without optimization, the configuration of Visual Studio Code on the three systems, a real GDB session, simple and conditional breakpoints, the steps and the call stack) and the strategies that are worth more than the instrument: reducing the case, looking for the last correct point with assertions and starting from the first frame of your own code in the sanitizer report.
Loading the content…
Completed
Sign in to save your progress