C++ Strong Type Arguments – Arduino FTP Client Refactor

Wouldn’t it be great that we could call functions in C++ like this?

create_rectangle(width=10, height=20);

It is definitely more readable than what we usually tend to do:

create_rectangle(10,20);

Is there any reason why the first argument should be the width and the second one the height? Not really, and therefore such function call is difficult to read and is error prone. A strong type enforces that the first argument must actually be of type width and the second one of type height so no error would be possible.

In C++ we can actually achieve something like this:

create_rectangle(Width(10), Height(20));

For this example we could be even more precise and do something like this:

create_rectangle(Width(10m), Height(20m));

The most impressive thing is that in C++ we can achieve this with zero overhead.

Being able to enforce types in our functions and constructor parameters can lead to much readable and less error prone code. In this video we explore the possibilities of a very simple implementation proposed by Jonathan Boccara on this article of his blog.

In the following video we will explain this concept and refactor the constructor for our ftp client library for the Arduino version of the ESP8266.

In the previous article you can find more about the FTP Arduino for ESP8266 client.

Leave a Reply