While reviewing an open source React Native project I’d worked on recently, I was surprised with how difficult I found parsing the SQLite statements that I’d written in JavaScript. It seemed that my best efforts of breaking a long statement into multiple lines had added a number of characters which negatively affected the human readability of the code. Take the following CREATE TABLE
statement as an example:
It turns out that SQLite is very flexible when it comes to the whitespace used between tokens in a statement (from the docs):
Ordinary SQL statements are free-form, and can be spread across multiple lines, and can have whitespace and comments anywhere.
Knowing this, it dawned on me that there was really no benefit in trying to concatenate everything into a single-line string. It only took rewriting a single statement to use template literals before I was hooked. Here’s the same statement from above tidied up using template literals:
Big improvement!