logoalt Hacker News

mawekiyesterday at 11:11 AM4 repliesview on HN

Embedding functionality into strings prevents any kind of static analysis. The same issue as embedding plain SQL, plain regexes, etc..

I am always in favor of declarative approaches where applicable. But whenever they are embedded in this way, you get this static analysis barrier and a possible mismatch between the imperative and declarative code, where you change a return type or field declaratively and it doesn't come up as an error in the surrounding code.

A positive example is VerbalExpressions in Java, which only allow expressing valid regular expressions and every invalid regular expression is inexpressible in valid java code. Jooq is another example, which makes incorrect (even incorrectly typed) SQL code inexpressible in Java.

I know python is a bit different, as there is no extensive static analysis in the compiler, but we do indeed have a lot of static analysis tools for python that could be valuable. A statically type-safe query is a wonderful thing for safety and maintainability and we do have good type-checkers for python.


Replies

eddd-dddeyesterday at 2:19 PM

I disagree. You'll be surprised to hear this, but source code... is just a very big string...

If you can run static analysis on that you can run static analysis on string literals. Much like how C will give you warnings for mismatched printf arguments.

show 1 reply
WesleyJohnsonyesterday at 5:45 PM

Could this be mitigated by using `dict()` instead of the `{}` literal, and then running an analysis to ensure the provided dictionary keys all end with valid operations? E.g, __contains, __lt, etc?

I don't have a strong background in static analysis.

notpushkinyesterday at 2:01 PM

I love how PonyORM does this for SQL: it’s just Puthon `x for x in ... if ...`.

Of course, if you use the same syntax for Python lists of dicts, you don’t need any library at all.

gpderettayesterday at 12:36 PM

If your schema is dynamic, in most languages there isn't much you can do, but at least in python

   Q(name=contains('k')) 
it is not particularly more complex to write and certainly more composable, extensible and checkable.

Alternatively go full eval and do

   Q("'k' in name")