trainings/CreatingReliableSoftwareCpp/Presentation/good_practise_kiss.md

1.1 KiB
Raw Blame History

KISS


keep it simple, stupid

  • Writing only enough code to pass a unit test, which relates to TDD and the first SOLID principle Single Responsibility.
  • Intuitive approaches to writing software, approachable and simply understood by everyone.

Small understandable classes and methods

class ImageStorage {
public:
    Image* getImage(const std::string& url) {
        const auto it = std::ranges::find_if(images_, [&url](const auto& image) { return image->url() == url; });
        if (it != std::cend(images_)) {
            return it->get();
        }

        return nullptr;
    }

    Image* store(std::unique_ptr<Image>&& image) {
        if (const auto* image = getImage()) {
            return image;
        }

        images_.push_back(std::move(image));
        return images_.back().get();
    }

private:
    std::vector<std::unique_ptr<Image>> images_;
};