It should be the first rule of building any formatted string. I see code daily building HTML, JSON, CSS, CSV, shell scripts or whatever just by bashing strings together. If you are lucky they do some form of escaping most of the time.
Really we should get in the habit of using proper encoders. Don’t think of these types as just strings you can substitute into, use some library that will actually write these things properly. Thankfully JS has JSON.stringify and good object literals so at least you don’t see shitty JSON encoding often (just sometimes when embedded in HTML). I wonder if adding string interpolation was a mistake. It makes it so easy to do the wrong thing. Of course there are cases where you want to format `Hello ${user.name}` or other human readable strings. But more often than not I see it being used incorrectly.
It should be the first rule of building any formatted string. I see code daily building HTML, JSON, CSS, CSV, shell scripts or whatever just by bashing strings together. If you are lucky they do some form of escaping most of the time.
Really we should get in the habit of using proper encoders. Don’t think of these types as just strings you can substitute into, use some library that will actually write these things properly. Thankfully JS has
JSON.stringify
and good object literals so at least you don’t see shitty JSON encoding often (just sometimes when embedded in HTML). I wonder if adding string interpolation was a mistake. It makes it so easy to do the wrong thing. Of course there are cases where you want to format`Hello ${user.name}`
or other human readable strings. But more often than not I see it being used incorrectly.