Hacker News new | past | comments | ask | show | jobs | submit login

The nice thing about React, vs others like Vue and Angular, is that we've learned not to write code in strings. You'd think this would be a fundamental law of software engineering, but we keep seeing things like

    v-bind:id="'list-' + id"
or

    <button [style.color]="isSpecial ? 'red' : 'green'">
or this.

Writing code, not strings, means you can do everything to it that you can do to code. You can type check code. You can lint it. You can optimize it, compile it, validate it, syntax highlight it, format it with tools like Prettier, tree shake it...

That's why I like JSX, it's Javascript all the way down. Everything is code. It's a very well designed and thought out DSL.




> Writing code, not strings, means you can do everything to it that you can do to code.

These are tagged templates, not strings. They work as code at runtime, and it already has support to optimize it, compile it, etc. This is JS all the way down. And spankalee mentioned below they're adding typescript support to type check the very similar lit-html, so that's coming too.

> That's why I like JSX, it's Javascript all the way down. Everything is code. It's a very well designed and thought out DSL.

It's also kind of a weird to highlight all this for a DSL that actually has to be parsed from a string to get it to be Javascript. Everything else you're talking about is tooling that had to be built special for it, not the language itself.

Why not just say "I like the style of jsx embedding and existing tools better" :)


+1. It's all strings (of source code) in the end. It's just a matter of how well the tools support those strings.

There exists a medium-sized ecosystem of tools which supports static checks on strings inside angle brackets, a la JSX. There is a smaller-but-growing ecosystem of tools that supports static checks on strings inside backticks, a la HTM/lit-html/etc. There's no fundamental difference here.


The fundamental difference is what you have in the end. With JSX you have function calls (JSX is a very thin DSL after all).

With tagged literals you end up with runtime string parsing and concatenation.


> The fundamental difference is what you have in the end. With JSX you have function calls

Check out the output of the Babel plugin mentioned in the readme

> // input:

> html`<div id="foo">hello ${you}</div>`

> // output:

> React.createElement("div", { id: "foo" }, "hello ", you) options

https://www.npmjs.com/package/babel-plugin-htm


It's (marginally) better than having it at runtime. Poor man's macros. Much like JSX in a sense, though JSX is a much thinner abstraction IMO.


and i think this is the point; runtime vs compile time! similar reasons that people love the popular new type systems... if it compiles, it probably works or you get an error rather than pushing that later and possibly not catching it


> These are tagged templates, not strings

Tagged templates are strings.


> These are tagged templates, not strings. They work as code at runtime, and it already has support to optimize it, compile it, etc.

Optimize, compile etc. what?

f`string` is literally a function call f("string"). What exactly are you "compiling and optimizing"?


No, that's incorrect. Interpolations (${}) are preserved. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...


Let's return to the original statement, shall we?

> These are tagged templates, not strings. They work as code at runtime, and it already has support to optimize it, compile it, etc.

So. How exactly do they "work as code" and "have support to optimize it, compile it, etc."?

Here's HTM's main function https://github.com/developit/htm/blob/master/src/index.mjs#L...

It: concatenates strings, parses (via regexes), then concatenates again. Can someone enlighten me as to why this is awesome, is code, is optimized and compiled?


I was responding to your false assertion that f`s` is the exact equivalent of f("s"), which only holds true without interpolations. f`a${b}c`) becomes f("a", b, "c").

Secondly, the answer is Babel. Since the string's components are part of the AST, it can optimize and compile it as the original poster claimed.


> which only holds true without interpolations. f`a${b}c`) becomes f("a", b, "c").

The difference is so subtle as to be irrelevant. You still need to manually parse, process and assemble strings. There’s no magic. There’s no code. It’s just strings (with some values here and there).

It’s enough to see the implementation of HTM (or lit-html or any other library using tagged literals) to see that.

> the answer is Babel

Which is only mentioned once the bullshit claims about tagged literals are refuted.

Babel is not exactly the answer, but as poor man’s macros it will do.


They're just talking about https://www.npmjs.com/package/babel-plugin-htm.

> A Babel plugin that compiles htm syntax to hyperscript, React.createElement, or just plain objects.


I agree with the general sentiment.

The difference is even more pronounced once you start typing your code.

TS/TSX = 100% typed

arbitrary template DSL = 0% typed

and now these string template based on interpolation = 5% typed: Only the dynamic values inside ${} expressions are "typed", but there are no coherence checks. DOM attribute names and values remain untyped

In the absence of any real advantage for string templates, why even bother?


I came to write... this.

Sure, stringly typed programming is sometimes a quick and dirty solution - and that's fine, even permanently, if you just don't want to invest much in whatever you're doing - which too, is fine.

But it's not a good thing. It's cheap and easy, which is sometimes something you need to settle for.


to give a clear answer here, tagged templates don't actually perform string concat. the function being used as the tag receives an array of the string literals, and the rest of the arguments as the values passed in templates, and can perform any processing it wishes at that point. I've seen some js sql libraries actually using this to prevent people from having the exact problem you're complaining about.


However, the main darling of tagged templates, lit-html does exactly that: parsing, concatenating, then doing an element.innerHtml

So does HTM we discuss here.

I quickly googled. sql-tag and node-sql-template-strings do a lot of string concatenation (well, of course, they have to create a SQL statement out of values passed in). sql-template parses and concatenates.

Because it's really basically all you can do, be cause tag`string` is just a function call tag("string"). There's no magic.


its actually more like tag`string ${x}` is tag(['string', ''], x)


Thanks, I was coming here to say how a bad idea is this thing. Totally agree with you, I can't understand why people still like write magic. I like your comparison strings Vs code. I'll steal that!


JSX is not Javascript, you need to transpile, and not everybody wants to mess with webpack+babel or create-react-app, or anything other what involves downloading 1000+ packages from npm just to do some quick PoC. Sometimes you want to stay light (and fast), and incrementally build your stack, just you like can do with vue.

So I actaully like this project, thanks for this :-)

BTW: it's similar to lit-html, t7 and hyperx


Then don't do that.

Write a method to get the list id.

And FWIW I honestly think JSX is misstep.


If I find myself doing something like that in Vue, I immediately make it a computed property. Granted, v-bind:id="something" is still markup that has little chance of being well understood by the IDE, but at least there isn't much to get wrong, and I don't have to remember if I need ', ", \', or \".


When you write JavaScript, you can only optimize it..as JavaScript. It still needs to be parsed, compiled, and optimized as JavaScript.

When you write templates, you get more room for optimization. Glimmer engine compiles templates to binary bytecodes to bypass JavaScript parsing. Vue3 inlines component fragments as part of the compilation step.


Not sure I follow.

Glimmer compiles to bytecode, but I am not sure how that “bypass[es] JavaScript parsing” as the bytecode is evaluated by a runtime written in JS (which is obviously parsed) and that translates the bytecode into JS calls. Basically one type of parsing has been replaced with another, which is fine though not obviously faster.

Likewise, I don’t see the point with Vue since again vue templates are translated into JS calls.

Or you could just make JS calls, like React does, no runtime translation required.


Yes, the runtime needs to be JavaScript.

It’s not obviously faster, but it’s been benchmarked to be faster. https://youtu.be/nXCSloXZ-wc

You can skip to 29:34 for the performance demo.


JSX isn't JavaScript. It's JSX.


no, but it BECOMES javascript after transpile time


Yep. Just like C code isn't binary code.


Why is it not JavaScript? The Ecmascript spec explicitly allows syntax extensions.


Is this valid Javascript: %%$$#~~~~##

What if I make it my special js "syntax extension"?


I'm not javascript syntax, especially if you're being pedantic, but focusing on that is kind of missing the point, which is that it's tool friendly in the way string interpolation is not.


Writing code, not strings, means you can do everything to it that you can do to code. You can type check code. You can lint it. You can optimize it, compile it, validate it, syntax highlight it, format it with tools like Prettier, tree shake it...

Both intellij and emacs do all of these things for Vue code. That seems to cover >90% of developers' tooling needs.

Also, code itself is a string. Code is data.


Code may be a string, but tools that treat it as such are universally abhorred, like the C preprocessor or eval in interpreted languages.


I wrote React for a couple years and was pretty dogmatic against the whole "stringy code" thing. Obviously and generally, we'd rather write code not in strings. But guess what? Moving to Vue from React was a major productivity boost for us - "stringy code" and all.


I'm calling bullshit on all these claims of "massive productivity boosts" when switching to Vue. What on earth were you doing in React that was so unproductive? Maybe if you're bogging yourself down with a bunch of absolute garbage like immutable and redux-saga, but you can hardly blame React then.


What's wrong with redux-saga? What alternatives do you offer to solve problems redux-saga helps to solve?


I assume the point is that some people bog themselves down with tools that make them less productive just because they read that it's best practice, without deeper understanding.

For example, in most threads about Vue on Reddit, the fact that you can just add Vue as a script tag and start working with it is it's most frequently cited advantage.

A small app, especially for most newbies just starting out, doesn't need webpack. But obviously they didn't read the docs and didn't even try the same with React. They just went straight for webpack, even though they shouldn't have. Not to mention CRA exists.

Same with immutable and saga. Their benefits are apparent in much bigger applications, and even then there are arguably simpler and better tools like Immer, update-immutable, redux-thunk, redux-promise-middleware or even rolling out your own middleware.


Why beginner would try that with React? Official documentation does not even offer this approach. Meanwhile Vue.js official documentation gives basics first with this approach.


that as the case may be, id say that was in spite of the “stringy code”. the point (i think) is that this, and JSX are functionally the same thing, but there are some very large issues with doing this in a string.

*edit: clarity


what if it being more productive for you has nothing to do with these magic strings? :)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: