From 0d869ced500a082e75c3c9d20e7165299024d26f Mon Sep 17 00:00:00 2001 From: Basab Date: Sun, 17 May 2026 13:57:02 +0530 Subject: [PATCH 1/9] add working code examples for extract method technique in c++26 and use idiomatic language features --- simple/cpp/extract-method_after.cpp | 14 ++++++++++++++ simple/cpp/extract-method_before.cpp | 12 ++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 simple/cpp/extract-method_after.cpp create mode 100644 simple/cpp/extract-method_before.cpp diff --git a/simple/cpp/extract-method_after.cpp b/simple/cpp/extract-method_after.cpp new file mode 100644 index 0000000..389ff34 --- /dev/null +++ b/simple/cpp/extract-method_after.cpp @@ -0,0 +1,14 @@ +#include +void printBanner(){} + +std::string getOutstanding() { return "42.00"; } + +[[nodiscard]] std::string formatDetails(std::string_view name, std::string_view outstanding) +{ + return std::format("name: {}\namount: {}\n", name, outstanding); +} + +void printDetails(std::string_view name) { + printBanner(); + std::println("{}", formatDetails(name, getOutstanding())); +} \ No newline at end of file diff --git a/simple/cpp/extract-method_before.cpp b/simple/cpp/extract-method_before.cpp new file mode 100644 index 0000000..3b6272d --- /dev/null +++ b/simple/cpp/extract-method_before.cpp @@ -0,0 +1,12 @@ +#include +#include +void printBanner(){} +std::string getOutstanding() { return "42.00"; } + +void printDetails(std::string_view name) { + printBanner(); + + // print details + std::cout << std::format("name: {}\n", name); + std::cout << std::format("amount: {}\n", getOutstanding()); +} \ No newline at end of file From 9d151484923bfc4030209319313070162285f4e3 Mon Sep 17 00:00:00 2001 From: Basab Date: Sun, 17 May 2026 14:14:22 +0530 Subject: [PATCH 2/9] fix formatting --- simple/cpp/extract-method_after.cpp | 2 +- simple/cpp/extract-method_before.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/simple/cpp/extract-method_after.cpp b/simple/cpp/extract-method_after.cpp index 389ff34..8c992a1 100644 --- a/simple/cpp/extract-method_after.cpp +++ b/simple/cpp/extract-method_after.cpp @@ -1,6 +1,6 @@ #include -void printBanner(){} +void printBanner(){} std::string getOutstanding() { return "42.00"; } [[nodiscard]] std::string formatDetails(std::string_view name, std::string_view outstanding) diff --git a/simple/cpp/extract-method_before.cpp b/simple/cpp/extract-method_before.cpp index 3b6272d..8754083 100644 --- a/simple/cpp/extract-method_before.cpp +++ b/simple/cpp/extract-method_before.cpp @@ -1,5 +1,6 @@ #include #include + void printBanner(){} std::string getOutstanding() { return "42.00"; } From 3a41db0ce66b84d361e82e56c71c2a43b98dc595 Mon Sep 17 00:00:00 2001 From: Basab Date: Sun, 17 May 2026 15:21:19 +0530 Subject: [PATCH 3/9] add examples of substitute algorithm in c++26 --- simple/cpp/substitute-algorithm_after.cpp | 14 ++++++++++++++ simple/cpp/substitute-algorithm_before.cpp | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 simple/cpp/substitute-algorithm_after.cpp create mode 100644 simple/cpp/substitute-algorithm_before.cpp diff --git a/simple/cpp/substitute-algorithm_after.cpp b/simple/cpp/substitute-algorithm_after.cpp new file mode 100644 index 0000000..468af3b --- /dev/null +++ b/simple/cpp/substitute-algorithm_after.cpp @@ -0,0 +1,14 @@ +#include +#include +#include +#include +#include + +std::optional FoundPerson(std::span people){ + static constexpr std::array candidates{"Don", "John", "Kent"}; + + if (const auto it = std::ranges::find_first_of(people, candidates); it != people.end()) { + return *it; + } + return std::nullopt; +} \ No newline at end of file diff --git a/simple/cpp/substitute-algorithm_before.cpp b/simple/cpp/substitute-algorithm_before.cpp new file mode 100644 index 0000000..190bc47 --- /dev/null +++ b/simple/cpp/substitute-algorithm_before.cpp @@ -0,0 +1,22 @@ +#include +#include + +std::string FoundPerson(std::span people) +{ + for (const auto & person : people) + { + if (person == ("Don")) + { + return "Don"; + } + if (person == ("John")) + { + return "John"; + } + if (person == ("Kent")) + { + return "Kent"; + } + } + return ""; +} \ No newline at end of file From b2fc3e4c48c1f05ad0d05e7bdefefb45872bcffc Mon Sep 17 00:00:00 2001 From: Basab Date: Sun, 17 May 2026 15:22:07 +0530 Subject: [PATCH 4/9] use std::print instead of println --- simple/cpp/extract-method_after.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simple/cpp/extract-method_after.cpp b/simple/cpp/extract-method_after.cpp index 8c992a1..2964cd1 100644 --- a/simple/cpp/extract-method_after.cpp +++ b/simple/cpp/extract-method_after.cpp @@ -10,5 +10,5 @@ std::string getOutstanding() { return "42.00"; } void printDetails(std::string_view name) { printBanner(); - std::println("{}", formatDetails(name, getOutstanding())); + std::print("{}", formatDetails(name, getOutstanding())); } \ No newline at end of file From 1f59dc12bfade2424595746627296c9cb8667d2f Mon Sep 17 00:00:00 2001 From: Basab Date: Sun, 17 May 2026 17:37:08 +0530 Subject: [PATCH 5/9] remove comments --- simple/cpp/extract-method_before.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/simple/cpp/extract-method_before.cpp b/simple/cpp/extract-method_before.cpp index 8754083..bf4a234 100644 --- a/simple/cpp/extract-method_before.cpp +++ b/simple/cpp/extract-method_before.cpp @@ -6,8 +6,6 @@ std::string getOutstanding() { return "42.00"; } void printDetails(std::string_view name) { printBanner(); - - // print details std::cout << std::format("name: {}\n", name); std::cout << std::format("amount: {}\n", getOutstanding()); } \ No newline at end of file From 043fd6b1fa5a5e6e24f0141b343fed4e89500847 Mon Sep 17 00:00:00 2001 From: Basab Date: Sun, 17 May 2026 17:37:22 +0530 Subject: [PATCH 6/9] fix formatting --- simple/cpp/substitute-algorithm_after.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/simple/cpp/substitute-algorithm_after.cpp b/simple/cpp/substitute-algorithm_after.cpp index 468af3b..32271fe 100644 --- a/simple/cpp/substitute-algorithm_after.cpp +++ b/simple/cpp/substitute-algorithm_after.cpp @@ -4,10 +4,12 @@ #include #include -std::optional FoundPerson(std::span people){ +std::optional FoundPerson(std::span people) +{ static constexpr std::array candidates{"Don", "John", "Kent"}; - if (const auto it = std::ranges::find_first_of(people, candidates); it != people.end()) { + if (const auto it = std::ranges::find_first_of(people, candidates); it != people.end()) + { return *it; } return std::nullopt; From 4765b8cd9bf1d8e047a838a55e2a2be0d3f5f908 Mon Sep 17 00:00:00 2001 From: Basab Date: Sun, 17 May 2026 17:38:17 +0530 Subject: [PATCH 7/9] add examples of isolating switch statement and using variant and visit instead to apply discount --- .../extract-method_isolate-switch_after.cpp | 53 +++++++++++++++++++ .../extract-method_isolate-switch_before.cpp | 47 ++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 simple/cpp/extract-method_isolate-switch_after.cpp create mode 100644 simple/cpp/extract-method_isolate-switch_before.cpp diff --git a/simple/cpp/extract-method_isolate-switch_after.cpp b/simple/cpp/extract-method_isolate-switch_after.cpp new file mode 100644 index 0000000..8c20ae6 --- /dev/null +++ b/simple/cpp/extract-method_isolate-switch_after.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + + +struct Product +{ + double quantity; + double price; +}; + +struct USDiscount {static constexpr double factor{0.85};}; +struct RUDiscount {static constexpr double factor{0.75};}; +struct CNDiscount {static constexpr double factor{0.9};}; + +using Discount = std::variant; + + +struct User +{ + Discount discount; +}; + +template +struct overloaded : Ts... { + using Ts::operator()...; +}; + +class Order +{ +public: + explicit Order(const User& user, std::vector products) + : user_{user} + , products_(std::move(products)) + {} + + [[nodiscard]] double calculateTotal() const + { + const double total = std::ranges::fold_left(products_, 0.0, [](double acc, const auto& product){return acc + (product.price * product.quantity);}); + return applyRegionalDiscounts(total); + } + + [[nodiscard]] double applyRegionalDiscounts(double total) const + { + return std::visit([total](const auto& discount) { + return total * discount.factor; + }, user_.discount); + } + +private: + User user_; + std::vector products_; +}; \ No newline at end of file diff --git a/simple/cpp/extract-method_isolate-switch_before.cpp b/simple/cpp/extract-method_isolate-switch_before.cpp new file mode 100644 index 0000000..ea2f066 --- /dev/null +++ b/simple/cpp/extract-method_isolate-switch_before.cpp @@ -0,0 +1,47 @@ +#include + +enum class Country +{ + US, + RU, + CN +}; + +struct User +{ + Country country; +}; + +struct Product +{ + double quantity; + double price; +}; + +class Order +{ +public: + explicit Order(const User& user, std::vector products) + : user_{user} + , products_(std::move(products)) + {} + [[nodiscard]] double calculateTotal() const + { + double total = 0; + for (const auto& [quantity, price] : products_) + { + total += price * quantity; + } + switch (user_.country) + { + case Country::US: total *= 0.85; break; + case Country::RU: total *= 0.75; break; + case Country::CN: total *= 0.9; break; + } + return total; + } + +private: + User user_; + std::vector products_; +}; From bfacdcebf7f1a93311d23132e8d20d5b7cd25cba Mon Sep 17 00:00:00 2001 From: Basab Date: Sun, 17 May 2026 17:40:10 +0530 Subject: [PATCH 8/9] use comments to indicate which sections should go into their own files --- simple/cpp/extract-method_isolate-switch_after.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/simple/cpp/extract-method_isolate-switch_after.cpp b/simple/cpp/extract-method_isolate-switch_after.cpp index 8c20ae6..1926f3e 100644 --- a/simple/cpp/extract-method_isolate-switch_after.cpp +++ b/simple/cpp/extract-method_isolate-switch_after.cpp @@ -2,30 +2,33 @@ #include #include - +//Product.h struct Product { double quantity; double price; }; +//Discounts.h struct USDiscount {static constexpr double factor{0.85};}; struct RUDiscount {static constexpr double factor{0.75};}; struct CNDiscount {static constexpr double factor{0.9};}; using Discount = std::variant; +template +struct overloaded : Ts... { + using Ts::operator()...; +}; +//User.h struct User { Discount discount; }; -template -struct overloaded : Ts... { - using Ts::operator()...; -}; +// Order.h and Order.cpp if separating header from source class Order { public: From 9d5530fd16af84bc201ad2ae2fb15c7fa3b8e248 Mon Sep 17 00:00:00 2001 From: Basab Date: Sun, 17 May 2026 18:20:40 +0530 Subject: [PATCH 9/9] remove overloaded template --- simple/cpp/extract-method_isolate-switch_after.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/simple/cpp/extract-method_isolate-switch_after.cpp b/simple/cpp/extract-method_isolate-switch_after.cpp index 1926f3e..788963d 100644 --- a/simple/cpp/extract-method_isolate-switch_after.cpp +++ b/simple/cpp/extract-method_isolate-switch_after.cpp @@ -16,11 +16,6 @@ struct CNDiscount {static constexpr double factor{0.9};}; using Discount = std::variant; -template -struct overloaded : Ts... { - using Ts::operator()...; -}; - //User.h struct User {