diff --git a/simple/cpp/extract-method_after.cpp b/simple/cpp/extract-method_after.cpp new file mode 100644 index 0000000..2964cd1 --- /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::print("{}", 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..bf4a234 --- /dev/null +++ b/simple/cpp/extract-method_before.cpp @@ -0,0 +1,11 @@ +#include +#include + +void printBanner(){} +std::string getOutstanding() { return "42.00"; } + +void printDetails(std::string_view name) { + printBanner(); + std::cout << std::format("name: {}\n", name); + std::cout << std::format("amount: {}\n", getOutstanding()); +} \ No newline at end of file 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..788963d --- /dev/null +++ b/simple/cpp/extract-method_isolate-switch_after.cpp @@ -0,0 +1,51 @@ +#include +#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; + +//User.h +struct User +{ + Discount discount; +}; + + +// Order.h and Order.cpp if separating header from source +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_; +}; diff --git a/simple/cpp/substitute-algorithm_after.cpp b/simple/cpp/substitute-algorithm_after.cpp new file mode 100644 index 0000000..32271fe --- /dev/null +++ b/simple/cpp/substitute-algorithm_after.cpp @@ -0,0 +1,16 @@ +#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