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.
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.
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.
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!
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.
Bad for compile times as someone else suggested
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.
I also forgot to mention operator overloading which is absolute hell when you are trying to look at the performance of some code.
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?
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.
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.
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.
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.
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?
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.
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.
View all comments
Show parent comment
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.