Where parallels cross

Interesting bits of life

Emacs as your code-compass: a gentle trigger for maintenance

Too long; didn't read

Do you wonder if it is time to give your code some extra care? I bet you need an icon and code-compass provides that by default now!

The problem

We like adding features. It brings new value to your users and nice challenges ahead! At the same time, we know that too much enthusiasm will little by little bring in some structural decay. When that happens you may need a plan because things may have gone already a bit out of hand. With my code-compass work I want to challenge that. I would like my computer to tell me: "Hey, you worked a lot on this code, what about a bit of care?". Until I manage to make it interact with me, how about some sort of visual queue that points out the state of the code. I can get that via Git, but I need something immediate.

It is a problem indeed

Our society comes with a structural issue: the tragedy of the commons. This happens when we think: "Okay, I know this is not right, but for a one-off should be okay". Then we commit our change. And other people do the same, making the code unreadable. This happens with code, with family issues, with the dirt on the road, with environmental issues. A way to fix this is to educate everybody to always leave things slightly better of how they found them, which Robert C. Martin calls the Boyscout Rule. That is the ideal. My take is that we can also make our tools help us with that. Our Emacs should warn us that things are going downhill. Can it tell us early enough so that we can maintain things while having fun?

And there is a solution

The secret is in the history of code again. We have seen that many changes (by many people) indicate errors and that old code is stable. So I thought how could I be aware of this while coding. It is true that code-compass makes it easy to plot this out. Still I would prefer less latency. Ideally when I open a file and in a way that does not distract me from the task at hand. This reminded me of the modeline. After few rounds of trial and errors, I made an icon that shows the trend of additions for the buffer.

The icon looks like the picture below.

/assets/blog/2021/03/26/emacs-as-your-code-compass-a-gentle-trigger-for-maintenance/up.jpg

The idea is that an arrow pointing up tells that a lot of code was added last three months. The inclination of the arrow changes to tell that there were less additions. If it points down it means that there were more deletions. That is good because less code means also less maintenance.

Behind the scenes I count all the lines added and deleted according to the Git history for the specific file. Then I compare these numbers: if there are no deletions or additions in the last three months the arrow points up and down respectively. Otherwise the arrow points upwards if there are more additions, and downwards otherwise. All this happens asynchronously because reading the git history can take a while, so there may be a little delay before the icon appears.

You can customize period (the three months) and faces with the variable c/icon-trends. You can also disable the icon setting the variable c/display-icon to nil.

This little icon on the left side of my modeline gives me some insights about the code. If I see an arrow pointing up, I know that I need to prune my code. And if it is bent, I keep just an eye open for maintenance.

Conclusion

Check out the latest version of code-compass and you will have your icon. Let me know if it helps you as well!

Happy coding!

Comments