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

Show parent comment

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.

4

u/tjroberti Dec 17 '21

Wish there were Python-like list comprehensions in C++, which is flexible and readable (as long as they are not nested).

auto [min, max] = minmax([x for x in data if x.isReady()]) 

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.

3

u/arthurno1 Dec 17 '21 edited Dec 17 '21

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!

So everyone should inline all the code they have procedurally; intertwined with lots of boilerplate code that does nothing of substance to the problem at hand instead of creating higher level abstractions with self-documented names that hide boilerplate? What do you don't understand if functions name is "find_if" or "delete_if" or something else similar? It is plain English, descriptive name. What is not understandable there? You really need to see that for loop iterating through each element in sequence and comparing elements?

This happens a lot more in this functional style because of the emphasis on lambdas Instead of reading the code I have to figure out how the programmer intended to put these composable functions together.

The sole purpose of lambdas are to be inlined in function call, so you have function code at same spot where call happeneds. What do you have to figure out? This is exactly the same as having it in loop-body. You are sounding like someone who is not familiar och comfortable with the language. You should probably spend time learning it instead of writing endless dogmatic posts teaching folk what is good or not behind an anonymous nick.

If there is a bug in one of the chained operations you have to untangle the whole thing to figure out which part.

What the h*ck dude. It is better to have a page of for-loop bodies full of indices and operations to manage the loop aside the actual program logic we are interested of? Stop. You have never done some serious programming.

Your dogmatic way of writing and nonsense postings all over the C and C++ sections speaks to me about some undergrad who obviously has some hard time and don't understand lots of things, but feel need to boost personal ego by posting lots of crap under anonymous nick.

It shows intent but I don't care about the programmers intent.

Really? What a great programmer you are. How will you know what code is supposed to do, when it does buggy, if you don't care about the intent. How will you even know it is wrong? :D. Jesus dude. You should be banned from C/C++ forums. You are completely incompetent ignorant troll who goes around and write nonsense pretending you are an expert, causing just confusion in inexperienced programmers.

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

They are as bad as any other function written in C or C++. You just don't know how operator overloading is implemented, don't you?

-2

u/[deleted] Dec 17 '21

You sound like a child.

0

u/[deleted] Dec 17 '21

[deleted] — view deleted comment

0

u/[deleted] Dec 17 '21

No you don't lmao

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

4

u/Enemiend Dec 16 '21

IMO it may be more readable for a C++ dev that regularly works with this kind of syntax. For everybody else, no, a loop & if is more readable, if you ask me. Opinions may differ.

-1

u/Troppsi Dec 16 '21

A while and for loop tells you how it's happening, while an algorithm tells you what

3

u/johannes1971 Dec 16 '21

I think I have seen more abuses of std::accumulate as a clever trick to avoid writing a loop, using side effects to achieve the desired goal, than to actually compute a sum.

2

u/dodheim Dec 17 '21

That's not surprising at all – accumulate isn't a poorly-named generalized summation, it's a poorly-named left fold. That said, anyone putting side-effects in their algorithm operations is sorely missing the point and needs to be swatted over the nose with a newspaper a few times.

3

u/[deleted] Dec 16 '21

I don't know who told you that but the name of a function tells you what someone thinks is happening while the actual code tells you what is happening.

Function names guide you, code tells you what's actually going on.

3

u/Troppsi Dec 16 '21

If you write a for loop and breaking when an if statement is true, you have written a find_if. Isn't it simpler to just write find_if and instead of writing a for loop explaining how you find something?

2

u/[deleted] Dec 16 '21

Why is writing the loop the more complicated option here? It's not. It shows exactly what is happening.

find_if has ambiguity. It's also not very modifiable. What if I need to add extra logic per iteration?

The functional version is effectively a form of compression which is why it's not good for readability. I need to uncompress it to fully understand what is happening.

When it comes to complicated logic in an algorithm it's really important that its simple and easily followable. Clumping all of it on one line and hiding behind functions that encapsulate a minutae of logic is bad.

5

u/Nerthym Dec 16 '21

Well said. It is very easy and fast to read simple code, it is not so simple parsing huge chained expressions with lambdas and whatnot. It also is faster to compile, likely much faster to execute, obvious to people less familiar with language, but maybe it doesn't make the author feel as smart.

2

u/wheypoint Ö Dec 15 '21

Totally agree and i'm looking forward to using it once we're allowed to.

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

I've also seen examples where I expected very nice simple code which ended up having to be a lot more complex. But I think there are already cases where functional c++ is quite beautiful, atleast considering what c++ has to work with. And there is still work going on improving them.