65 points · by andrewstuart · 4y ago · 54 comments
Does anyone have any suggestions for such examples? Any language is fine. As long as there's some way to say "look, here is where people say this is great code".
https://www.gnu.org/software/mes/manual/html_node/LISP-as-Ma...
It's terrible code from a software engineering perspective though. The readability is roughly that of x86 Assembly. The density obfuscates what is actually happening. If someone opened a pull request containing one of these function implementations, I would probably assume they're trying to insert a backdoor somehow. Either way, I'd never accept code like this.
Great code is simple and clear in a human sense, not just minimal in a conceptual or computational sense.
Old time programmers can't get past the low resources of old systems so they continue to prefer clever and tight code over simple and clear code. Last I was in school many of my teachers preferred clever code so that's the coding style we strived to copy. I suspect that's still the gold standard. But I agree, simple and clear in a human sense, is much better. Specially in a team environment where we need to read other's code.
Having to explain your code is just wasted effort and time.
well, it's an eval/apply loop, which by definition, is going to be able to execute code!
I dont think it's obfuscated at all. Of course, reading it requires knowledge of how the read/eval loop works in lisp - something the viewer might not have studied, but that doesn't make it obfuscated.
Not deliberately obfuscated, true. But any code that contains tokens like "CAR", "caar", "CDR", "cdar", "CONS", "caddr", etc doesn't belong in a modern production environment where auditability and maintainability are paramount. What's the goal here, shortest possible text? Why not just replace each of those with a Unicode Greek letter and save two bytes and three characters each?
If that means Lisp itself is the problem, so be it. The assumption that "software engineers" in the late 1950s somehow designed the holy grail of programming languages is laughable anyway.
w for w in words if w in WORDS
There's just two concepts there, w and words, doing a whole lot. I mean, that's literally a filter, but Python refuses to embrace these things because they're supposedly confusing or hard. In F#, that would be: List.filter (fun word -> List.contains word WORDS) words
I would of course rename WORDS and words to be something distinct rather than just differ by capitalization. The Python is confusing because the way to trace this comprehension is by starting with the w in the middle, then the w at the end, and then the w at the beginning (i.e., 2-3-1 sequence).Also, I highly dislike using lines of code as some sort of ultimate measurement. The program is what it is, and then requires a page 5 times the length of the program to explain the program. The program should be clearer or more comments added to it or both.
The program also uses Python's "feature" of evaluating default arguments once when the function is defined as a sort of global variable, because the P function is never used by passing in a value for N. Seems clever, which is the problem with it. Lastly, function names should be more clear. "known", "P", and honestly the others are not good function names.
filter(lambda x: x in WORDS, words)
would I think do pretty much the same as what he wrote, and then depending on how you use the output, you may want to wrap that in list() or something. If you are going to iterate over it, it's gtg as-is.There are much more idiomatic and clear (imo) ways of definining a function-static block (which gets evaluated just the first-time a function is called) than that default arg trick.
Although, F# has list comprehensions as well.
[for word in words do if List.contains word WORDS then word]
That's verbose because I spelled out word, but I feel it reads well while the Python version is weird to me. Mathematical set definitions follow the form of {e <- set | <condition on e>} which is read as "e in set such that e satisfies condition" or "for e in set and e satisfies condition". The Python version kind of reads weird to me as it's more like {e | e <- set | <condition on e>}. (I note that Counter is some iterable object and not a list, I suppose.) I really like comprehensions, but I always find Python's to be a bit strange. I think it's because the "in" in the posted comprehension has two semantic meanings, if I understand correctly. It's a nitpick for sure, but slippery is the best way I can describe Python.> then depending on how you use the output, you may want to wrap that in list() or something
Shudder. Haha.
Yea, the default argument trick threw me off, which is why I don't think it's a good example of the "best" code. The best code doesn't have tricks.
It's a neat domain program, but not the "best" code.
words.filter { WORDS.contains($0) }.forEach { w in
...
} array_map(fn($match) => print($match), array_filter($words, fn($_) => in_array($_, WORDS)));
or less verbosely: array_map(fn($match) => print($match), array_intersect($words, WORDS));https://betterexplained.com/articles/understanding-quakes-fa...
Also a lot of 4k demo scene have some great looking videos rendered purely by a small amount of code.
In the year 2033 Earth was discovered by Survey Fleet MCXII of the Galactic Empire. The Emperor ordered Earth to send a representative to the court, with strict instructions to “bring something beautiful”.
Emperor: What beautiful things does Earth have?
Earth Representative: Your excellency, our mathematician Euler proved in our year 1737 that
+/(1+⍳∞)*-s ←→ ×/÷1-(⍭⍳∞)*-s
Emperor: What is the ⍭ symbol?Earth Rep.: ⍭i is the i-th prime.
Emperor: And what is ∞? Does it do anything useful?
Earth Rep.: It denotes infinity, your excellency.
Emperor: (ponders equation for a minute or two) Respect!
Emperor: Neat notation you have there. Tell me more.
Earth Rep.: Your excellency, it’s called APL. It was invented by the Canadian Kenneth E. Iverson…
⍳ is range 0…n–1
÷ is reciprocal of everything to its right
×/ is product of everything to its right
+/ is sum of everything to its right
At least, that's how my CS prof 20+y ago described it to us :)
The C 'Hello, world!' program documented the style of a whole programming language in 5 lines of code. Every computer engineer who sees it can now recognize C code. Even non -programmers understand vaguely what's going on.
Just about every programming language today starts with the same example. That's how wildly succesfull it became as demo.
This isn't just a small implementation of the C standard library, it's an absolute joy to read and usually every easy to understand. It takes real talent to write code which is both this legible and efficient.
Often when I'm trying to understand what glibc is doing I'll read the musl version of a function and that will give me an indication of what the glibc source is trying to communicate.
He was one of the Gang of Four who wrote Design Patterns, but he wrote this library years before the book came out. It always seemed to me that he invented all the main patterns in the Interviews library before they were subsequently (nicely!) documented in the book (which went on to become a smash hit).
I've since realised that I much prefer functional programming (Clojure!) over OO, BUT, oh my goodness, what a truly groundbreaking and beautiful bit of work it was for its time. I read every line of code in that library, and I became a much better programmer for it.
So much wasted human-centuries of software labor.
I think of things like the PowerBuilder datawindow widget, something that HTML+JS couldn't touch for 15 years until I started to see things like ExtJS and jquery. And it ran on 486s and pentiums.
As for the original question, practically all the superuseful UNIX utils that had to be performant on ancient MHz-class hardware is amazing stuff.
You could charge a credit card with just 7 lines of code.
But I've heard good things about the Fast Inverse Square Root Calculation algorithm in the Doom 3 source code. https://www.youtube.com/watch?v=p8u_k2LIZyo