Chapter 4.5

T.1: Use templates to raise the level of abstraction of code

Story time

In 2012 I facilitated a code review with a colleague, let’s call her Beth, which I set aside as a useful example of the field. The purpose of the function was to take a selection of icons arranged in a row along the bottom of the screen and spin through them by an arbitrary amount. We had just migrated to C++11.

After half an hour of hacking away at it, the function looked like this, having lost about a dozen spurious lines of code:

UIComponent* spin_cards(UIComponent* first, UIComponent* n_first, UIComponent* last) { if (first == n_first) return last; if (n_first == last) return first; UIComponent* write = first; UIComponent* next_read ...

Get Beautiful C++: 30 Core Guidelines for Writing Clean, Safe, and Fast 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.