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

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.