Live data from Hacker News

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

read.engineerscodex.com

11–20 of 204 posts

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

#11
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.

C++ makes this (and many other things!) needlessly painful.

In C# it is just

    return numbers.Sum();

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

#12
I wonder how old this advice is. I know that it predates the century but I'm not sure how far back.

(A favorite quote of mine that I believe is from the 1970s says "It's easier to make working code fast then to make fast code work."

Since the fundamental problems of programming have not changed over the centuries, I wouldn't at all be surprised if there's an anti-clever saying from the dawn of computing)

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

#13
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]

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

#14
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.

    int sum = 0;
    for (int i = 0; i 
Pretty much looks the same in all C-like languages. I've written that in Java, Go, TypeScript, PHP, etc...

On the other hand, that second 'clever' example always looks different for every stupid language. It's std::accumulate in C++, streams in Java, list comprehension in python, etc...

Clear is better than clever.

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

#15
post #7
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 like C++, but I don't understand this tradition of keeping these annoying namespaces prefixed to everything. Just get rid of those std::, foo::bar::whatever::, etc from your code and make it more readable. Use the "use" clause. It's very rare for such names to be ambiguous in the same file, unless I'm missing some bigger picture here.

    using namespace std;
is considered kinda bad since you don't want your namespace to be polluted with a bunch of std stuff. For example if you have `int count` lying around somewhere you'd want to be able to call `std::count` without fear of it being shadowed.

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

#16
This is true, but in code reviews and such, it often boils down to familiarity above anything else, like someone preferring

    names = []
    for record in records:
        names.append(record["name"])
to

    names = [record["name"] for record in records]
Now, I might say something if I saw this:

    import operator
    names = list(map(operator.itemgetter("name"), records))
Seems a bit unidiomatic given that list comprehensions are in the language... but probably many disagree.

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

#17
I’ve been a C++ dev for a couple of decades and know my fair share of unreadable code. I’ve recently started learning Python and holy shit, it’s like you get accolades in this language for doing as much as possible in as few characters as possible. Guess I’m getting too old for these young whippersnappers.

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

#18
Yep. I now find it enjoyable to refactor my code to the point of making it feel obvious and straightforward. Not always easy.

For me, it helps imagining some colleague read my code and judge me. And fortunately, I work at a place where I trust people to recognize when something that looks obvious is not actually dumb. Not that I'm so great at dumb but I try at least.

Tooling that allows you to move classes, methods, rename stuff, inline or extract functions efficiently helps a lot. It pains me a bit to write this, as someone whose favorite code editor is a text editor, not an IDE.

I find there's comparable joy in writing. Rephrasing and simplifying a text to find the most efficient / obvious phrasing. I suspect writing software might affect writing in such ways, though I don't think I would be able to tell a developer's writing apart.

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

#20
post #10
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.

We now have return std::reduce(x.begin(), x.end()); Which is a little cleaner and is even faster (compiler is free to do the additions in any order). https://en.cppreference.com/w/cpp/algorithm/reduce - it looks like even the `accumulate` example can be made simpler with `std::plus`. I prefer the `reduce` option for a number of reasons, but understand why someone might not.

I think most people would be thinking "hmm now I need to read what 'reduce' mean in this context"
Post reply on HN