How to Document Code with Swimm.io
Tags: documentation, onboarding
Published: 2022-08-26 00:53:37



Code should be documented to avoid building technical debt. Even if other developers join a team with poorly documented code, they tend to document the code less themselves, worsening the problem. It also avoids a unique individual (the founder, for example) being the king over a part of a codebase (which may be why some people don’t document). 



The code itself is kind of a documentation already. While not being a complete documentation, lacking insights and reasons a particular choice was made, it does show a lot. That means writing documentation would have to **copy a lot of the ideas** that are visible in code. That could be the structure of objects and classes. On the other hand, it is cumbersome to go to a separate site for documentation, simply because it is a pain just to **find the right spot in the documentation** that refers to the section of code I am interested in. And I haven’t even talked about keeping code and documentation in **sync**.

**Swimm.io **offers a nice solution to all of this. It references documentation right within VSCode. In the documentation, you can quote code directly. It is automatically updated if any changes are committed to the repository or it at least tells you if any manual adjustment is needed.



So in the last post I have written about [documenting APIs with Insomnia and Redocly](https://sebastianroy.de/designing-the-glue-between-server-and-client/), now let’s look at documenting the code itself. The setup we will be using VSCode as an editor, a GitHub and [swimm.io](https://swimm.io/) account (one private repo free, unlimited repos $29 per user per month).

## Connect a Repository

{% embed https://www.loom.com/share/0a86e53430844544914e3c7c49d39c56 %}


First, we need to [create an account](https://app.swimm.io/register) and connect it to a GitHub repository. 

![](https://res.cloudinary.com/sebastian-roy2/image/upload/v1661378084/PNG-Bild_tcx9dn.png)


## Create a new Document


{% embed https://www.loom.com/share/23069f9510d741eabf7beefb3d1ecff7 %}

Now we have access to the codebase from our documentation, we can create a new document and directly select code as a quote in it.

So create a new document and name the document. 

![](https://res.cloudinary.com/sebastian-roy2/image/upload/v1661443349/How%20to%20Document%20Code%20with%20Swimm.io/PNG-Bild_q2q6j4.png)

Then press `/` to open option menu and select `Code Snippet`.
 
![](https://res.cloudinary.com/sebastian-roy2/image/upload/v1661443432/How%20to%20Document%20Code%20with%20Swimm.io/PNG-Bild_wxnghk.png)

This will let you choose a file from your code. Use your courser to select the code you want to refer in the documentation and klick `Add & Exit`.


![](https://res.cloudinary.com/sebastian-roy2/image/upload/v1661443654/How%20to%20Document%20Code%20with%20Swimm.io/PNG-Bild_yuwuo0.png)

So let's say we aim to refer to a variable name in the code. We could just do that, but what happens when someone changes the name? Then the documentation and the code is out of sync. To avoid that, we can link variables to the code. When you select a keyword and convert the style into code by clicking `</>`. It then opens a suggestion from which line in the code you want to link the variable to. 


![](https://res.cloudinary.com/sebastian-roy2/image/upload/v1661453391/How%20to%20Document%20Code%20with%20Swimm.io/PNG-Bild_elwval.png)![](https://res.cloudinary.com/sebastian-roy2/image/upload/v1661453404/How%20to%20Document%20Code%20with%20Swimm.io/PNG-Bild_uxiqmy.png)

After Linking, you can see green checkmarks next to the keyword
![](https://res.cloudinary.com/sebastian-roy2/image/upload/v1661453516/How%20to%20Document%20Code%20with%20Swimm.io/PNG-Bild_a8pum3.png)


Now that we created our first document, we will have to commit it to the repository by clicking `„Create a pull request“` and select `„Push to master“` and hit `Save`.
  
Now that the documentation is stored in our GitHub repository, next we will add the Swimm extension to VSCode and see how our code now looks from our IDE.

## View Documentation from VSCode

Open VSCode. In the extension tab, search for `Swimm` and install it. 

![](https://res.cloudinary.com/sebastian-roy2/image/upload/v1661453574/How%20to%20Document%20Code%20with%20Swimm.io/PNG-Bild_kydu9o.png)

Then we log in to our Swimm account from the Swimm extension tab and make sure to sync the git repository and checkout the main branch (or the one you committed the Swimm documentation to).

This is where the work pays off. You note an additional line above the `user` struct. If we hover over it, we get the caption we set for our code previously.

![](https://res.cloudinary.com/sebastian-roy2/image/upload/v1661453641/How%20to%20Document%20Code%20with%20Swimm.io/PNG-Bild_x9uj6f.png)

Now, if we click on it, we directly jump to the documentation section within VSCode.

![](https://res.cloudinary.com/sebastian-roy2/image/upload/v1661453731/How%20to%20Document%20Code%20with%20Swimm.io/PNG-Bild_d1wgba.png)

This is so cool. If we double-click, we can even edit the documentation and save it on the next commit, all without leaving VSCode.


![](https://res.cloudinary.com/sebastian-roy2/image/upload/v1661453778/How%20to%20Document%20Code%20with%20Swimm.io/PNG-Bild_ug86tk.png)
## Keeping Documentation and Code in Sync

Now what happens when we rename the field `Email` to `PrimaryEmail`.
![](https://res.cloudinary.com/sebastian-roy2/image/upload/v1661453812/How%20to%20Document%20Code%20with%20Swimm.io/PNG-Bild_zuvmu8.png)

When refactoring this quoted section, Swimm will issue a warning. 
![](https://res.cloudinary.com/sebastian-roy2/image/upload/v1661453849/How%20to%20Document%20Code%20with%20Swimm.io/PNG-Bild_yroayb.png)
We will not have to `Edit Doc` and `Reselect` the code of interest. Then hit `Update Selection` and `Create Pull Request` to push to `main`. All issues should be resolved again.

![](https://res.cloudinary.com/sebastian-roy2/image/upload/v1661453907/How%20to%20Document%20Code%20with%20Swimm.io/PNG-Bild_jvu6m8.png)

And this is basically all we need to know on how to document code with Swimm.io. 

![](https://res.cloudinary.com/sebastian-roy2/image/upload/v1661453937/How%20to%20Document%20Code%20with%20Swimm.io/PNG-Bild_ajdjpg.png)


## Bonus: Playlists

Going back to the beginning of this post, regarding onboarding new developers. Playlist give new developers an ordered way to dig into your codebase.

## Conclusion

Although we started by documenting code, we discussed the importance of that in the context of building a team to develop a product. I am myself not up to speed yet, but will spend the next couple of days integrating Swimm.io in my routine. Do you have any war stories come to mind? Would love to read them in the comments.

## Links
- Avoiding Technical Debt [https://rubenscheedler.hashnode.dev/a-technical-debt-crisis-is-brewing-in-the-land-of-software-and-heres-why](https://rubenscheedler.hashnode.dev/a-technical-debt-crisis-is-brewing-in-the-land-of-software-and-heres-why)