Zettelkasten Forum

Code snippets in ZK

I know there are many programmers here on the forum. I'm wondering whether and how people are using ZKs to support their coding efforts.

I've been using my ZK a lot more recently for that. Whenever I learn something from Stack Overflow or around the web, I put it in the ZK somewhere. Or when I come up with my own code solutions, I make them atomic snippets as well.

The atomicity is key. I know it's common for people to store re-usable code in libraries or modules, but this creates the same issue as when people keep all their notes in one long document: Finding the specific information you need is hard because it's buried with a lot of other stuff.

So keeping very specific code in my ZK has made it easy for me to find and reuse solutions.

Here's an example:

I have a hub page called "My R Cookbook" (inspired by the R Cookbook series of books):

I then link out either to (1) direct links to source, or (2) my own zettels.

Here is what one zettel looks like

I'm finding this setup very helpful and it's making my code efforts less redundant, especially when I step away for a while and then come back.

Anyone else doing this?


  • edited April 2020

    @achamess your 'hub' note is a super example. I love how you've not only made a structure of 'Basics', 'Data', and 'Programming' but also how you added context, such as with 'number factors'. This will get more and more valuable as your collection of atomized code fragments grows. Your example is one where you want to update the 'hub' note over time. Growing its value and utility. If I was a smart as you ;), I'd create a hub note with my few notes on Keyboard Maestro, but instead, for now, I just use the Omni Bar.

    I want to add a little different example. I this case I processed an idea I got while reading Gregg and Steinberg's Cognitive Processes in Writing. it has a similar structure as yours. This one is one that I'll not regularly update. It has 3 outgoing links and 3 incoming links with 2 tags.

    Will Simpson
    I must keep doing my best even though I'm a failure. My peak cognition is behind me. One day soon I will read my last book, write my last note, eat my last meal, and kiss my sweetie for the last time.

  • That's right up my alley :)

    As I've just written in my personal journey post, using the Zettelkasten for a practical topic, programming, really made it useful for me. Philosophy was fine and dandy, but I mostly used my Zettelkasten for day-to-day topics, because I love interacting with it, and as a tool to organize the stuff I learned at Uni.

    Here's a ##outline list of notes that are potential book outlines or overviews:

    The "Indie Dev" series was a longer note that I split up one evening in 2018-09-18. It's 50:50 German and English, so even though the topic would be easier to understand for to y'all, here's the 201712051941 Comprehensive Cocoa Text and Text Kit overview instead, a collection of stuff I learned about writing text editors for Mac:

    Which also links to this structure note, 201707121417 Distraction-free Text View component:

    Which links to a code snippet, 201707121415 Fixing NSTextView with insets recentering on resizing:

    I would start with the 201707121415 Fixing NSTextView... snippet and then integrate it into a collection for added context.

    2017 predates The Archive, so code snippets are indented by 4 spaces instead of using GitHub flavored Markdown's fenced code blocks :)

    This is very different to the way I processed Epstein's Range, where I forced myself to add a "forward link" from where I was first, and then write the note second, to guarantee a level of connectedness.

    Here's a simple snippet that I use to copy & paste in almost every project sooner or later, without any context, 201606251130 Check box class for Cocoa. Really, this one has 0, zero, zilch incoming links. I just search for "checkbox" and copy the code into the app.

    Author at Zettelkasten.de • https://christiantietze.de/

  • I also add code snippets and unix commands to my slipbox.

    However, I'm quite bothered by the evanescent nature of many code snippets. That is, outside the few very stable languages such as Java or C and for majority of library code even there, how long do the snippets stay valid until a new version of runs them over? A Year? Six days if you do javascript?

    Regardless, there are lot of stuff I've recorded anyways. Most useful snippets have been some proof of concept programs I've done and some reusable snippets on especially gnarly libraries.

    I've also been investing in learning nix and few other tools to gain some permanence for my code related notes. This has had a pleasant side effect that many of my code snippets are now actually executable: I can spawn a repl for playing with the code on the zettel with a keypress.

  • I'm not using a strict Zettelkasten approach, but I have lately been trying out some of the ideas in context of my existing notes, which live (well, mostly) in vimwiki.

    Right now, I'm on a release engineering team, which in practice means I do a lot of things in the space between software development, traditional sysadmin work, and contemporary "DevOps"-style practices. I write/review some code, configure some other code, and type lots of commands into terminals to deploy somewhat scary websites. My last few jobs have had a similar mix, if at smaller scale.

    Juggling the state of small technical projects and various running systems has pushed my note-taking in certain directions. I'm usually asking two kinds of questions:

    1. What is this thing?
    2. What did I do about it last time?

    I think of things as having a subject-matter dimension and a temporal dimension, and I want to be able to index into the notes according to either or both. So while I'm interested in the model where you convert reading into individual notes that represent ideas and connect to other ideas, ZK-like, what I keep coming back to when I think about my own work is a little different: More like a log with entries tagged to relevant subjects.

    Logging is a broad concept. The way I imagine this, a log entry might be a record of commands run on a production server, a summary of where I left off and expected next steps for a given project, a note about a conversation, or an idea I had while thinking about a problem. It might also encompass point-in-time records from external systems like command-line history, version control on various repos, issue-tracking systems, bookmarking & browser history, e-mail, etc.

    As it stands, I have wiki pages for topics which start with an overview and various links, and then under a "log" header I write datestamped entries as relevant things happen. A trivial example about a particular repo called dev-images:

    [[warelogging]], [[wmf]], [[docker]], [[blubber]], [[docker-pkg]]
    See also:
      - [[https://wikitech.wikimedia.org/wiki/Blubber/Tutorial/HelloWorld|Blubber HelloWorld]]
      - [[docker-library-httpd]]
    = test process =
    Building locally with docker-pkg (the `--no-pull` is crucial, has to come after
      docker-pkg --info -c dockerfiles/config.yaml build --no-pull dockerfiles
    = log =
    == 2020-02-07T16:25:45-07:00 ==
    Rebuilt for https://phabricator.wikimedia.org/T244382
    == 2019-11-26 ==
    Working on https://gerrit.wikimedia.org/r/c/releng/dev-images/+/551658

    Ideally, those log entries would instead be their own entities, whether in a file or in a database table, and each would be linked to relevant topics and show up in a list of log entries when viewing those pages. Something like the difference between storing e-mail in folders like traditional clients, and applying arbitrarily-many tags to messages in a system like GMail.

    Another way that it's useful to access log entries is to look at what happened for a given date range, which often highlights relationships between work and ideas. I hacked out a script called timeslice to pull stuff out of git logs, vimwiki diary entries, CLI history, and Pinboard bookmarking for a date.

    It all needs more work, but so far I'm finding this approach promising.

  • I recently have thought about my own approach as regards to coding and using a Zettelkasten. For me it's possible to divide the Zettels into three different groupings:

    1. abstract concepts - ideas, definitions, patterns, etc.
    2. concrete implementations - APIs, documentation of functions and keywords, classes, etc.
    3. HowTo's, concrete solutions and approaches

    It helps to have these groupings in mind, because it happens kinda often, that what we call a concept is also what we call the implementation, too and still they are different: Almost any language will provide an implementation of the Array data structure that will be called Array also. But they are different.

    When coming up with solutions to programming problems I want to have these three things available to me: The underlying bedrock principles of programming, how the language, the framework, etc. has implemented these concepts and how a specific thing can be achieved.

    Things should become somewhat composable/linkable in this way.

  • This conversation is fantastic. Thank you all!

    Thanks for the words of encouragement and sharing your hub page. Looks great!

    @ctietze Thanks for sharing. Agree 100% that my motivation to use the ZK is increased by its perceived utility. It's really paying immediate dividends

    @brennen Great thoughts and example. I also keep logs in my code notes to see the specific way I used a solution in the past. I agree it would be nice to have these in a separate database or file. I have to check out timeslice. Thanks for sharing.

    @matti I really like how you think about the type of note you make. I agree, there are different kinds of notes, some of which are more general like "control flow (concept)" vs. "for loops in python (implementation)" vs "Automatically generating directories with names based on files using for loop (HowTo)". Do you tag your notes in any way to indicate the kind they are?

  • You all have inspired me to create a hub for my puny but growing coding knowledge.
    I'm a puppy running with you big dogs.

    Here is a sample of my hub note in the short time I've been developing it today. The notes were already created and all I have done so far is to bring their links together in a 'hub' and sorted them. I've added a wee bit of context with more to come.

    I've added it to my saved searches menu as a reminder to be adding to it when I do coding work. This is a different use case for my zkn than I've considered. Much of my coding reference material is in a collection in Evernote. I have lots of web clips from Opensource.com and stackoverflow.com

    Here is what Working with dates and times in KM [[201911101708]] looks like. (only the top ⅓)

    Will Simpson
    I must keep doing my best even though I'm a failure. My peak cognition is behind me. One day soon I will read my last book, write my last note, eat my last meal, and kiss my sweetie for the last time.

  • Do you tag your notes in any way to indicate the kind they are?

    Not consistently, no. I am mostly looking for a "local fit", when linking and a semantic anchor when tagging. That means 1) I would ideally find the note in two or more different places (optimizing for serendipity) and 2) I would need to find an appropriate context only once (optimizing for note creation throughput).

    • semantic anchor means here: whatever the Zettel is contentwise, it relates semantically (it articulates) to this tag (because I say so)
    • local fit means here: whatever the overall structure of my Zettelkasten might be, the incoming and outgoing links are the context in which it has meaning
  • Hmm interesting! I didn't find categorization of the note contents useful in the past. If a note is called How to get the script's file name in Ruby and contains 3 lines of code and a comment + tags, I'm pretty sure that's not a concept, but just a snippet. It's probably not heavily linked to, except from "cookbook" or "boilerplate" collections, like 10 things to start a macOS app project.

    On the other hand, Different kinds of MVVM in reactive programming contains a commented list to more detailed notes if the distinct approach is fleshed out, and weblinks for stuff that isn't, but which I found someplace. It's pretty clear at first glance at both title and content that this is a conceptual idea I captured. Like topics from humanities, this is more heavily linked to, and links to other notes more heavily, than a mere snippet.

    @matti Can you elaborate (here or in a separate "Have a look at my ...!" discussion?) how you work with this?

    Author at Zettelkasten.de • https://christiantietze.de/

  • edited April 2020

    @ctietze @achamess et. al. I ended up writing a blog post about my current approach. You can find it here. I also created a thread for this just in case there are questions to my specific way of doing things, so I wouldn't derail the discussion too much here.

Sign In or Register to comment.