All posts in "Expressive code"

“auto to stick” and Changing Your Style

Published September 28, 2018 - 19 Comments

While performing a code review on a refactoring project, I stumbled upon a change that took a line of code from this state: Widget myWidget{42}; to that: auto myWidget = Widget{42}; Well, in the actual case the type wasn’t called Widget and the initialization value wasn’t exactly 42. But that’s the gist of it. What’s […]

How to Write Simple Code to Accomplish Complex Tasks

Published September 11, 2018 - 5 Comments

Today’s guest post is written by guest author Miguel Raggi. Miguel is a Computer Science and Math professor at UNAM, Mexico’s largest university. He loves clean, expressive, performant C++ code (and strives to convince students to write it in this way!). Miguel is the author of discreture, an open source C++ library to efficiently generate […]

How to Deal with Values That Are Both Input and Output

Published September 7, 2018 - 9 Comments

Passing inputs and getting outputs from a function is pretty straightforward and uncontroversial: inputs get in as function arguments by const reference (or by value for primitive types), outputs get out via the return type. Output function(Input1 const& input1, int input2); Now this is all well, until input-output values get in the picture. An input-output value is […]

Function Poisoning in C++

Published September 4, 2018 - 9 Comments
function poisoning C++

Today’s guest post is written by Federico Kircheis, a (mainly C++) developer in Berlin, always looking how to improve himself, and finding interesting problems to solve. Federico talks to us about a little known compiler feature that could have an impact on how you design code: function poisoning. Also interested in writing on Fluent C++? […]

Modern C++: 7 Ways to Fake It Until You Have It

Published August 31, 2018 - 6 Comments
modern C++ fake it until you have it

Do you wish you had a later version of C++ in your production code? If you do, you’re not alone: a lot of C++ developers today don’t work with a compiler that supports the latest version of the standard. It could be for many reasons: perhaps you have a lot of legacy code to migrate, […]

How to Design Early Returns in C++ (Based on Procedural Programming)

Published August 24, 2018 - 12 Comments

Travelling back from ACCU conference a couple of weeks ago, one of the insights that I’ve brought back with me is from Kevlin Henney’s talk Procedural Programming: It’s Back? It Never Went Away. It’s surprisingly simple but surprisingly insightful, and it has to do with early return statements. Early return statements are controversial in the programming […]

Default Parameters With Default Template Parameters Types

Published August 10, 2018 - 1 Comment

There is a particular case for default parameters: it’s when their type is a template type. Even though the idea is similar to the regular default parameters, there are some subtleties that are worth mentioning. To illustrate this, I will use the example of map_aggregator, the output iterator for aggregating data into a map, for […]

Should I Use Overloads or Default Parameters?

Published August 7, 2018 - 2 Comments

“Should I use overloads or default parameters”, haven’t you asked yourself that question? When designing an interface where the user can leave the value of an argument up to the API, two approaches are possible: Using a default parameters: void drawPoint(int x, int y, Color color = Color::Black); And using overloading: void drawPoint(int x, int […]