4 ms·
Is there something like this but for regular code? I think it's called verbose programming or literal programming? I can't remember but if anyone knows of simil
by antjanus 8y ago
Is there something like this but for regular code? I think it's called verbose programming or literal programming? I can't remember but if anyone knows of similar projects, please let me know!
- nailuj 8y agoYou mean notebook-style programming environments like Jupyter? They have kernels for a lot of languages. I think Emacs' org-mode can execute code embedded within the document as well. The concept you're thinking of is called Literate Programming.
- tjallingt 8y agoIts called literate programming; for example https://wiki.haskell.org/Literate_programming https://wiki.haskell.org/Literate_programming
- Jtsummers 8y agoLiterate programming. You'd write a collection of blocks and "tangle" them into a program (weave usually means generating the documentation). A common workflow for me when prototyping something, and then I let it grow into a fullblown program, with org goes something like this: * Main This program will print out "Hello, world!". #+NAME: main #+BEGIN_SRC c :tangle hello.c :noweb yes <<includes>> int main(void) { <<main-body>> } #+END_SRC * Includes - stdio :: this is included to get =printf= - others :: if I had other includes I'd explain why here #+NAME: includes #+BEGIN_SRC c #include <stdio.h> #+END_SRC * Main body Maybe an explanation of the algorithm #+NAME: main-body #+BEGIN_SRC c printf("Hello, world!\n"); #+END_SRC Literate programming doesn't care about the order, I can put that main at the bottom or in the middle. This allows me to introduce program concepts in a logical fashion that is different from what the compiler wants or needs. This allows me to, for instance, group all data structures and even share code across them where this would be less feasible in straight C. References like the `<<main-body>>` can be shared by multiple code blocks. Obviously, as programmers, we want to minimize repetition. But sometimes a language forces us to repeat ourselves. If it gives us a way to include common source blocks, that's awesome (like you can make a header file called common.h that every one of your source files includes). But that's feasible in every language. Nor are the macro systems ideal in every case where it is technically feasible. As I write the code, usually in one pane of tmux, I'll tangle it. Then in a separate pane (usually on the right) I'll execute the code. Or I may have something that monitors the files for changes and builds it automatically and runs tests. EDIT: This is also an effective technique for developing an understanding of a new code base. Bring all the code into one big source block in a new org file (or whatever you use). Tangle it and make sure it's the same as the original (this process gets repeated a lot, make it a script). Grab segments of the code and extract them into new blocks (with a properly named reference). I usually do this with large chunks (whole class declarations, struct declarations, or function definitions). Document what the code does and its relation to other segments. Repeatedly extract segments until they're down to reasonable chunks (a 500 line function? It's probably worth going down to 10-50 smaller blocks covering each logical section).
- uhryks 8y agoI've been aware for long that I know so little about org-mode so I had never imagined I could do such a thing. Love this idea for understanding a code base, I'll have to try this out.
- Jtsummers 8y agoThis is my true "killer feature" from org-mode. I can do outlines in almost anything. Markdown is good enough for a lightweight, text-mode document formatting system. But the ability to integrate code from any language into a document, and spit that same code out for execution, makes it a near ideal environment for me. I miss some features from IDEs doing this, but honestly it's not that big of a deal for me. I end up having to commit more to memory. Or write up my own tutorial/example section for things that I keep forgetting. You can have code blocks that don'