My favorite finds when golfing

Simply googling for tips for golfing in {insert language of choice} will return hundreds of links with discussions, arguments and tips for doing so. Some of my favorite finds that can be useful and that I wasn’t aware of, in no particular order:

Destructuring or unpacking an array

You’re probably familiar with an expression such as [a,b,c]=[1,2,3] which allows us the initialise the three variables with the chosen values in one simple expression. But you can also rely on any previous assignment on the left on the subsequent ones, for example [a,b,c=a]=[1,2] will give c the value that a has in case there is nothing to unpack for it.

What about unpacking into an existing array, even pushing into it by relying on the length property. b=[],[a,b[b.length]]=[1,2] will add the second value 2 to the end of the b array. Is it useful in your day to day coding? Probably not, but with the advent of ES6 syntax you better be used to this because it’s not going away anytime soon.

Map, reduce and filter

Understanding, and I mean really understanding what these do and what their differences are is crucial if you want to take advantage of the language. Without getting too much into them, knowing that reduce is meant to be used as a custom single value extraction from your array, something like what Math.max does but with your own logic, map is meant to return a new array with the values altered from the original array without having to manually loop through it, for example [1,-2,3,-4].map(Math.abs) will call Math.abs on each of the values and return us the array [1,2,3,4] as expected, as opposed to something like:

var rawValues = [1,-2,3,-4];

var parsedValues = [];

rawValues.forEach(function(value, index){

parsedValues.push(Math.abs(value));

});

Last but by no means least we would have filter which expects a callback that will determine by the truthiness of the returned value whether an element of the array is preserved, or kicked out. This one also returns a new array with the filtered data. To finish off with an example, say we have an array of mixed values and we want to keep all the valid numbers different from 0, that is, remove strings, arrays, objects and other invalid numbers.

With filter we can do [0,1,2,3,"invalid",[],6].filter(Math.sign) and we would be returned [1,2,3,6] without having to implement the logic that determines whether or not every element is a number. And the golfer in me would also like to point out that it could be further squeezed into a shorted expression changing the .filter(Math.sign) with .filter(a=>+a) .

Arrow functions

Arrow functions also have their own quirks like the way you need to wrap an object if it’s returned in parenthesis because if you don’t then it matches the syntax of an expression instead of an object. I had read about it but never experienced it first hand until I had to do multiple things inside an arrow function.

So for instance I originally expected (B,C)=>{D:2} to be a function that returned {D:2} when called. However, it returns undefined, although the syntax is valid and nobody complains. If you wanted to return an inline object you’d do (B,C)=>({D:2})

Also, if you are executing multiple expressions inside the arrow function you’ll need to explicitly call return or chain them with && as long as the expressions return true so for example if we had a function that logged something to the console and then returned the parameter passed it could be written as (A)=>{console.log(A); return A} or (A)=>!console.log(A)&&A since console.log returns undefined.

One last thing about arrow functions that also applies to the unpacking section above. The default values for when parameters or values are missing can interact with the other variables to the left. So for example in the function (A=2,B=A**2)=>console.log(A,B) the value of B will be A to the power of two, unless we receive something else.

About the golf specific languages

This is a bit of a touchy subject even within the golfing community. Some people have created programming languages that are aimed solely at golfing and they compete alongside others that use day to day programming languages. There are those who hate this, those who embrace it, and those who accept it, I’m part of the latter group, but I by no means want to discredit the effort put into both creating the language and playing in it, I find those esoteric programming languages fascinating, but from a distance.

Conclussion

I’d argue that code-golf can be greatly beneficial as long as we keep our mind in the game and not try using it where it doesn’t belong. The things I pointed out on the story may or may not be news for you but I believe that an excellent developer is not the one who just does his job well, a great developer needs to love to code. If you love to code you’d love something playful and harmless such as this and if by playing you can also learn a thing or two then it’s a win win situation.