For managing my blog I have written a bunch of Python scripts. And over time they have grown. In a frenzy I have refactored it with dependency injection, but now start to find it overengineered and deconstruct that.
Auf der Arbeit nutze ich das Team-Chatprogramm Slack. Dort kann man einen Status einstellen. Wenn ich von Zuhause arbeite, möchte ich einen entsprechenden Status haben, damit die Kolleg*innen wissen, dass sie mich nicht im Bürogebäude suchen brauchen. Wenn ich aber im Büro bin, will ich das auch entsprechend ankündigen. Das geht, ich vergesse es allerdings nur immer.
On the Refactoring Guru website, there is one anti-pattern called “primitive obsession”, which is about the obsession with primitive types. In Python I would define “primitive types” as “jsonable types”, the ones that can be represented in JSON. This is a pattern that I have observed a couple of times, and I want to show a little example from my own code.
The Vigilant Crypto Snatch software started as a two-user project, where Chris and I just had it running on our respective machines. Over time a few other users came along, and now we have a Telegram group with around 50 members and likely even more users of the software. Initially the software was just a command line utility and ran only on Linux. Then I've used Streamlit to create a little web user interface to simulate triggers. People have asked for Windows support, which was not hard to do with Python. The user basis grew even more, and eventually somebody asked for a GUI. Command line programs are very uncommon on Windows, and the reach of the software seems to be large enough to be of interest to non-tech-savy users.
I have recently simulated a few games using Python code, see the articles with the Game Simulation Toolbox tag. All the code is in one repository, but I haven't really written good code. It was mostly just experimenting, and I did not work as cleanly as I otherwise do. My latest addition, the backtracking with Railroad Ink ended up as completely hacky code: Control flow on the module level, no clear separation, huge functions, little use of classes. It is a total mess.
In this post I want to show how to refactor with the Dependency Inversion Principle. I find it hard to create a mock example with sufficient complexity, so I will just take the cryptocurrency buy software that I have featured in this blog a few times. In each iteration I will show the full code, talk about the problems.