Written by: Gabor Kovacs, VP of Delivery
But the best and possibly the most “acceptable” is the string to Number conversion, which is very common in Clash of Code first lines:
A simple + sign can convert a string to integer.
I would not recommend you use the previous trick in production code, but it is not something (specially the second one) that will eventually not appear in your minified is code. The one now should never be mentioned in real life circumstances, simply because it is already obsolete, but somehow still works.
This one should be basic knowledge, but despite it works from C to almost all the C based languages, people commonly forget the “comma”, aka you can have multiple commands where you should have only one, if you separate them with a simple “comma”. It not only works when you initialize values, but most importantly within a for cycle command, allowing you to not put brackets, or within the part commonly used for the iterators to have multiple initialization or step. I have used in production code before the latter one, when I had to increase two variables for a for cycle, and I think it is a cleaner code, than another solution.
Here is an example with both three use cases:
Once you already know the previous trick, here is another thing to make you for cycles, especially if it is used for a repeat(n) purpose to be shorter. Here is a common solution to the repeat solution:
And here are shorter solutions:
Here is the common solution:
With a little trick, we can make it two characters shorter:
Yes, those are “backticks”, replacing both the brackets and the quotation marks. If you wonder, this works for any separator, and for any function that requires the input as one string. One of the reasons not even jsonminify-ers are doing as short codes as some of my Clash solutions is because it is not something they do when minify-ing.
The solutions are somewhere around the term “Template literals”, and specially where you can write functions with string inputs to work around a template literal as an input. It is called “Tagged templates”, and, I never used that for what it was meant to do, but it gave the idea of this very strange workaround to make code shorter.
Did you learn something new? Will you use these in coding challenges? I want to hear your suggestions on the LinkedIn comments.