Sublime Text Tip: SQLBeautifier
I don’t work with SQL a whole lot these days but every once in a while I find myself needing to debug a query gone wrong or at least look at somebody else’s query just so I can better understand the code that makes use of it.
Unless the query is coming directly out of some Elite Coding Hero’s meticulously clean and well-formatted source code (or at least my own 😁), the query almost never looks “beautiful”. Far from it, actually. Usually it looks like an unholy, hot mess. Ever try to read an SQL statement with 3 or more table joins, multiple sub-statements and waaay too much conditional logic where all of the whitespace and line breaks have been stripped away by a server-side language? It’s not fun. Usually I wind up copying the whole statement out of wherever it came from, pasting it into a new file in Sublime and formatting it by hand – like an animal – just so I can read the darn thing.
Enter SQL Beautifier
That’s why I was so psyched to find SQL Beautifier by Ziang Song. It’s a Sublime Text plugin that automatically formats any selected SQL in your code. It almost entirely removes the need for me to waste time reformatting SQL code. Just select your SQL and hit CMD+K+F
on your keyboard (CTRL+K+F
on windows/linux) and that disgusting pile of SQL turns into some pretty decent looking code! If your file contains nothing but SQL (like a fresh file where you just pasted in a query for instance) you don’t even need to select anything. Just CMD+K+F
and boom, you’re in clean code town. Amazing.
SublimeFormatSQL looks like an alternative as well – I haven’t tried it but it uses the same python-sqlparse library as SQL Beautifier. SQL Beautifier claims support for Sublime Text 2 and 3 however, whereas SublimeFormatSQL only lists support for Sublime Text 2 (although who knows, maybe it works in 3 as well – your mileage may vary).
Beautifiers Abound
There are actually a ton of beautifier plugins available for all sorts of languages if you’re like me and weren’t aware of this. You can beautify terrible looking Ruby, PHP, SASS, JavaScript, you name it. If somebody or something produced code with horrible indentation, no regard for whitespace or mixed capitalization and you’re forced to look at it, you need to get in on this.
Not a Sublime user? Atom users have a package that aims to centralize ALL of the beautifiers into one easy-to-install package. It’s called atom-beautify and apparently I’m the last person on Earth to learn about it because it’s got over 3.6 million downloads. Jeepers! Better late than never I suppose.
Level Up
Learning about beautifiers feels like unlocking some kind of new super power – it’s tools like this that make you a better, more productive coder and makes co-workers look at you like you’re some kind of magical code wizard. Now coming across ugly code is almost fun because it’s an opportunity to show off a cool new trick! Hopefully this tip replaces some of your pain with fun too. Now go forth and make your code beautiful!