Search code examples
c++typecheckingstatic-typing

C++: "Prefer statically type-checked solutions (when applicable)"


Prefer statically type-checked solutions (when applicable).

(2013) Bjarne Stroustrup, The C++ Programming Language 4th Edition

What does Stroustrup mean with this advice?


Solution

  • "Prefer solutions that can be statically checked."

    Static checking: the bug is found automatically before the program even runs.

    Dynamic checking: the bug is found automatically when the code is executed.

    See: Static Checking (MIT Edu)


    Compile-time checking (when and where possible) is always far better than run-time checking for clarity and performance reasons. You don't need to write error handlers for errors caught at compile time.

    See: P.5: Prefer compile-time checking to run-time checking ( Thanks to @user4581301 )