2
17 Comments

Embedding Code Into Documentation

Anyone have a problem including code into documentation?

I find that code changes pretty quickly so this leads to me avoiding writing code in documentation in the first place.

I wasn’t sure if this is a legitimate problem or if there’s a sizeable cohort of individuals suffering from this particular pain point.

on December 13, 2020
  1. 4

    We use github gists and it works pretty well. For instance on our KB

    1. 1

      Do you ever worry about the content of the gist no longer reflecting what's in the codebase? Say if you copied over some code from the codebase and the latter changed?

      1. 1

        Not usually because they are implementation options and we only very rarely remove functionality. If the code works for our users when it was written, it should still work many years later. We also don't use the blog code as the source of truth for implementation, that we put in the SDK documentation, which is much closer to the codebase.

  2. 3

    I think the question needs some more clarification, what kind of documentation are you talking about? For example online docs in which case what kind of platform?

    Since most of my stuff is code and technical, I use plugins to format the code and use markdown files. However there are other ways you can do it using something like GitHub code snippets and embedding them so you can update the code in one place and it will automatically appear refreshed in documents.

    Just a couple of thoughts.

  3. 3

    Yes, it's mildly annoying. No, I wouldn't pay to fix this.

    1. 1

      Why would you not pay for a fix? Is it that you don't find it to be a large enough inconvenience?

      1. 1

        I think the biggest question is how will this be fixed. Probably it will require some vendor lock-in or some complex setup, which would take a lot more time than just fixing the code when it gets outdated. Also, APIs don't and shouldn't usually change too often.

  4. 1

    Not an issue for us at all. We use Phabricator to manage all our development tasks, and tickets (and merge requests) are all linked to the repo so we can narrow down to the actual live code at any time if we reference a ticket or PR in our Phabricator Wiki documentation.

  5. 1

    I'm building a notebook system that may exactly solve your painpoint, it's called Starboard

    1. 1

      Wow, cool cool! Keep me posted!

  6. 1

    I have had this issue in the past, and Elixir solves this in a really great way. Basically if you put example code of running a function from the REPL into a doc block, that code automatically gets run when you run tests on that file!

    E.g. if you've defined a function that squares numbers and then you put the following into its @doc string:

    @doc """
    This function takes one argument and returns that number squared. For example:
    
    iex> Enum.map([1, 2, 3], &my_square_function/1)
    [1, 4, 9]
    """
    

    Then your documentation will cause your tests to fail if your example ever gets broken by changes to your code. Some other modern languages have similar tooling built in and I could definitely see using 3rd-party tooling to do it if you're working with an older language that doesn't do this.

    (note: in the example, it should be @doc, without the [](/doc). It seems I've hit an edge case in IH's handling of pre-formatted code blocks.)

    1. 2

      Wow this is pretty cool! I must say. I really think other languages should add this feature

  7. 1

    Using markdown, a JS script creates virtual files of the code snippets after which the user can edit them live on code sandbox. It doesn’t solve the up-to-date issue but IMHO apis shouldn’t change that often.

    1. 1

      What's the JS script that you're referring to?

      1. 1

        A custom script that creates a code sandbox request object from HTML pre elements and adds a button to open codesandbox. Markdown generates the pre elements from the code snippets and the script picks it up to create a button.

        https://codesandbox.io/docs/api

  8. 1

    Yeah I have no idea how. I've been using markdown, but I wish it was more live and runnable

    1. 1

      Yeah it's a problem I really have as well :(