Live data from Hacker News

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

read.engineerscodex.com

181–190 of 204 posts

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

#181

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…

Yes, but people are widely familiar with certain concepts. Hating "clever code" doesn't mean hating any abstraction or good elegant ideas (like function calls).

The thing is that this stuff has already been in circulation for so long, and many of the good ideas have been found. If you tried to come up with new solutions for function calls, people would understandably be skeptical.

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

#182
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

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)

#183
post #147
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've seen junior devs write code carefully, putting time and effort into it. The code was ornate and almost too over-commented. mid-level devs become more pragmatic, but can skimp on both elegance and simplicity vs complication. What's interesting is that decent senior dev code sometimes almost looks careless, but really works well in the end. For example, immediately exiting with an error instead of complex error re…

Another example is some devs chain maps and array filters when a mundane for loop would do.

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

#184
post #133
post #120

Earlier quoted context omitted.

I think it's mostly a fad issue. Normal loops and if statements are just as possible to hit with a static analyzer. But the very fact that they look easy makes a certain kind of programmer see them as beneath them. They want the complex looking code, even if it's functionally equivalent and semantically no more sound. They like the visual noise and complexity of it. It rubs their egos the right way.

Loops are more complex. They expose more implementation details, worse, they "expose" irrelevant details. Unless your CPU is very simple, like a Cortex M0, the C compiler will likely rewrite your loop using vector instructions (think MMX / SSE / Neon), leaving an unrecognizable mess where a neat loop with an index used to be. C was invented to match PDP-9 and PDP-11, and it matches them beautifully. Constructs like *…

Nice dissertation but loops is all you need.

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

#185
post #24

I would argue that what constitutes clever code varies a lot by language. There's always a "cleverness" threshold where being able to read or refactor the code becomes harder, but this threshold isn't universal. Python in particular makes it very easy to be too clever, since its extremely rigid syntax was designed specifically to discourage it, but it ended up giving the user the necessary tools to be clever anyway,…

Coming from Perl, Python made me write more verbose simple code. But I noticed that in Perl I would do something clever, then a few days later it would come back and bit me in the arse, when I was trying to debug it. Sure Perl saved a few lines of code over Python, but my Python code ended up more reliable. I do miss Perl though.

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

#186

Earlier quoted context omitted.

What would qualify as clever Python? These kind of broad and vague statements make me wonder if I am guilty..

TL;DR - OOP obsession is a common python developer phase. it's a dangerous phase. - maintainable code is not minified code. minified code is minified code. - stoopid code is often stoopid enough when it satisfies real world / human concerns, not technical concerns. ---- I've done all of these, and I see other people repeating them. 1. hyper optimised and utterly fragile class based inheritance / abstractions. Not opt…

I find functional programming is where things get bad. It does give some pretty elegant ways to do things, but it can become a nightmare when debugging. Sometimes I need to dig into Django code to work out what's going on. It's all fine while the code is imperative, but as soon as you hit a functional part, then following what's going on gets difficult.

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

#187
post #178
post #133

Earlier quoted context omitted.

Loops are more complex. They expose more implementation details, worse, they "expose" irrelevant details. Unless your CPU is very simple, like a Cortex M0, the C compiler will likely rewrite your loop using vector instructions (think MMX / SSE / Neon), leaving an unrecognizable mess where a neat loop with an index used to be. C was invented to match PDP-9 and PDP-11, and it matches them beautifully. Constructs like *…

00106 template 00107 _Tp 00108 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, 00109 _BinaryOperation __binary_op) 00110 { 00111 // concept requirements 00112 __glibcxx_function_requires(_InputIteratorConcept ) 00113 __glibcxx_requires_valid_range(__first, __last); 00114 00115 for (; __first != __last; ++__first) 00116 __init = __binary_op(__init, *__first); 00117 return __init; 00118 } https://…

Try -O2 or -O3 like you would in a release build.

See https://stackoverflow.com/a/50786881

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

#188
post #133

Earlier quoted context omitted.

Loops are more complex. They expose more implementation details, worse, they "expose" irrelevant details. Unless your CPU is very simple, like a Cortex M0, the C compiler will likely rewrite your loop using vector instructions (think MMX / SSE / Neon), leaving an unrecognizable mess where a neat loop with an index used to be. C was invented to match PDP-9 and PDP-11, and it matches them beautifully. Constructs like *…

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

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

#189
post #187
post #178

Earlier quoted context omitted.

00106 template 00107 _Tp 00108 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, 00109 _BinaryOperation __binary_op) 00110 { 00111 // concept requirements 00112 __glibcxx_function_requires(_InputIteratorConcept ) 00113 __glibcxx_requires_valid_range(__first, __last); 00114 00115 for (; __first != __last; ++__first) 00116 __init = __binary_op(__init, *__first); 00117 return __init; 00118 } https://…

Try -O2 or -O3 like you would in a release build. See https://stackoverflow.com/a/50786881

Optimization levels aren't going to care if you're using std::accumulate or not.

They'll either analyze the loop directly, or they'll analyze it after munging it through the template function instantiation code and inlining the results of the template function.

After that, both are doing what you originally said, lifting the C into an abstract form and analyzing it for whether parallel computation can be applied.

Hell, the code to SSE enabled asm examples they give in the comment you link are just plain for loops.

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

#190
I think the obvious problem here is the manager's attitude:

While I was proud of it, there was suddenly a problem when I talked to my manager about it.

"While I understand how complex this was, when it comes to performance reviews, this code looks trivial. It looks too easy, too simple. I would recommend writing an implementation doc of this module just so we can demonstrate that this was actually quite complex."

Post reply on HN