Live data from Hacker News

Ask HN: What is the most beautiful piece of code you've ever read?

news.ycombinator.com

231–240 of 394 posts

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#231
post #201

Earlier quoted context omitted.

I disagree strongly. This code is very maintainable: it does not have dependencies, it is trivial to test, it is wickedly short, and with the appropriate comment there is no confusion regarding its purpose. Also, its field of applicability is clear from the context: replace this code with a call to fsqrt if you happen to have a fast hardware implementation of it. It is the most easily maintainable code, ever!

the one maintainability metric, and i think is the most important one, is whether it's easy to modify (to make it do something slightly different). You'd be hard pressed to write an inverse square cube without basically rewriting the whole function. There's nothing that can be reused. The only saving grace is that it is side-effect free, so replacement is trivial, unlike a lot of other code that's not maintainable.

>inverse square cube

You mean inverse cube root? I believe the geometry ain’t easily upgradeable this way.

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#232

Curiously Recurring Template Pattern (CRTP) [0] in C++. class Derived : public Base [0] https://en.wikipedia.org/wiki/Curiously_recurring_template_p...

That's awesome. I ran into this situation in C#, and looks like it is useful in some cases.

https://zpbappi.com/curiously-recurring-template-pattern-in-...

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#235

Well, it's a C macro in Apache Thrift code base [1] #ifdef __GNUC__ #define TDB_LIKELY(val) (__builtin_expect((val), 1)) #define TDB_UNLIKELY(val) (__builtin_expect((val), 0)) #else #define TDB_LIKELY(val) (val) #define TDB_UNLIKELY(val) (val) #endif This code is beautiful when it deal with CPU cache-line effects to speed up your program. [1] https://github.com/apache/thrift/blob/647501693bd14256df8839...

You seem to be referring to the arguably beautiful effect of code.

Are you seriously praising this monstrosity of preprocessor macros, integers used as magic booleans and compiler specific builtins as beautiful code??

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#237
post #66

I began coding in IBM/LCSI PC Logo. The first line of code I ever wrote was: FD 100 That's the "hello, world" of turtle graphics in Logo. While probably not as beautiful as the several splendid examples posted in this thread, that simple line of code changed my world. I could make stuff happen in an otherwise mostly blank monochrome CRT display. Until then I had seen CRTs in televisions where I had very little contro…

The true beauty of Logo comes in the realization that you can add your own words to it, and that all these specialist words you devise are themselves first-class citizens of the language, of syntax and status equal to its generic builtins. Hence:

    TO CIRCLE
    REPEAT 360 [FD 1 RT 1]
    END

    TO FLOWER
    REPEAT 20 [CIRCLE RT 18]
    END
and so on ad infinitum, until you arrive at a complete custom vocabulary that concisely and precisely expresses the particular concepts and behaviors of interest and importance to you. In doing so, you move from thinking algorithmically (which is glorified spaghetti) to thinking compositionally, which is the key to scalability – managing complexity as your needs and ambitions grow.

Whereas Algol-y languages treat user-defined vocabulary as second-class citizens, beneath their own privileged built-ins. Which is a ridiculous of status when you consider which is actually important to the user: precise, powerful, tailored words that describe their particular problem, or the crude primitive undifferentiated building blocks that the language dumps out of the box?

The beauty of bottom-up programming (as any Lisp fule kno:) is that endlessly tests your own understanding of the problem domain: to define effective, productive words you must have some idea of what you’re talking about; you can’t help but learn the foundations of the problem space as you go. There’s a basic humility to this approach; there’s nowhere to hide laziness or ignorance.

Whereas in top-down programming it’s much too easy for highly-educated highly-paid absolute know-nothings to bullshit eternally, constructing great theatrical class architectures; grand mechanical castles in the sky that look all very difficult and impressive to observers while never saying anything relevant or useful.

That key switch from algorithmic to compositional thinking is not a natural conceptual leap for self-learners – it takes a carefully directed prod at a particular point in the learning curve to jump those rails – but it opens up worlds. #PlatosCave

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#238
post #87
post #76

Someone once posted a C program here on HN, where each line, even the comments, seemed to "line up" in 6- or 8- character blocks with a space in between. It made the whole program look sort of like a table. I felt it was sort of like poetry. I unfortunately no longer have a link to it, and once looked very hard but couldn't find it. I would be extremely appreciative of someone else saw it and had a link. If I recall…

Most likely an IOCCC entry, but there are a lot that correspond to this description. Maybe you can find it back looking at previous winners of the contest: http://www.formation.jussieu.fr/ars/2000-2001/C/cours/COMPLE...

I appreciate your help, but I feel like its unlikely to be in there since it wasn't really obfuscated - it was meant to be clear in some sense of the word and had a bunch of comments explaining it.

The comments sounded natural, but lined up so each Nth column was a space (or punctuation) all of the way down the code. None of the things I am seeing in that list seem to have quite the same layout.

There was some comment about it like "some people feel code should be beautiful to enjoy debugging" or something along those lines but I don't really remember.

I have been looking for a long time though and really wish I had saved it properly when I first saw it.

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#240
post #23

For me it's a BASIC one-liner which generates mazes 10 PRINT CHR$ (205.5 + RND (1)); : GOTO 10 I found this specific one via slashdot[1], but something similar, which I've never managed to find/replicate, was used to generate mazes on the Atari 800 XL at my school when I was a kid. [1] https://developers.slashdot.org/story/12/12/01/1847244/how-d...

No, it does not generate a maze. It generates a random sequence of \s and /s, that can trigger a maze being generated in a typical user's mind. This is the art of illusion.

Everything is an illusion. \s and /s are just liquid crystal pixels on the screen, filtering some light. However it's easier to say that if something has a name and you can identify it, then it's probably that thing. I see a maze it means it's a maze :)
Post reply on HN