C++

Lesson 08

Algorithm and iterator intent

Use standard algorithms when their names describe the pass over a range more directly than manual counters.

Good Code

src/review_counts.cpp
#include <algorithm>
#include <vector>

int count_published_reviews(const std::vector<Review>& reviews)
{
    // Algorithm name says this pass counts matching reviews.
    return static_cast<int>(std::count_if(
        reviews.begin(),
        reviews.end(),
        [](const Review& review) { return review.is_published(); }
    ));
}

Bad Code

review_counts.cpp
#include <vector>

int count_published_reviews(const std::vector<Review>& reviews)
{
    int total = 0;
    for (std::size_t index = 0; index < reviews.size(); ++index) {
        // Manual counters make the range goal easy to miss.
        if (reviews[index].is_published()) {
            total++;
        }
    }

    return total;
}

Review Notes

What to review

Good Code

The good version says the function counts reviews that match a predicate. The range and predicate are visible without index bookkeeping.

Bad Code

The bad version works, but reviewers must read the loop body to learn that the goal is a count. Manual index code also adds room for off-by-one mistakes.

Takeaways

  • A standard algorithm can make a range operation read as a single reviewable action instead of a custom loop.