r/cpp • • Dec 15 '21

Modern C++ in Advent of Code

This series, by Šimon Tóth, is a step by step guide through the solutions of Advent of Code 2021 using Modern C++.

242 Upvotes

83 comments sorted by

View all comments

24

u/wheypoint Ö Dec 15 '21

Great to see c++20 in action and nice write ups

I really like that modern c++ is getting these more functional features but tbh i think they aren't what i'd use here. I Think it's great when some raw loop can be replaced by some more readable desciptive algorithm, even better if it leads to shorter code. But do people really think

uint32_t count_increasing_windows(std::istream &input) {
  auto sliding_window_view = std::ranges::istream_view<uint32_t>(input) 
| std::ranges::views::transform([e1 = 0, e2 = 0, e3 = 0](uint32_t curr) mutable {
    e1 = std::exchange(e2, std::exchange(e3, curr));
    return e1 + e2 + e3;
    });
  return std::ranges::count_if(sliding_window_view, 
                           [prev = std::numeric_limits<uint32_t>::max(), drop = 2]
                           (uint32_t curr) mutable {
                              if (drop > 0) {
                                drop--;
                                return false;
                              }
                              return std::exchange(prev, curr) < curr; });}

is more readable than the equivalent super simple index or iterator based for loop?

Now maybe in haskell you could solve this using a nice functional composition of simple functions but i think it's too much for c++ ranges.

7

u/[deleted] Dec 15 '21

Honestly, it's not. I really pushed it and this solution is actually undefined behaviour since the lambdas are not regular invocable.

But I do think that from a software engineering perspective, moving to ugly functional code will be a good move. Simply because it makes static analysis so much more powerful.

I wish we could write beautiful functional code in C++, but I don't think that will ever happen :-D

25

u/[deleted] Dec 16 '21

It's an unreadable mess. I do not for the life of me understand why people have an issue with seeing a for or while loop. They think it is bad code for some reason which is just bizarre to me. It's far easier to read and shows you *what is actually happening*. It is much better than code compressed into chained, filthy functional bollocks.

4

u/[deleted] Dec 16 '21

Do you really consider this:

auto [min, max] = data | filter(&MyClass::isReady) | minmax_element();

less readable than the equivalent loop & if.

10

u/[deleted] Dec 16 '21 edited Dec 16 '21

Absolutely.

In fact, i'd say that is completely unreadable.

That is doing multiple complex things on one line. I know it's fashionable but it creates garbage looking code.

What about when it comes to performance profiling? This has to be unpicked. Or just debugging for that matter. I can't believe people even argue that this is more readable and/or less error prone.

2

u/[deleted] Dec 16 '21

Do you have experience with functional languages? That might be the dividing line between people that consider this more or less readable.

It absolutely is less error-prone. There are so many subtle bugs you can introduce in this code.

For profiling, the profile will be slightly differently structured (because of the view wrapper type), but it should have the same elements.

5

u/[deleted] Dec 16 '21 edited Dec 16 '21

I do. I have some experience and I've come to the conclusion procedural, simple code is more readable. Simple code is less prone to errors. There are certain elements to functional programming that are useful. Like preferring pure functions. But in general it's biggest sin is that its hard to be productive and is full of performance traps.

However, above is bad for a multitude of reasons.

  1. If you are unfamiliar with the functions used you have to jump around the code to find out what is happening. That is context switching and bad for debugging. This happens a lot more in this functional style because of the emphasis on lambdas and small composable functions. Instead of reading the code I have to figure out how the programmer intended to put these composable functions together. Bad!

  2. If there is a bug in one of the chained operations you have to untangle the whole thing to figure out which part. This is time consuming and bad for debugging and I also do not see how it is less prone to errors. More operations are happening than you can reasonably think about here, and if you don't think that then you aren't the only one who is going to read your code.

  3. Bad for compile times as someone else suggested

  4. It shows intent but I don't care about the programmers intent. I care about what the programmer has written. This feels like obfuscation and hand wavy attempts for the person writing it to convince me it does what it says it does. I can't know that just from looking at this line of code, and I will have unpick it to understand. That's not productive.

  5. I also forgot to mention operator overloading which is absolute hell when you are trying to look at the performance of some code.

1

u/fredoverflow Dec 17 '21

I've come to the conclusion procedural, simple code is more readable.

What is your opinion on the Go programming language?

1

u/[deleted] Dec 17 '21

I've not tried it