TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Literate programming is much more than just commenting code

173 点作者 increscent大约 3 年前

30 条评论

falcolas大约 3 年前
My favorite literate program still has to be the <i>book</i> &quot;Physically Based Rendering&quot;. An optimized, feature rich ray tracer in the form of a textbook.<p>That said, I wouldn&#x27;t personally want to try and collaborate on such a program with more than one other person. It would make for a great single-contributer OSS library though. Rubber duck debugging built right into the prose.
评论 #30762442 未加载
评论 #30762217 未加载
svat大约 3 年前
I would go further: literate programming is not just &quot;much more than&quot; commenting code, because you can do LP without commenting much. The main thing in LP is the idea&#x2F;orientation of writing as if you&#x27;re writing something for a human reader. This does often lead to more comments, but even something like &quot;here&#x27;s the code&quot; followed by lots of code <i>can</i> be LP, if you deem it sufficient for your intended audience. (Earlier comment of mine about target audience and not over-commenting: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=29871047" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=29871047</a>)<p>This works well for people who are writers by nature (like Knuth who&#x27;s always making edits and improvements to his books <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30149221" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30149221</a>). One problem though (and there are several) is that because this is so personal, nearly everyone who seriously tries LP ends up writing their own LP tool (including the author of this post!).
评论 #30770841 未加载
antirez大约 3 年前
I think likewise. When I had to write the radix tree implementation for Redis I faced two problems:<p>- I needed a stable implemention as soon as possible, I had a performance issued that needed to be solved by range queries.<p>- The radix tree was full of corner cases.<p>So I resorted to literate programming, which is in general very near to my usual programming style. You can find it in the rax.c file inside the Redis source code, as you can see as the algorithm is enunciated, the corresponding code is inplenented.<p>Other than that I wrote a very extensive fuzzer for the implementation. Result: after the initial development I don&#x27;t think it was never targeted by serious bugs, and now the implementation is very easy to modify if needed.
评论 #30767862 未加载
评论 #30773302 未加载
BeetleB大约 3 年前
The problems one will run into with literate programming:<p>1. Lack of tooling.<p>2. Refactoring becomes nontrivial<p>3. How one would write a program in literate style will vary widely from person to person. If you write your code in literate style, it may be easy for you to follow it years later and modify it, but it likely will not be the case for a coworker. If they have to modify the code, the cognitive load will not be too different from that of just dealing with well written code.<p>Disclaimer: I&#x27;ve written two nontrivial programs literate style that I continue to rely on and occasionally modify years after writing them. It works as advertised.
评论 #30768695 未加载
评论 #30770869 未加载
评论 #30762129 未加载
yumiris大约 3 年前
Literate programming has been particularly useful for my &quot;dotfile&quot; configurations, such as .emacs, .vimrc, .zshrc and even the .gitconfig file.<p>I use one .org file to declare all of my configurations, and tangle them together into the aforementioned files. This keeps things pretty portable, and makes up for the unintuitive readability of many dotfiles.<p>It can also work for rudimentary shell scripts and other single-file goodies; however, scaling it to proper multi-file programs proves to be difficult, especially when multiple developers are involved.
评论 #30762339 未加载
sritchie大约 3 年前
Literate programming is going to feel far more powerful when we expand the definition to include:<p>- Smalltalk-ish things like writing suites of custom viewers for various types, - demos and examples in-line inside of a library - multiple stories about the same piece of code, but all with the ability to IMPORT the story as a library<p>I&#x27;ve been writing sicmutils[0] as a &quot;literate library&quot;; see the automatic differentiation implementation as an example[1].<p>A talk I gave yesterday at ELS[2] demos a much more powerful host that uses Nextjournal&#x27;s Clerk[3] to power physics animations, TeX rendering etc, but all derived from a piece of Clojure source that you can pull in as a library, ignoring all of these presentation effects.<p>Code should perform itself, and it would be great if when people thought &quot;LP&quot; they imagined the full range of media through which that performance could happen.<p>[0] sicmutils: <a href="https:&#x2F;&#x2F;github.com&#x2F;sicmutils&#x2F;sicmutils" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sicmutils&#x2F;sicmutils</a><p>[1] autodiff namespace: <a href="https:&#x2F;&#x2F;github.com&#x2F;sicmutils&#x2F;sicmutils&#x2F;blob&#x2F;main&#x2F;src&#x2F;sicmutils&#x2F;differential.cljc#L21" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sicmutils&#x2F;sicmutils&#x2F;blob&#x2F;main&#x2F;src&#x2F;sicmuti...</a><p>[2] Talk code: <a href="https:&#x2F;&#x2F;github.com&#x2F;sritchie&#x2F;programming-2022" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sritchie&#x2F;programming-2022</a><p>[3] Clerk: <a href="https:&#x2F;&#x2F;github.com&#x2F;nextjournal&#x2F;clerk" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;nextjournal&#x2F;clerk</a>
评论 #30770775 未加载
QuikAccount大约 3 年前
I like literate programming in theory but the most common response I see to it is that writing self documenting code is better because as you are working on a code base with many people, it is unlikely they will keep your prose up to date as the code is changed.
评论 #30762422 未加载
评论 #30761630 未加载
评论 #30761753 未加载
评论 #30763166 未加载
评论 #30762287 未加载
lf-non大约 3 年前
I am not a big fan of the complex literate programming style involving code-generation which this article talks about.<p>But I recently discovered that Google&#x27;s zx [1] scripting utility supports executing scripts in markdown documents and I combined it with httpie [2] and usql [3] for a bit of quick and dirty automation testing and api verification code and it worked out pretty well.<p>I imagine for most people nowadays jupyter or vscode notebooks are the closest it comes to practical literate programming.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;zx#markdown-scripts" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;zx#markdown-scripts</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;httpie&#x2F;httpie" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;httpie&#x2F;httpie</a><p>[3] <a href="https:&#x2F;&#x2F;github.com&#x2F;xo&#x2F;usql" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;xo&#x2F;usql</a>
andrewshadura大约 3 年前
ifupdown, the Debian tool to manage network interfaces, used to be written in literate C using noweb. When I took it over from the original author, I struggled to understand how it worked. I had to print out the weaved version of it, and read it making notes on the paper. I eventually managed to make sense of it, but making any change was very difficult, so I ended up converting it to plain C, adding some comments from the original literate source and reindenting.
foxdeploy大约 3 年前
This talked about writing code for humans then immediately jumped into some arcane mathematic scrawl like the stuff when Sephiroth casts supernova
评论 #30761999 未加载
vim-guru大约 3 年前
I&#x27;ve written a fair share of literate code.<p>It works well for personal stuff where you would like to leave some bits of information for yourself (typically, configuration files).<p>It works well for small libraries where good documentation is important.<p>It works well for visualisation-work, where you may combine multiple languages and data-formats without writing API&#x27;s for each.<p>In larger scale apps though and with collaboration; you run into problems with tooling on multiple levels. I am working on tackling scale, but collaboration is tricky. Mostly because you need structure to collaborate and then you will likely end up with an outline that&#x27;s pretty close to a directory-tree and then you&#x27;ve lost one of the good bits of literate code in my opinion.
atweiden大约 3 年前
I’d like to see a literate programming version of GitHub where the community standardizes around an eminently-readable Markdown-like syntax. srcweave [1] looks like a great start.<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;justinmeiners&#x2F;srcweave" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;justinmeiners&#x2F;srcweave</a>
评论 #30762247 未加载
评论 #30763734 未加载
rektide大约 3 年前
Building a Habitable Computing Environment[1] was a recent blush i had with a &quot;literate&quot; computing project, this time less about programming specifically &amp; about system setup&#x2F;config.<p>I confess I&#x27;d rather forgotten what literate specifically meant beyond code comments describing the flow, but i did find it to be a remarkably comprehensive &amp; understandable document, a prime example of how we might teach &amp; understand computing. Even if it did leave me puzzling out what a number of the many many many scripts were for!<p>Certainly the overall project of computing needs a lot of help, ways to explain itself. Ive seen tons and tons and tons of &quot;dotfiles&quot; projects, but none have gotten anywhere near to as comprehensible as this literate programming project, from what I&#x27;ve seen.<p>[1] <a href="https:&#x2F;&#x2F;tess.oconnor.cx&#x2F;config&#x2F;hobercfg.html" rel="nofollow">https:&#x2F;&#x2F;tess.oconnor.cx&#x2F;config&#x2F;hobercfg.html</a> <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30748033" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30748033</a> (19 points, 1d ago, 0 comments)
mci大约 3 年前
IMHO, attempts to show literate programming on screen are doomed to meet with mediocre success. DEK invented literate programming with printed books in mind. I dare say that the only successful literate programs are books printed on paper.<p>First, in a printed book, it is easier to find a previous page and compare a fragment on it with the current fragment. Second, a printed book has no links tempting you with the words &quot;CLICK ME&quot; to disrupt the flow so you can read it from cover to cover with fewer distractions. Third, anecdotally, I can see flaws much easier on a printout than on screen, both in programs and in texts.
评论 #30763684 未加载
评论 #30764824 未加载
nonrandomstring大约 3 年前
This is great stuff. It&#x27;s how all code and data research should be presented, where the document is the program and you can reproduce it as easily as you can read it. After years of using Pure Data (a visual dataflow) whose unofficial motto was &quot;The diagram is the program&quot; I got this philosophy stuck deep in my brain. Today I use Org-Mode (In Emacs) for tangling (with something called Babel) that can run source code from many languages as part of an active document.
评论 #30761238 未加载
dwohnitmok大约 3 年前
Are there any large (&gt; 5 people teams) projects written with literate programming?<p>Also are there any IDE plugins or error stack trace&#x2F;debuggers for literate programming?<p>I haven&#x27;t really paid attention to literate programming in a long long time and I&#x27;m curious if the field has advanced.<p>(Also I don&#x27;t understand this: &quot;A typical literate file produces many source files.&quot; Why? Why would you care about having multiple source files? Isn&#x27;t the literate file the source at this point?)
评论 #30762188 未加载
评论 #30762301 未加载
评论 #30761302 未加载
评论 #30761566 未加载
hzhou321大约 3 年前
What are the key differences between a human audience and a machine interpreter? It is not the language or prose. It is the structure and order. For machines, details comes first. You declare all the actors and types with every non-forgiving annotations first. You may tuck the details into a header, but it still needs be ordered according to compilers and structured in the way that machines gets the details first. On the other hand, for human, it is top-down context oriented. The details are important, but not after we establish the right context.<p>So for literate programming, if you just think it is how you write the code (e.g. self-documenting or not), or you think it is the amount of commenting (e.g. doc string or not), if you are not first and constantly thinking about how to structure your code and establish context, you are not getting literate programming.<p>Now, once you understand your ends, the means (tangle or weave), will come along. It is easy to invent one if you don&#x27;t have one. On the other hand, getting your coworkers to agree and work together, that&#x27;s hard. It is easy to get machines to work together and it is easy for human to cope.
评论 #30761836 未加载
评论 #30762467 未加载
copperx大约 3 年前
It would be great if IDEs supported literate programming; the tangle&#x2F;weave commands, simple as they are, create many possible points for navigation. An IDE would be ideal to go back and forth from the prose to the code.
评论 #30761418 未加载
评论 #30763200 未加载
ggm大约 3 年前
I have always felt a literate program is probably for many of us, a future deliverable on the hack we&#x27;ve implemented up front.<p>Very very few people can start from the abstraction and get TO a literate outcome without a lot of false steps along the way.<p>Or, as an alternative, the LOC of a literate program has to include the 100x cost of exploring how to carve it out of the block of mud we start from, including making our own tools.
评论 #30762102 未加载
评论 #30763108 未加载
derangedHorse大约 3 年前
Maybe I&#x27;m missing something, but I didn&#x27;t find his way of programming to be all that more useful than just having well written code. The small code snippets are labeled and shown where they are referenced but this seems to mimic the functionality of functions which, when using an IDE like Visual Studio, can have where it&#x27;s referenced identified through tooling.
评论 #30765361 未加载
silcoon大约 3 年前
Another solution has been implemented in Marginalia. Notes near complete source code.<p>source code: <a href="https:&#x2F;&#x2F;github.com&#x2F;gdeer81&#x2F;marginalia" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;gdeer81&#x2F;marginalia</a> example: <a href="http:&#x2F;&#x2F;gdeer81.github.io&#x2F;marginalia&#x2F;" rel="nofollow">http:&#x2F;&#x2F;gdeer81.github.io&#x2F;marginalia&#x2F;</a>
kkfx大约 3 年前
IMVHO literate programming is &quot;describing an algorithm&quot; like writing a book, witch is absolutely good, but demand much more time than directly writing code. That means: or we change actual &quot;quick&quot; development model to a new&#x2F;old &quot;slow&quot; one, perhaps additive and coherent like classic systems (SmallTalk and LispM systems, the OS as a single application easy to change at runtime, anything available as a function&#x2F;method anywhere) to keep the overall development speed useful enough or there is no room for literate programming.<p>Now, seen actual overall software quality (far less hacky than the past, but also unable to innovate, bloated, with gazillions of deps) we need to change back to days of the real innovation BUT that means we need to completely erase actual economical model centered on giants, witch can be &quot;a little bit&quot; difficult since they are giants and they do not like the idea to be thrown out of the window...
fanf2大约 3 年前
It seems to me that literate programming was partly invented to escape from the rigid structure of Pascal programs; if Knuth had been using a language that allows use before declaration, literate programming would be just comments. Like literate Haskell.
eterevsky大约 3 年前
I tried reading some literate code, and I have troubles understanding it, compared to well structured normal code with moderate amount of comments.<p>Sometimes when you are writing an article it may make sense to write LP-like snippets of code like<p><pre><code> int my_function() { &#x2F;&#x2F; Initialize variables return 0; } </code></pre> but you don&#x27;t really need to invent the whole &quot;literate programming&quot; concept to do this and you don&#x27;t need to write all of your code like that.
shp0ngle大约 3 年前
All bigger programs I have seen that used literate programming were unreadable and I always wished they used something else when reading the source code.<p>Maybe I saw bad examples though.
nesarkvechnep大约 3 年前
I think Literate Programming is fantastic when used to teach computational thinking.<p>On a side note, it seems a lot of the other commenters miss one of the best &quot;features&quot; of LP - minimizing repetition. Chunks of code can be reused and so patterns can become clearly evident. Also, chunks can be defined &quot;out-of-order&quot;.
mtm大约 3 年前
One of my favorite examples of a literate-style program is &quot;cl-6502, A Readable CPU Emulator&quot; by Brit Butler
0des大约 3 年前
OK, I&#x27;ll bite.<p>I stopped using 1 letter variables, abbreviations, and non descriptive function names. If a function or block can&#x27;t be read and followed like a story, and without comments, it probably can be simplified.
krick大约 3 年前
Ok, that will be unpopular.<p>&quot;Literate programming&quot; is a non-invention by somebody (Knuth), who is very much revered by many programmers (many of whom never even actually read him), but who was — let&#x27;s admit it — just terrible at writing readable code. I&#x27;m very much not a fan of the &quot;Clean Code&quot; by Martin, but he had a very nice example of refactoring some of Knuth&#x27;s code to show you what I mean (although, it&#x27;s kind of evident that writing clearly wasn&#x27;t in Knuth&#x27;s DNA just by reading his famous books). Today, this is an attempt to solve a problem, which you created yourself by avoiding using tools that already exist to solve that problem. Then you invent all sorts of tooling and mental tricks to make solving this problem <i>your way</i> more comfortable. But if you would just use these <i>already existing</i> tools, there would be no need in making up a new name for what you do. It wouldn&#x27;t be some &quot;literate programming&quot;, it would be just <i>programming</i>, <i>the sane way</i>.<p>First off, what tools I&#x27;m talking about: well, that&#x27;s everything PL developers invented over the decades, and it obviously depends on which PL you are going to use. If this is some pseudo-assembly language like what Knuth uses in his TAOCP, then, well, there aren&#x27;t many such tools, so creating your own template-preprocessor (which is, in a sense, making a new PL with additional features on top of your pseudo-assembly) perhaps would be an okay-ish idea. But if you use something that people <i>actually use for programming</i>, then you surely have functions, some kind of advanced data structures, perhaps classes and inheritance, perhaps some templating features as well (like… traits?).<p>Going back to the example at hand (the code author &quot;simplifies&quot;): all that &quot;simplifying&quot; consists of a top-down description of what he&#x27;s going to do. Really, the code he ends up with (in &quot;transpiled&quot; form) isn&#x27;t that much harder to read and understand than his &quot;LP&quot; version of it. Inline some comments to explain what he explains in the &quot;LP&quot; version, and you and up with the same thing, but much more concise (so, faster to read — and easier to edit!). If it was a bit more complicated: you do the same thing that he did with his &quot;templating&quot;, but simply by doing what programmers actually do in such cases — extract complicated fragments of a function into smaller functions, and give them proper names. Maybe add some comments — yes, they are a part of your PL for a reason.<p>Moreover, the most complicated thing in his example isn&#x27;t <i>how</i> the algorithm is written down, but the very algorithm itself. It is ok as long as you never actually run this code, but if you actually use it in some useful program, where it can cause problems, a programmer coming across this thing would need to stop to wrap his head around what this is doing, if it&#x27;s actually all subsets and how fast the call stack may grow (as it so often turns out when you use recursion to write down &quot;an elegant&quot; solution). I mean, I&#x27;m only suggesting, but wouldn&#x27;t this be a little bit more straight-forward?<p><pre><code> function subsets(elements) { results = [] &#x2F;&#x2F; All subsets of a set of 5 elements are basically binary numbers &#x2F;&#x2F; from 00000 to 11111, which is from 0 to 2⁵-1 for (i in range(0, 2^(len(elements)) - 1) { results.add(get_subset_by_binary_number(elements, i))) } return results } &#x2F;&#x2F; Blah-blah &#x2F;&#x2F; Given [1, 2, 3, 4] and a number with binary representation 0101 &#x2F;&#x2F; will return [2, 4] function get_subset_by_binary_number(set, number) { ... } </code></pre> This isn&#x27;t my main point, though. My main point is, that people write code for a reason. There can be number of reasons, but usually it fits into a range from &quot;doing some enerprisy-boilerplaity stuff I&#x27;ll need to redo over again next week&quot; and &quot;writing a book, which has code, because it&#x27;s about programming, and code describes programming better than english&quot;. In the first case it probably won&#x27;t need a lot of &quot;LP-kind of explanations&quot;, and where it needs to go over &quot;why the fuck did I do it like that&quot; a bit more extensively, you&#x27;ll just link Jira issue in a comment. In the second case it might look a bit moe like LP, but it&#x27;s just called &quot;writing a book&quot;.<p>In all of the cases in between you&#x27;ll add some amount of comments, always trying to minimize overall amount of stuff other people will have to read (and, well, you to write), which is expressing as much as you can with words you cannot avoid to write (i.e., code that actually does things, explaining them both to humans and to a computer) and minimizing what you can avoid (i.e. English). (Closer to &quot;a book&quot; on this spectrum it will also include some Jupyter Notebooks.)
评论 #30766859 未加载
DeathArrow大约 3 年前
&gt;Code should be written for humans not machines.<p>Unfortunately, machines have a different way of understanding code than humans.