[Architecture] How can I avoid "shotgun editing" in my app?

I’m writing a personal-use app that helps me making my blog (don’t search it, it is not on the Net yet :wink: ). Recently I’ve experienced an issue when I want to add a new type of element to my engine.

To put it short, here is the problem: when I want to add a new kind of object (like “bold”, “link”, “image”, etc.) I must:

  • Create a new element type in “elements/”, so there is a Python object for it (usually a dataclass),
  • Create a parser in “source/”, so the thing could be read at all from a blog entry,
  • Make a HTML writer, so it compiled to .html files,
  • Ditto for PDF

This is at least 4 files to edit at once. I said “at least”, because very often it is more than 4 and I have to edit yet another files if the feature requires it. So, this easily put me in a situation where I’m working on ~10 files all in the same time.

So, should I rewrite my app? If so, what strategy I have to use this time?

That sounds pretty reasonable for a adding a full vertical slice to a project. The only real alternative I am aware of that you could use is a plugin based approach where the parser and the generators have hooks which allow adding elements without having to edit their source code.

For the generators this can be somewhat easily achieved by having methods on the data structures representing the elements. For the parser this is quite a bit harder and depends on how exactly it works.

(But tbf, I feel like the actual solution is to not roll your own system here and just use markdown or rst and existing compilers)

1 Like

I think this is a question more of style and need. Does one need to have many files that are likely to be used in other applications? Then make the app into many source files.

I’m the only programmer responsible for the programs I write. This may explain why I do what I do. I don’t share any library files I create with anyone because there is no need, and based on past management decisions, it is very unlikely I will ever be part of a team of programmers where I will share code with them.

For me I have one local utility library where I put general use utilities. Then one file for my main app, call it app.py. I have never had any need to make more than that. And, since I’m still a beginner with Python, I don’t know how to write modules yet.

But after working with Perl for 20+ years I’ve not yet had a proven need to change this style as my programs tend to have many similarities and fewer needs as far as functionality. Most of my programs are:

  1. Read a file.
  2. Process the file.
  3. Write a file of some type.
  4. Maybe send a new file to the user via email.

I don’t do anything fancy over the internet, no fancy math, no graphing, no web site automation (yet), nothing else really.

If you think that you may need some functionality then divide your source files into modules, each with related classes or functions, but don’t overdo it relative to your current, proven needs. The modularity concept is there to make things more useful, not make them more difficult.