Live data from Hacker News

Clever code is probably the worst code you could write (2023)

read.engineerscodex.com

71–80 of 204 posts

Re: Clever code is probably the worst code you could write (2023)

#71
post #4

I also find that, in C++, int sum = 0; for (int i = 0; i is a lot easier to understand than return std::accumulate(x.begin(), x.end(), 0, [](int a, b) {return a + b;}); Yet, the latter is considered more correct and better, with static analysis like cppcheck telling you to use the latter. It does have many advantages, like no mutable variables lying around, but gee it is annoying to read.

It's mostly the problem of the language, and not of the approach.

In a more expressive language you can omit the explicit slicing (x.begin(), x.end()) and have the compiler derive the lambda's signature for you, so you'd write something like reduce(x, (a, b) => a + b), or even fold (+) x, with all the same static analysis and efficient compilation guarantees.

Re: Clever code is probably the worst code you could write (2023)

#72
post #4

I also find that, in C++, int sum = 0; for (int i = 0; i is a lot easier to understand than return std::accumulate(x.begin(), x.end(), 0, [](int a, b) {return a + b;}); Yet, the latter is considered more correct and better, with static analysis like cppcheck telling you to use the latter. It does have many advantages, like no mutable variables lying around, but gee it is annoying to read.

I haven’t written any C++ since the 90s. That is unrecognizable. Is that an anonymous function?

Yes.

https://learn.microsoft.com/en-us/cpp/cpp/lambda-expressions...

Re: Clever code is probably the worst code you could write (2023)

#73
It's like no one in the comments here read the piece. Honestly, it's a bit like the author himself didn't.

The big thing in this article is the dangerous and incompetent manager. People like that in a company could destroy the entire thing, and yet it's sort of just mentioned in passing like "oh that's funny". It's not funny, it's absolutely terrifying!

Re: Clever code is probably the worst code you could write (2023)

#74
post #4

I also find that, in C++, int sum = 0; for (int i = 0; i is a lot easier to understand than return std::accumulate(x.begin(), x.end(), 0, [](int a, b) {return a + b;}); Yet, the latter is considered more correct and better, with static analysis like cppcheck telling you to use the latter. It does have many advantages, like no mutable variables lying around, but gee it is annoying to read.

That is somewhat annoying, yes. I'm a huge fan of Python's list comprehension, but it's generally accepted to be a normal part of the language, and IMO, more readable: print([x for x in range(10) if not x % 2]) [0, 2, 4, 6, 8] vs. l = [] for x in range(10): if not x % 2: l.append(x) print(l) [0, 2, 4, 6, 8]

Call me crazy, but I find the second example more readable.

The first example is long line and you have to go back and forth to understand it. The second example is a single top down pass.

Re: Clever code is probably the worst code you could write (2023)

#75

Earlier quoted context omitted.

C++ makes this (and many other things!) needlessly painful. In C# it is just return numbers.Sum();

Where did "numbers" come from, and why are you so sure you can Sum() it? The original C code offered has some data structure (perhaps an array?) called x. Do C# arrays have a Sum method? I don't think so. In Rust you would probably just write: x.iter().sum()

> Do C# arrays have a Sum method? I don't think so.

It's not literally a method (it's not, for instance, in the vtable of some array class), but as far as their API is concerned, yeah, they do: https://github.com/dotnet/runtime/blob/main/src/libraries/Sy...

Re: Clever code is probably the worst code you could write (2023)

#76
post #59

Earlier quoted context omitted.

IEnunerable of course does not have Sum. Only collections of numeric types (including arrays) have Sum. https://learn.microsoft.com/en-us/dotnet/api/system.linq.enu...

So you can't use IEnumerable .Sum if T is a custom type implementing operator+ ?

[deleted]

Re: Clever code is probably the worst code you could write (2023)

#77
post #4

I also find that, in C++, int sum = 0; for (int i = 0; i is a lot easier to understand than return std::accumulate(x.begin(), x.end(), 0, [](int a, b) {return a + b;}); Yet, the latter is considered more correct and better, with static analysis like cppcheck telling you to use the latter. It does have many advantages, like no mutable variables lying around, but gee it is annoying to read.

I haven’t written any C++ since the 90s. That is unrecognizable. Is that an anonymous function?

C++98 should be forgotten as a bad dream. C++17 is almost a sane and convenient language (as long as you remember about the boiling depths into which you can fall if you're careless).

C++ will live for very long, like Fortran. And also like Fortran, there now must be a serious and uncommon reason to start a new project in it.

Re: Clever code is probably the worst code you could write (2023)

#78

I think “clever” is more related to unfamiliarity. There is actually a lot of cleverness going on that people just become familiar with. Structured programming is actually very clever if you think about it. Function calls are, when you look at it closely, very clever. It encapsulates how to jump to a function entry point, how to pass on values in registers or in memory, how to adjust stack pointers, and all other sor…

I think there's a difference between "clever" and complex. You can express a complex algorithm or pattern with simple easy to understand code - complexity doesn't have to manifest itself as unreadable or incomprehensible code. To me "clever" code is more about they way you are doing something than the complexity of what you are trying to do. Clever is the opposite of straightforward and easy to comprehend without a d…

I also prefer this perspective. I had an epiphany about this sort of code when I was trying to describe what my code did for a research paper, and in trying to express why I was proud of it, I called it complex, only for my research advisor to point out that complexity was not the point of the work, so calling it complex did not convey what about it made the research interesting.

Although it wasn't his intention, it changed my perspective on the "clever" tricks I liked using, since it made me realize that being clever was not the kind of complexity that mattered. So, nowadays I try to write simple, easy to understand code, leaving the cleverness and complexity to the way the problem is tackled.

Re: Clever code is probably the worst code you could write (2023)

#79
post #59

Earlier quoted context omitted.

IEnunerable of course does not have Sum. Only collections of numeric types (including arrays) have Sum. https://learn.microsoft.com/en-us/dotnet/api/system.linq.enu...

So you can't use IEnumerable .Sum if T is a custom type implementing operator+ ?

You could, but .Sum is a little more specific than that - it is non-overflowing sum.

Historically, constraining generic arguments on addition was problematic - the full feature set of numeric types was "lifted" to be fully representable through generics only recently[0].

With that said, there is an open proposal[1] to introduce additional generic math overloads to IEnumerable methods, but it hasn't seen much activity as the existing overloads cover most commonly used numeric types already.

[0]: https://learn.microsoft.com/en-us/dotnet/standard/generics/m...

[1]: https://github.com/dotnet/runtime/issues/64031

Re: Clever code is probably the worst code you could write (2023)

#80
post #2

Here's an old joke about the progression from junior to mid-level to senior developer: Junior dev: My code is simple, straightforward, and easy to understand. Mid-level dev: My code is clever, innovative, expressive, hyper-optimized, and ingenious. Senior dev: My code is simple, straightforward, and easy to understand. In software development, "clever" solutions are like poems. In the best poems, there are usually mu…

I don't think it's accurate, though. Junior devs often write overly complicated code because they don't really understand the problem they're trying to solve. Junior devs write unintentionally complex code, mid-level devs write intentionally complex code, senior devs write simple code.
Post reply on HN