118 points · by kick · 6y ago · 127 comments
Fastai has a guide how to write Whitney-style code in Python [2].
Thre is a relevant thread in HN [3].
[1] https://github.com/kevinlawler/kona/wiki/Coding-Guidelines#t...
https://code.jsoftware.com/wiki/Essays/Incunabulum
I think a lot of the reason people have difficulty is that you need to read the macros first; jumping into the middle won't make any sense. The macros are very mnemonic and there's only 5 of them. For example, I found the DO macro to be particularly elegant because it expresses "repeat something n times", and one of the features of array languages which makes them terse and powerful is this implicit repetition.
But http://dbp-consulting.com/tutorials/debugging/linuxProgramSt...
does suggest zeroing the top-most frame (%ebp) and making sure the stack is 16-byte aligned might also be worth doing. (Possibly it’s already 16-byte aligned already because you jmp to main instead of calling it though?)
a=arg1 b=arg2 k c.kPersonally I think stuff like the DO macro (F/Fj in this code base https://bitbucket.org/ngn/k/src/master/k.h) should be used everywhere to the extent it is part of the C standard. If compilers were aware of it, there are significant optimizations which could be made. It's dirt simple and saves considerable typing and eliminates lots of potential for error.
DO(count(v)) v[i]=i;
DO(3,4) a+=i*10+j;
DO(3,4,5) a[i][k]+=b[i][j]*c[j][k];
Where i,j,k are first, second and third indices. So i in the last example takes values 0..2, for each i value j takes values 0..3 and for each j value k takes values 0..4.OK, which is more readable:
(setf (car lst) "value")
Or: (rplaca lst "value")
I think the one which allows me to learn "setf's destination is the first parameter" and forget the rest is easier by far. setf is glorious macrology: It allows people to write arbitrarily complicated datatypes and have assignment just work, reliably.But the problem with this code isn't the macros. It's the fact the author types like they're being charged by the character and taxed for whitespace. The macros are amazing to the extent they allow this sort of thing. Blaming them for it is wrong, but judging by the only standard that matters, readability, this is not a good coding style.
In the absence of familiarity, minimizing the number of moving parts and factoring out repeated patterns still has intrinsic value for aiding understanding.
I explained myself right below my examples.
> In the absence of familiarity, minimizing the number of moving parts and factoring out repeated patterns still has intrinsic value for aiding understanding.
And now you're repeating what I said.
I dunno about you, but I have very little working memory in my head, just a few items, and only one focus of attention with a narrow field of view.
I can’t read that style of code, but opening a traditional codebase with part of the code here using lots of symbols to achieve a tiny amount of work and the next piece of work is four screens away, using a lot of symbols to achieve a small amount of work, it’s like declaring legalese as the most readable kind of English because it bloats so big it must be readable.
judging by the only standard that matters, readability, this is not a good coding style
What about composable idioms and conventions which let you do a lot of data transforms with a little attention and time?
What about a small amount of code to maintain and refactor? If this style saved you ten lines it might not help but where’s the cutoff that you’d start to be interested - if it saved you 50% of the work? 75%? 99.999999%?
If “big count per line of code is constant” this ought to be an exceptionally low bug style, right?
What about having less code to demonstrate correctness of?
What about having little code to throw away and rewrite if you don’t like it, leasing to a faster refactoring cycle time and a better expression of your intent after s set time? Or leading to readability simply being less important because the cost of rewriting 50 lines from scratch is so much lower than rewriting 10,000 lines from scratch?
Or if you’re writing documentation and there’s an order of magnitude less code to write about?
Or if you should write as many lines of test code as real code, and there’s an order of magnitude less code to test?
The whole style isn’t just “golf the character count”, but “collapse down the size of everything so one human attention covers a lot more of it, like zooming out to get a higher overview”.
Crossing down from several files to one file is a phase transition, crossing from one long file to one screen, is.
Right. Which is why readable, mnemonic names are so important. Single-character names are good for array indexing, because their scope is so small you can see the whole region they're used in a fraction of a screen (usually) but variables which get used over a larger fraction of the codebase need names which explain what and why they are.
> What about composable idioms and conventions which let you do a lot of data transforms with a little attention and time?
Great. That doesn't preclude meaningful variable names.
> What about a small amount of code to maintain and refactor? If this style saved you ten lines it might not help but where’s the cutoff that you’d start to be interested - if it saved you 50% of the work? 75%? 99.999999%?
Great. That doesn't preclude meaningful variable names.
> If “big count per line of code is constant” this ought to be an exceptionally low bug style, right?
If that's true to an unlimited extent, IOCCC programs should be nearly self-debugging, with how short they are.
> What about having less code to demonstrate correctness of?
Testability is about each bit of code doing one thing, not doing ten things in one line. You can test one thing. If you try to test ten things at once, you have 1024 possible ways for that test to come out, assuming Pass/Fail is completely binary, as opposed to real testing, which is more like Pass/Fail/WTF.
> What about having little code to throw away and rewrite if you don’t like it, leasing to a faster refactoring cycle time and a better expression of your intent after s set time?
Throwing code away doesn't hurt my feelings.
> Or leading to readability simply being less important because the cost of rewriting 50 lines from scratch is so much lower than rewriting 10,000 lines from scratch?
I can't rewrite what I can't read. I would struggle to rewrite code I struggle to read.
> Or if you’re writing documentation and there’s an order of magnitude less code to write about?
Writing documentation based on LoC count is deranged.
> The whole style isn’t just “golf the character count”, but “collapse down the size of everything so one human attention covers a lot more of it, like zooming out to get a higher overview”.
And we circle back around: Lisp macros allow me to zoom out like that by letting me factor out common patterns more easily, like collapsing all possible "assignment pattern" instances to a setf invocation. I don't see the guts of how assignment is done for that particular kind of data when I'm focused on the other bits of the codebase. It's out of focus for me then, and it collapses neatly into one little form.
But, and here's the big sticking point for me, I can do all that without variable names which look like line noise.
* A proprietary array processing language
* A variant of APL with elements of Scheme
* K serves as the foundation for kdb+, an in-memory, column-based database
* Advocates of the language emphasize its speed, facility in handling arrays, and expressive syntax.
1: https://ngn.bitbucket.io/k.html
2: https://en.wikipedia.org/wiki/K_(programming_language)
Edit: some examples of idiomatic code (I assume...)
* https://github.com/KxSystems/cookbook
f:{a+::x}.[a:10]
{[x]a::.`a+x}
f 30
40
f 30
70
f 30
100
.f
[a:100]
Naturally, if you have not explicitly re-bound the closure of a function, .f gives you access to that function's natural scope: a:99
99
b:"text"
"text"
.{}
[prm:{[x]{[x]$[x;,/x ,''o'x ^/:x;,x]}@$[-8>@x;!x;x]};in:{[x;y]~^y?x};a:99;b:"text"]
Nifty, eh?oK also supports unlimited lexical closure, which is a bit unusual among K interpreters; usually they either eschew closure entirely for simplicity or (as in k3) allow functions to close over a single scope.
nsl.com
kparc.com
Telemetry?
All the famous spyware I'm aware of which you had to have initiated the action to install includes an EULA which does mention the data collection. That doesn't make it any less spyware.
i'm not trying to purposefully copy [Arthur's] style. i've always been trying to write shorter code - there are public traces of how that evolved in ngn/apl and in dyalog's ride. i acknowledge that after seeing some of arthur's code, including b, my mental threshold of what is considered acceptable dropped significantly. once you accept certain principles about the code, for instance that it's more important to be able to hold it at once in your head than to be able to explain it to the uninitiated, this style becomes more efficient and more pleasant
...
if you seek simplicity, this style is to a large extent discovered (as opposed to invented)
complicated things can be arranged in many ways. simple in few. that's why it always looks like entropy is increasing in the physical world :)
https://chat.stackexchange.com/transcript/message/52953241#5...
Fewer semicolons means fewer statements. Apart from simplifying the structure of ones code overall, this also drives writing in a somewhat more functional style. You will find yourself taking advantage of the comma and ternary operators more, if you don't already. Macros are another tool for decreasing repetition. So is expressing programs in a "data-oriented" style, using lookup tables instead of explicit conditionals. If something is used only once, inlining it can reduce the number of distinct statements.
As people in this and similar threads mention - it is easier to read; in fact, it is easier to read than even the preprocessed output, if you are familiar with the style and concepts.
Even though you don't understand this language/style, doesn't mean a user may not want to refer to source code, or someone may want to contribute to the project.
It only took ten years for a non-toy one to come about! (Less if you count the one that got sued off the internet from the Morgan Stanley employee.)
Coding in a terse style really does have some benefits. You can understand more program flow at one time and you can easily see larger patterns.
Another recent discussion https://news.ycombinator.com/item?id=21890259
https://bitbucket.org/ngn/k/downloads/k (Linux binary 64-bit)
https://bitbucket.org/ngn/k/get/master.tar.gz (Source code)
No Javascript required
0:"path" /read lines
"path"0:("line1";"line2") /write lines
1:"path" /read bytes
"path"1:"content" /write bytesonce you accept certain principles about the code, for instance that it's more important to be able to hold it at once in your head than to be able to explain it to the uninitiated, this style becomes more efficient and more pleasant
I'm still not sure that this terse style in C improves reading. I agree that lots of concise idioms present in APL family of languages do improve reading (once you picked them up), and I do like lots of short functions more than a number of long functions (another contrast to IOCCC, where you don't see many functions). But those languages are tailor made to maximize this aspect. You for example don't think much about loop invariants in APL and friends because they are mostly implicit, and in turn you get very good refactorability. Would it improve other non-array languages? I doubt.
It did seem a little "off" to me, but I couldn't put my finger on it; thanks for describing what was wrong with it.
> It is likely that you can actually understand the thing with only proper renaming.
Fair point, but I usually like to keep my use of renaming to understand code for the decompiler :)
It’s important that many/most cultural norms are not optimal, they are arbitrary. So don’t immediately discount something for looking weird.
That part I have some experience with, and I think that's not the case. I was taught APL (in a CS course) along with a hundred or so other students, and it was nearly universally disliked; IIRC, it only resonated with some of the EE people. The department's administration was even written in APL, and nobody wanted to touch it. After finishing my assignments, I've never wanted to look at it again. So I, for one, doubt the style is universally likable.
I agree; it's definitely a niche thing. It takes quite a while to get into it. Even if you understand the basics; if you don't like 'puzzling with code', you won't be idiomatic. And most people don't like puzzling with code; they want to get things done. You need to do quite a lot with it to start recognising the idioms, but once you do it's a blast. And it makes both reading and writing quite easy. That's why I think doing something like the advent of code in one of these languages will help a lot; I had to quit this year because a project at up my time I reserved for aoc19. The exercises I did though where cool to solve in k.
The point of this style isn’t obfuscation, it’s clarity and compactness. The compactness boosts the signal to noise ratio and allows the programmer to retain more information in working memory. I haven’t programmed C like that and wouldn’t, but I definately understand and respect the theory behind it.
I have on the other hand written q like that, at first skeptical of the style. But after a couple days, I wouldn’t have it any other way. Being able to fit your entire program on a single screen has pretty profound productivity benefits. It also makes IDEs unnecessary.
For other code structured like this, check out Kona, J or kdb+/q.
In general, I don’t think refusing to believe is a very useful attitude. See: Chesterton’s Fence.
When I first saw examples of how terse K and APL code gets, my thought was “obviously, the language was designed to solve this specific example or it wouldn’t be this terse”. But after the 100th or so significantly different example, one can’t NOT admit that the problems are simple, and common practices are just incredibly bloated..... which leads some (me for example) to adopt a much terser style.
False. I can read it just fine. Ergo, it is readable.
You cannot read it, and that is a different problem.
> I wonder why
Because there are benefits you do not understand.
It is very strange to me that you assume someone who does something doesn't get anything out of it simply because you don't get anything out of it.
https://news.ycombinator.com/item?id=22009831
> It is very strange to me that you assume someone who does something doesn't get anything out of it simply because you don't get anything out of it.
I don't believe I assumed that?
This is clearly your lack of ability here.
Understanding why someone else makes a difference choice can be illuminating. Given that the author of the original k has written at least 5 or 6 versions of the language interpreter from scratch every time in this style, he must be doing something right. Right?
Not necessarily, but others have pointed out reasons why one might write code this way.
I tend to label code that makes me do that "unreadable"; it's not that I literally can't read it (I routinely deal with worse) but it requires more effort than I consider reasonable for the medium it is presented in.
https://bitbucket.org/ngn/k/src/master/m.c
It is plain to any developer that this fits all definitions of "unreadable" and honestly I think in this case it falls on you to explain why you think it isn't.
False.
I am a developer. It is readable to me.
> I think in this case it falls on you to explain why you think it isn't
Why code isn't unreadable?
What a silly question!
You simply don't know how to read it.
If you do not know Java, you might say all Java code is unreadable. Why would you expect any other language to be different?
[0]: https://bitbucket.org/ngn/k/src/master/m.c
[1]: https://github.com/jsoftware/jsource/blob/master/jsrc/v2.c
There's good reason as to why C written by APL programmers looks as it does, and shallowly dismissing it does a disservice to everyone.
However it does not naturally scale to teams. But each way of working has its own strengths and weaknesses; if you are aiming for large teams working together on large projects, I would not recommend this. That is not because of readability, but because the code is so terse that you would, even with 2 people, be changing the same code a lot of the time, so it's more efficient to do it alone or in pair programming (which does work well; also remotely in an APL-like in my experience).