r/AskProgramming • u/foreverdark-woods • Jul 14 '26
Architecture Declarative code style vs. over-engineering
I am writing a Python application whose core algorithm computes file content hashes. Depending on the configuration and file type, there may be different ways to hash a file.
Ideally, I would some kind of annotation for the hashing methods that determines when to use which function, e.g.,
"""Compute hash from raw bytes"""
"""Compute perceptual hash of image"""
In this example, mimetype is a property from f: File and hash_type a configuration (e.g., from my-app --hash-type=perceptual).
I like how extensible this is, I could add new functions, but I also think that adding new functions may require adding more flexible filter mechanism, such as (semi-pseudo code)
@hash_fn(Property("f.mimetype")=AnyOf("image/*", "video/*"), Property("config.hash_type")=In("perceptual", "..."), )
def compute_some_hash(f: File) -> Hash:
...
The advantage is, when done well, I wouldn't have to touch the hash function decision logic at all because this approach is super-declarative. The disadvantage is this approach is hard to implement well, so requires some amount of time, and it's quite obscure (i.e., not really KISS).
This application is for personal use and I regard writing it also a part of exercising my mind, but my free time is limited and maintainability and correctness are a big concern.
Do you think I should better go with the good old
def choose_hash_fn(f: File, c: Config) -> HashFn:
if f.mimetype in mimetypes("image/*") and c.hash_type == "perceptual":
return compute_image_hash
# maybe other criteria
else:
return compute_bytes_hash
5
u/Mediocre-Brain9051 Jul 14 '26
I don't understand why wouldn't you rather use a (file_type - > compute_bytes lambda) hash or objects (template method or strategy patterns.
The if is ugly.
3
u/qlkzy Jul 14 '26
I deeply dislike the "self-registering plugin" approach (except for tests, because those happen to have a good combination of simplicity and non-interaction).
But in general it just completely screws up the locality of your program.
In this particular case there's also a real mess of precedence ordering: if multiple functions could match, which one wins? How do you detect overlaps? Does the precedence change if your function moves between files?
Whatever you do with the decorators is just syntax sugar. However you tune that, what you're ultimately doing is building a table of
(match_rule, handler)entries.So I would just expose that table directly. That's the actally-declarative approach anyway, IMO.
You can always experiment with various ways to construct that table (decorators, some helper functions that feel DSL-like). But making the data structure the core of the idea is much easier to work with long term than making the decorators the core of the idea.
2
u/jameyiguess Jul 14 '26
How would decorators solve the issue of knowing when and what to call?
2
u/sswam Jul 14 '26
My gut feeling is that decorators stray far from Python's original goals of simple, clear code, and should be avoided as much as possible. I've done fancy code with decorators but it wasn't much fun really.
2
u/ColoRadBro69 Jul 14 '26
I don't agree with this thinking at all. The world isn't a crud app.
3
u/mc_pm Jul 14 '26
I'm always wary of adding complexity if you don't know you're going to need it yet. Do it the gross way to start, get your thing working, and think about it's actual value. Maybe after writing the first version, you'll have a better idea, and you can invest in it then. Or maybe it will work fine for what you need and then you don't.
2
u/high_throughput Jul 14 '26
The good old one for sure. Easier to follow, easier to test, stronger typing, more expressive, better for tooling including AI.
2
u/cejiken886 Jul 15 '26
This is fine. Proceed.