A simple case of over engineering

Like many of you I work with multiple languages. Being human there is always that inevitable moment where my fingers type faster than my brain.

I had a long coding session in python and switched over to a C++ project, without taking any time in between to make that mental “shift” from one language to the other.

There was a need for a simple loop of numbers, in python this a trivial task using range:

Something like:

x = range(6)
for n in x:
print(n)

Its simple, its elegant, it works — unlike my brain, which after one cup of coffee obviously needed more what stared back at me on the screen looked something like this:

for(auto x : range(10))
{
qDebug(x);

}

This is not work for obvious reasons, the biggest being I am in C++ and there is no “range” function.

Like most of you, my first attempt was to simply do it myself, C++ CoRoutines would do nicely here:

generator<int> generatorForNumbers(int begin, int inc = 1) {

for (int i = begin;; i += inc) {
co_yield i;
}

}

As soon as I typed it, the realization that this project is using C++ 11, not C++20, one thing I will give to C++ is that it does run basically forever.

Fine….google it is: “Range in C++ like Python”

First link: https://stackoverflow.com/questions/13152252/is-there-a-compact-equivalent-to-python-range-in-c-stl

Reading the article it became clear I was not the only one who had this same thought.

If you are reading this, I can only assume you are a programmer and by now you have also come up with about a dozen different solutions

I also posted the question in the VoidRealms facebook group-surely out of 4000+ developers someone knew the answer…

As you probably suspected the answers from the group were all over the place, from more complex to “if you use coroutines…”. Everyone had thoughts on how to accomplish this.

Not a bad solution…but not great….

//Put this code somewhere else...
QList<int> range(int max)
{
QList<int> list;
for(int i; i < max; i++)
{
list.append(i);
}return list;}//In the function we wanted to do this inforeach(int x, range(10))
{
}

hmmmm closer….but it has issues…

#include <vector>
#include <numeric> //std::iota

std::vector<int> x(10);
std::iota(std::begin(x), std::end(x), 0); //0 is the starting number

Maybe if I included Boost…

std::vector<int> x;
boost::push_back(x, boost::irange(0, 10));

We as developers tend to make life a lot harder than it needs to be, and admittedly I spent a lot longer on this issue than I should have. There are really two very simple solutions to this issue:

1 — Don’t treat C++ like Python

for(int i; i < 10; i++)
{
}

2 — Don’t over-engineer the solution

Why are you looking here, I just said don't over-engineer it!

In the end of it all, I spent about an hour diving into parts of the language I really did not need to go for something super trivial. But I did learn a few things, namely how other developers think. We are creative by nature, and we have a deep desire to see just how creative we can be.

If you have zero experience, I would recommend starting with Qt Core beginners. If you already know some C++ start with Qt Core Intermediate. You can also watch all the youtube videos free even without any advertisements.

Also we have a rich community of developers in the VoidRealms facebook group — Im in there all the time asking questions and helping others.

Free tutorials on youtube
https://www.youtube.com/user/VoidRealms/videos

Qt 5 Core for beginners with C++
https://www.udemy.com/course/qt-core-for-beginners/?referralCode=E2115FC6B3CC3BD742AF

Qt 5 Core Intermediate with C++
https://www.udemy.com/course/qt-core-intermediate/?referralCode=1F1A60AF279FEE1719CD

Qt 5 Core Advanced with C++
https://www.udemy.com/course/qt-core-advanced/?referralCode=8DB432110F38B253D251

Qt 5 Widgets for beginners with C++
https://www.udemy.com/course/qt-widgets-beginners/?referralCode=1B2FA9787D88D97D38D3

QML for beginners with Qt 5
https://www.udemy.com/course/qml-for-beginners/?referralCode=3B69B9927B587BBF40F1

Qt 5 Design Patterns
https://www.udemy.com/course/qt-5-design-patterns/?referralCode=3C2C41D87AB3A17B395F

Migrating to Qt 6
https://www.udemy.com/course/migrating-to-qt-6/?referralCode=F4775D810BD9551083B6

Awarded Content Creator in the 2020 Qt Champion Awards:

“Bryan has a few courses available on Udemy, specifically his course on Qt 5 Design Patterns and also has a popular group on Facebook which helps others with their Qt questions as well. For his work alone in creating the Qt 5 training course online for everyone to benefit from it was easy to make him our year’s Content Creator.”

Follow me on:

YouTube: https://www.youtube.com/user/VoidRealms

Facebook: https://www.facebook.com/groups/1400884323467285

Udemy: https://www.udemy.com/user/bryan-cairns/

Computer programmer loves, Qt, Python, Flutter