Re:Clever code is probably the worst code you could write
by MrJohz on Wednesday May 22, 2024 @12:44AM
(#40437425)
Can you write functions and classes in Python that roughly mimic the DSL you're aiming for? This is often called an embedded DSL, and it's a great technique for allowing developers to work with domain objects inside a language that they're still familiar with.
There's a handful of templating languages that do this sort of thing using functions, so in Python you might write
form(
{"method": "POST"},
label(
"Name",
input({"type": "text"}),
),
)
The learning curve becomes significantly lighter because you're just using Python constructs in the Python language, which means your IDE can suggest functions to you like it would with any other library.
Solution 2 is superbly consise and straight to the point, but I am pretty sure noone will ever climb the learning curve that this additional DSL imposes to any newcomer.
I really wonder which final choice I will make for my production code.
[truth is that refactoring solution 2 is painless, but at the same time debugging solution 2 is tricky]