From f8d9ac5d13dd37b8b57af0478e77ba1e75c813aa Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Tue, 11 May 2021 13:05:39 +0200 Subject: Adding zenservice code --- docs/cpp-coding/09-Considering_Correctness.md | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 docs/cpp-coding/09-Considering_Correctness.md (limited to 'docs/cpp-coding/09-Considering_Correctness.md') diff --git a/docs/cpp-coding/09-Considering_Correctness.md b/docs/cpp-coding/09-Considering_Correctness.md new file mode 100644 index 000000000..5bc8b61ec --- /dev/null +++ b/docs/cpp-coding/09-Considering_Correctness.md @@ -0,0 +1,30 @@ +# Considering Correctness + +## Avoid Typeless Interfaces + + +Bad Idea: + +```cpp +std::string find_file(const std::string &base, const std::string &pattern); +``` + +Better Idea: + +```cpp +std::filesystem::path find_file(const std::filesystem::path &base, const std::regex &pattern); +``` + +The above is better but still suffers from having implicit conversions from `std::string` to `std::filesystem::path` and back. + +Consider using a typesafe library like + + * https://foonathan.net/type_safe/ + * https://github.com/rollbear/strong_type + * https://github.com/joboccara/NamedType + +Note that stronger typing can also allow for more compiler optimizations. + +* [Sorting in C vs C++](Sorting in C vs C++.pdf) + + -- cgit v1.2.3