Visiting the "Dutch Code" in C++
The "Dutch Code" discussion is circulating in programming circles. On Medium, an article by Tom Smykowski prompted me to look closely at the code. That led to implementing it with a different approach in C++. After the first approach, I simplified the code. Both sets of code are below.
"Dutch Code" Controversy
Here is the controversial code rewritten in C++ to avoid dealing with wide character strings. The purpose of the function is to generate a string for a status bar, e.g., battery charge level or screen brightness. The input is a percentage as a floating point value between zero and one.
std::string GetPercentageRounds(double percentage) {
if (percentage == 0) { return "oooooooooo"; }
if (percentage > 0.0 && percentage <= 0.1) { return "*ooooooooo"; }
if (percentage > 0.1 && percentage <= 0.2) { return "**oooooooo"; }
if (percentage > 0.2 && percentage <= 0.3) { return "***ooooooo"; }
if (percentage > 0.3 && percentage <= 0.4) { return "****oooooo"; }
if (percentage > 0.4 && percentage <= 0.5) { return "*****ooooo"; }
if (percentage > 0.5 && percentage <= 0.6) { return "******oooo"; }
if (percentage > 0.6 && percentage <= 0.7) { return "*******ooo"; }
if (percentage > 0.7 && percentage <= 0.8) { return "********oo"; }
if (percentage > 0.8 && percentage <= 0.9) { return…