Earlier quoted context omitted.
Nice dissertation but loops is all you need.
From a certain standpoint, a Turing machine is all you need. (Heh, even Malbolge Unshackled is likely Turing-complete.)
Clever code is probably the worst code you could write (2023)
191–200 of 204 posts
Re: Clever code is probably the worst code you could write (2023)
#192Earlier quoted context omitted.
Unless you got dumber between writing and debugging, it more likely means that it takes twice as long (or even more if you haven't touched it in a bit). It's unlikely that Kernighan meant it takes someone twice as smart to figure out what you were doing as that would be a nonsensical interpretation (someone twice as smart may not be able to figure out what the stupid person is trying to do in the first place if the c…
> It's unlikely that Kernighan meant it takes someone twice as smart to figure out what you were doing no, that is exactly what he meant. clever code means you are just barely able to understand it enough to write it yourself [in any amount of time]. therefore, you aren't going to be able to debug it at all, by a factor of nearly two. and if you are the "smartest" person in the org (which he often was), then you are…
Re: Clever code is probably the worst code you could write (2023)
#193Earlier quoted context omitted.
Are you sure that the compiler doesn’t autovectorize a simple loop into this form anyway? This is kind of the defacto scenario for that pass.
This transformation is only legal if order of summation doesn't matter. So the compiler can do it for ints, but not floats.
Re: Clever code is probably the worst code you could write (2023)
#194Earlier quoted context omitted.
Same for Rust.
I tend to use for loops in Rust for this reason; simple, and understandable by anyone who's programmed in an imperative language. The one-liner approach tends to include an explicit type declaration (Or turbofish), `iter()`, and `collect()`, at minimum.
x.iter().sum()Re: Clever code is probably the worst code you could write (2023)
#195Earlier quoted context omitted.
> It's unlikely that Kernighan meant it takes someone twice as smart to figure out what you were doing no, that is exactly what he meant. clever code means you are just barely able to understand it enough to write it yourself [in any amount of time]. therefore, you aren't going to be able to debug it at all, by a factor of nearly two. and if you are the "smartest" person in the org (which he often was), then you are…
The Linux kernel for example is filled with code that is at the limit of cleverness of the people who came up with it (e.g. RCU or intrusive linked lists). Turns out that once a concept is introduced, people can absorb it and become smarter. It’s just simply categorically not true that something written at the limit of your cleverness at one time prevents you from being able to debug it because cleverness is not some…
never-the-less there is still a lot of truth to the saying, nobody said it was universally categorically true.
i've written clever code when it needs to be clever, for example performance. along the lines of the RCU code. when i do i plan for handling the complexity - static analysis or exhaustive testing if possible.
suddenly needing to advance your cleverness by 2x isn't impossible, but it's not fun if your butt is on the line.
Re: Clever code is probably the worst code you could write (2023)
#196This 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 lan…
Re: Clever code is probably the worst code you could write (2023)
#197Earlier quoted context omitted.
Great example. My personal preference is the first one, but I put that down to me being inexperienced and not working in a professional development environment. I've been using Ruff with most of the rules enabled, and they have a page for this scenario here: https://docs.astral.sh/ruff/rules/manual-list-comprehension/ Another one that trips me up is the ternary operator: https://docs.astral.sh/ruff/rules/if-else-bloc…
Let me see if I can sway you. The nice thing about both the ternary and the list compression is that they become statements of the form derived_thing = some_computation . The code flows better when you’re skimming it at a high level and thinking “then we get this”, “then pluck this”. You can think more about your reformed data and less about how it was reformed. The alternative is that the branching obscures what you…
Re: Clever code is probably the worst code you could write (2023)
#198Earlier quoted context omitted.
>the `accumulate` example can be made simpler with `std::plus` Accumulate, surprisingly enough, accumulates by default: return accumulate(x.begin(), x.end(), 0);
Just make sure you're accumulating integers and not doubles!
#include
#include
#include
using namespace std;
template class Cont>
T sum(Cont x)
{
return accumulate(x.begin(), x.end(), (T)0);
}
int main(int argc, char *argv[])
{
vector ds({1.1, 2.2, 3.3, 4.4});
vector fs({1.1f, 2.2f, 3.3f, 4.4f});
vector is({1, 2, 3, 4});
vector ulls({1ul, 2ul, 3ul, 4ul});
printf("doubles: %f\n",sum(ds));
printf("floats: %f\n",sum(fs));
printf("ints: %d\n",sum(is));
printf("ulls: %llu\n",sum(ulls));
return 0;
}Re: Clever code is probably the worst code you could write (2023)
#199Earlier quoted context omitted.
I think relaxing the ordering requirement let's you use simd something like this (semi-pseudo code) (a, b, c, d) = (0, 0, 0, 0); for(int i = 0; i
What if n is not a multiple of 4? There is a trick I can't recall at the moment for handling this with a case statement on n mod 4 at the end to wrap this up.
Re: Clever code is probably the worst code you could write (2023)
#200Earlier quoted context omitted.
The Linux kernel for example is filled with code that is at the limit of cleverness of the people who came up with it (e.g. RCU or intrusive linked lists). Turns out that once a concept is introduced, people can absorb it and become smarter. It’s just simply categorically not true that something written at the limit of your cleverness at one time prevents you from being able to debug it because cleverness is not some…
agree, but as i said: "he was making a memorable and funny quote, with quite a bit of truth to it, not a precise scientific hypothesis" never-the-less there is still a lot of truth to the saying, nobody said it was universally categorically true. i've written clever code when it needs to be clever, for example performance. along the lines of the RCU code. when i do i plan for handling the complexity - static analysis…