Your API usability does matter. Use modern C++

Your API usability does matter indeed. It is not just some snazzy implementation behind, displayed on GitHub to boast about.
Your API usability does matter indeed. It is not just some snazzy implementation behind, displayed on GitHub to boast about.

Think of your users, clients, and customers. What they see first and judge your code quality for, is your API design.

By using standard modern C++  one can achieve more usable API than ever before.  And without any stunt programming whatsoever.

The Use Case

Consider this.

How simple but effective and comfortable this is. Just print whatever you fancy.  No, it is not the old “bad” std::iostream or its nemesis printf().  If you think this dbj::print is just too simple to be considered as an API, it will surprise you how many people would find this really usable. And really important. And the simpler it is more people will use it.

Also, there is {fmt} official proposal on how to combat the “chevron hell” in a standard way.

API success keyword is “usability”

This dbj::print() we see above, is somewhere in between well hated std::cout and not that well understood printf(). I was hoping not, but I feel, I have to have the obligatory example of how cumbersome and time-consuming it is to use std::cout

There is no semi-serious programmer who has not developed at least some solution to avoid this in her code.  And of course at least half has tried with macros. Variadic macros maybe? Just to realize the enormity of the mistake of not avoiding macros as much as possible.

And yes there is a proportion of developers in the community to be considered something like “streaming zealots”, who would happily type it as it is, using those dreaded “streaming operators ” << .. however crazy that might look.

And to me, they are perhaps not that “crazy”. Because of the “funky” printf format string and characters.  And the black art of using them. Not being used by them.

Hands up those who actually know what are all printf() format characters? Together with modifiers.

The above snippet very likely would not compile. I am not sure. I just can’t remember how to properly use them printf() formating chars, beyond numbers and words. You know, this little familiar stunt:

Multiply the above with the number of platforms your code has to run on. Unless you use simple printf() calls only. I am sure you do …  do you? Obviously, something had/has to be done.

So what have I done?

Do not forget what we need here (to be done right) is just an eminently usable interface to whatever is behind. An API.  It really does not matter in the context of usability where and how the output goes.  As long as it works. And by “works” here we mean — we just do not want it to “get in the way”. And yes, << and >> are seriously getting in the way.

So without further “drama”, here is the simple modern C++ that allows me to have a robust solution that works with std iostreams. Probably seen before, standard no stunts, modern c++ solution.

Modern C++ just makes this surprisingly simple. And what is simple is good. Because it is robust. And it is easy to improve.

For example, I am sure you can easily devise a mechanism where one can output to something different than std::cout, for example. To be switchable either at compile-time or at runtime.

Basically what we have here as a micro wrapper mechanism to the whole std ostream family. Minus the “streaming out operator” >> . With all the ostream goodies intact.

And yes I do realize this is one inline C++ function. That is the beauty of it.