Hacker News new | ask | show | jobs
by jmull 1203 days ago
I agree this is pretty much the best example of this feature in action... which is why I don't think it should have been made a language feature.

It's nifty, but not nearly better enough to justify its existence, IMO. Here's the alternative:

    var result = await query(
        'SELECT * FROM foo.bar where bar = :baz',
        {baz}
    );
I get it... there's that extra level of indirection. But people are working hard, as we speak, to abuse the feature.
2 comments

I'd rather have the language settle on one single templating syntax rather than every library and their son bake a half-assed one. "Oh, does query use the : syntax? The $ one? Does it take the template string first, or the arguments?" And with your example, `query` needs to figure out how to parse the string, extract the template slots, and pass the correct arguments into the correct slots. It's a recipe for disaster if every library needs to reimplement that.
Well, people could settle on a common templating syntax without making it a language feature. The fact that they haven't tells you it's not that important, relative to other concerns.

It's not like "figure out how to parse the string, extract the template slots, and pass the correct arguments into the correct slots" is rocket science.

And it's not like people are going to rewrite the numerous existing libraries for this kind of thing. The new tagged-template APIs are going transform their arguments and call the existing APIs.

I guess it's nice that new Javascript-specific templating languages can have common escaping syntax. It's just hard to get excited about the 15th standard.

I like tagged template strings but nowadays most libraries use TS (or at least JSDocs) and any serious IDE will be able to quickly answer your question of what type of parameters a function is expecting
In this case - the thing I personally value in the template version is I don't have to name the parameters and specify them in a separate place. It's especially useful in larger queries.

    var result = await query(
        'SELECT * FROM foo.bar where bar = :baz
          -- 100 more lines of where clauses, CTEs, etc.
        ',
        {baz} // where did I use this again?  I'd need to scroll up.
    );
versus

    var result = await query(
        sql'SELECT * FROM foo.bar where bar = ${baz}
          -- 100 more lines of where clauses, CTEs, etc.
        `);