Ranges, Code Quality, and the Future of C++

And even without understanding iota, you can still read this and abstractly understand the algorithm.This reduction of complexity is precisely what makes code easier to read.However, there’s a small difference from the functionality of Eric’s example: this version don’t stop at 10 triples..It will print triples forever..How do we modify it to only print 10 triples?The answer is… well, you can’t do it elegantly..The algorithm for generating triples is embedded into the code utilizing the triples (by printing them)..Because of this, in order to limit the number of triples, we have to modify the algorithm itself.Now the code isn’t simple at all..While it doesn’t take quite as long to understand as Eric’s example, the code is no longer trivial to read..How long does it take you to check that line 13 is correct?.Should it be ++count or count++?.Should it check for == or !=?The cognitive overhead of reading this function is now significantly higher..This one algorithm generates triples, counts how many triples were generated, and stops generating when it hits 10..It’s hard to read because this isn’t just one algorithm; it’s multiple algorithms written together.Ranges and CoroutinesThis situation illustrates precisely why ranges are so great (and important)..A range allows you to return the algorithm itself, rather than the data the algorithm generates.. More details

Leave a Reply