A collection of Bad Code Smells in a Catalog form for Developers & Researchers. Code Smell is a typical bad code implementation, and learning these concepts immiedietly makes you a better developer!
Honestly, it is much more code to use loop with non-local control like break, continue etc. than just calling a collect function (which I assume just means to_list). In the above example, in most programming language I know, you don’t even need to collect the result into a list.
Not to mention, large loops with non-local control is a breeding ground for spegatti code.
In many languages, there are type class / trait / interfaces (whatever you want to call them) that allows lazy structures to share the same API as strict ones.
Yeah, in Java calling first() on a stream is the same as an early return in a for-loop, where for each element all of the previous stream operations are applied first.
for (var car : cars) {
if (car.year() < 1977) return car;
}
Not to mention Kotlin actually supports non-local returns in lambdas under specific circumstances, which allows for even more circumstances to be expressed with functional chaining.
These are not quite equivalent. In terms of short-circuiting yeah they both short-circuit when they get the value. But the latter is returning from the current function and the former is not. If you add a return to that first example then they are equivalent. But then cannot be used in line. Which is a nice advantage to the former - it can be used inline with less faff as you can just assign the return to a value. The latter needs you to declare a variable, assign it and break from the loop in the if.
Personally I quite like how the former requires less modification to work in different contexts and find it nicer to read. Though not all logic is easier to read with a stream, sometimes a good old for loop makes the code more readable. Use which ever helps you best at each point. Never blindly apply some pattern to every situation.
Ya, streams may seem tedious (why do I have to call stream and collect?), but it’s like that for performance (and probably backwards compatibility).
If writing readable code is not peformant, then the language implementation needs to be fixed.
Honestly, it is much more code to use loop with non-local control like break, continue etc. than just calling a collect function (which I assume just means to_list). In the above example, in most programming language I know, you don’t even need to collect the result into a list.
Not to mention, large loops with non-local control is a breeding ground for spegatti code.
In many languages, there are type class / trait / interfaces (whatever you want to call them) that allows lazy structures to share the same API as strict ones.
Yeah, in Java calling
first()
on a stream is the same as an early return in a for-loop, where for each element all of the previous stream operations are applied first.So the stream operation
cars.stream() .filter(c -> c.year() < 1977) .first()
is equivalent to doing the following imperatively
for (var car : cars) { if (car.year() < 1977) return car; }
Not to mention Kotlin actually supports non-local returns in lambdas under specific circumstances, which allows for even more circumstances to be expressed with functional chaining.
These are not quite equivalent. In terms of short-circuiting yeah they both short-circuit when they get the value. But the latter is returning from the current function and the former is not. If you add a return to that first example then they are equivalent. But then cannot be used in line. Which is a nice advantage to the former - it can be used inline with less faff as you can just assign the return to a value. The latter needs you to declare a variable, assign it and break from the loop in the if.
Personally I quite like how the former requires less modification to work in different contexts and find it nicer to read. Though not all logic is easier to read with a stream, sometimes a good old for loop makes the code more readable. Use which ever helps you best at each point. Never blindly apply some pattern to every situation.
Well yes, I was simplifying because I wanted to address the main (incorrect) criticism by @spartanatreyu@programming.dev. I agree with your comment