Live data from Hacker News

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

read.engineerscodex.com

191–200 of 204 posts

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

#191
post #188

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

You got me

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

#192
post #132

Earlier 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…

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 something fixed and static.

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

#193
post #182

Earlier 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.

Applying std::reduce to floats would be a similarly bad idea for having the compiler do it, except people will write it without thinking. Of course, people always struggle with writing correct floating point so I doubt it makes things better or worse, but it’s conceptually the library equivalent of -fast-math (which coincidentally would also auto-vectorize the loop but can impact more than just that).

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

#194

Earlier 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.

FYI I think you've misinterpreted the parent. To my eyes it looks like they're agreeing that Rust makes summing a list easy using combinations. For example, this is how I would do it in Rust:

  x.iter().sum()

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

#195
post #132

Earlier 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…

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

#196
post #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 lan…

list-map-itemgetter is actually the fastest solution in Python. Should actually promote this.

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

#197
post #52
post #25

Earlier 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…

This is my choice. So easy to get lost in the sea of intermediate veriables inside a loop. If stuff can be rewriten in list comprehensive separately then I will do it.

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

#198
post #65

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

#199
post #99

Earlier 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.

I assume you're thinking of: https://en.wikipedia.org/wiki/Duff%27s_device

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

#200
post #195

Earlier 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…

Sure. I wasn’t advocating for making all your code clever. But clearly super clever code doesn’t make code magically not debuggable. It may not be fun to touch but usually you just make sure it’s well tested, documented, & then you try to never touch it again.
Post reply on HN