Code Clubs

I've been getting into golf again, though “stumbling” is a more appropriate. Used to play with my dad and friends intermittently. Then intermittent began to mean years instead of months in between sessions. Now my clubs are out of storage and I hope to keep it that way.

The reason I bring this up is because I've discovered this recreational programming practice called Code golf. The Wikipedia definition is as follows:

Code golf is a type of recreational computer programming competition in which participants strive to achieve the shortest possible source code that implements a certain algorithm.

The main site (if that's even true for such a thing as “code golf”) includes numerous challenges with numerous languages and a leaderboard to egg you on. It's not my kind of golf, but I appreciate the spirit of Code golf — driving towards concision.

I had an instance today at work where we were trying to parse data out of an AWS CLI command. This was the initial go around:

aws rds describe-db-dngine-versions --engine postgres --engine-version 9.6.20 | grep -a 500 "ValidationUpgradeTarget" | grep "EngineVersion" | sed -e 's/"//g' | sed -e 's/EngineVersion: /PostgreSQL /g' | sed -e 's/,//g'

Then we played around with using another “club” instead of grep & sed — jq. With jq, the command turned to this:

aws rds describe-db-dngine-versions --engine postgres --engine-version 9.6.20 | jq -r ".DBEngineVersions[].ValidUpgradeTarget[].Description"

Accomplishing the same thing in fewer lines. Though it's more than that. While the second solution with jq is smaller, it's also more readable. You can easily infer the structure of the response.

Code golf judges your code by the number of characters & byte size. The real world judges your code by clarity.