Chapter 15. Designing and Implementing a âMinute/Hour Counterâ
![image with no caption](/api/v2/epubs/9781449318482/files/httpatomoreillycomsourceoreillyimages942034.png)
Letâs take a look at a data structure used in real production code: a âminute/hour counter.â Weâll take you through the natural thought process an engineer might go through, first trying to solve this problem and then improving its performance and adding features. Most important, weâll also be trying to keep the code easy to read, using principles from throughout this book. We might take some wrong turns along the way or make other mistakes. See if you can follow along and catch them.
The Problem
We need to keep track of how many bytes a web server has transferred over the past minute and over the past hour. Hereâs an illustration of how these totals are maintained:
![image with no caption](/api/v2/epubs/9781449318482/files/httpatomoreillycomsourceoreillyimages942036.png)
Itâs a fairly straightforward problem, but as youâll see, solving it efficiently is an interesting challenge. Letâs start by defining the class interface.
Defining the Class Interface
Here is our first version of the class interface in C++:
class MinuteHourCounter { public: // Add a count void Count(int num_bytes); // Return the count over this minute int MinuteCount(); // Return the count over this hour int HourCount(); };
Before we implement this class, letâs go through the names and comments to see if thereâs anything we want to change. ...
Get The Art of Readable Code now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.