Live data from Hacker News

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

read.engineerscodex.com

81–90 of 204 posts

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

#81
So I’m going to play the devil’s advocate and say that concise code has a readability advantage in that you don’t need to keep track of intermediate variables or other state across hundreds of lines of code or multiple files.

This was/is the promise of languages like APL; those willing to invest in learning arcane and terse symbols can move mountains in a few keystrokes.

I know that for my part, when reading a language I’m familiar with, it’s usually much faster to puzzle out a concise solution than a verbose-in-the-name-of-simplicity one.

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

#82
post #54

Earlier quoted context omitted.

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()

This comment makes no sense. You dismissed the C# code, and then wrote the same thing in Rust, but with an extra non-conceptually-meaningful boilerplate step. May as well ask, "where did x come from, and why are you so sure you can iter().sum() it?" C# has generic types, so yes, C# arrays of numbers have a Sum method. https://stackoverflow.com/questions/2419343/how-to-sum-up-an... Don't make bold dismissive comments…

The C# code ends up relying on LINQ, it's interesting how many C# programmers don't even think about that, either anything they work on already uses LINQ or they just reflexively bring it in everywhere they write C#

You'll see that a few of those SO comments actually say they're relying on LINQ to make that work. The array type doesn't have such a method itself.

So, in reality although many C# programmers will think of this as "correct" it just won't even compile... except if there's already LINQ.

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

#83
post #74

Earlier quoted context omitted.

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.

Agreed, me as well.

The second example reflects how my brain thinks and the intuitive order of what has to happen.

In the first example, it's all out of order.

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

#84
post #74

Earlier quoted context omitted.

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.

I’m sure I’m biased, since I code mainly in Python. List/dict comprehensions read the same to me as the longer form, just more concisely. This can be taken too far, and you can wind up with horribly obtuse one-liners that are just awful.

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

#85
post #77

Earlier quoted context omitted.

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.

Why does one need an uncommon reason to begin development in Fortran?

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

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

One man's clever is another man's clear.

https://web.archive.org/web/20180619022832/http://webdocs.cs...

"The instructions corresponding to a conventional language might be expressed something like the following:

Select an apple from the box. If it is good, set it aside in some place reserved for the good apples; if it is not good, discard it. Select a second apple; if it is good put it in the reserved place, and if it is not good discard it. ... Continue in this manner examining each apple in turn until all of the good apples have been selected. In summary, then, examine the apples one at a time starting with the first one we pick up and finishing with the last one in the box until we have selected and set aside all of the good apples. On the other hand the instructions corresponding to an array language could be stated simply as “Select all of the good apples from the box.” Of course, the apples would still have to be examined individually, but the apple-by-apple details could be left to the helper.

Conventional programming languages may be considered then “one-apple-at-a-time” languages with machine languages being a very primitive form, whereas array languages may be considered “all-the-apples-at-once” languages. In two of the following three sections we shall consider the array languages APL and its “modern dialect” J with an intervening section giving a short discussion of the array language Nial which was influenced by APL."

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

#88
post #63
post #43

Earlier quoted context omitted.

Same in JS. I once worked with someone who would use [].forEach and didn't like for loops because functional good, iterative loops bad. It was essentially in the coding style of the project and the for version would be rejected in MRs. I don't miss this. IMHO [].forEach just essentially expresses the same thing as a regular for loop, although in a more verbose and convoluted way that is also likely very inefficient b…

You are making a strong evidence-free claim about what JS compilers don't do. https://stackoverflow.com/questions/9981607/array-foreach-ru...

At $WORK we use Mozilla Rhino for some JavaScript processing.

We tested the normal loop vs. the clever loop. Performance-wise, the normal loop blew the doors off the clever loop.

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

#89
The knockdown test for too clever is that the original author can't maintain or extend the code later on. You can use this phenomenon to prove to people that they were being too clever. "See? not so easy, and this is all your doing"

Beyond that there is a a tradeoff to be had between utilizing powerful and expressive language features, and alienating less proficient programmers. In industry, redundancy and parallelism (of humans) matters a lot, and so the code has to be dumbed down quite a bit. If you're working solo or with a few highly competent peers, you can afford more cleverness.

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

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

    x = np.arange(0,10,1)
    print(x[x%2==0])
Post reply on HN