Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions simple/cpp/extract-method_after.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <print>

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()));
}
11 changes: 11 additions & 0 deletions simple/cpp/extract-method_before.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>
#include <format>

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());
}
51 changes: 51 additions & 0 deletions simple/cpp/extract-method_isolate-switch_after.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <algorithm>
#include <variant>
#include <vector>

//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<USDiscount, RUDiscount, CNDiscount>;

//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<Product> 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<Product> products_;
};
47 changes: 47 additions & 0 deletions simple/cpp/extract-method_isolate-switch_before.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <vector>

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<Product> 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<Product> products_;
};
16 changes: 16 additions & 0 deletions simple/cpp/substitute-algorithm_after.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <algorithm>
#include <optional>
#include <string>
#include <span>
#include <array>

std::optional<std::string> FoundPerson(std::span<const std::string> people)
{
static constexpr std::array<std::string_view, 3> candidates{"Don", "John", "Kent"};

if (const auto it = std::ranges::find_first_of(people, candidates); it != people.end())
{
return *it;
}
return std::nullopt;
}
22 changes: 22 additions & 0 deletions simple/cpp/substitute-algorithm_before.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <string>
#include <span>

std::string FoundPerson(std::span<const std::string> people)
{
for (const auto & person : people)
{
if (person == ("Don"))
{
return "Don";
}
if (person == ("John"))
{
return "John";
}
if (person == ("Kent"))
{
return "Kent";
}
}
return "";
}