Things I learnt during my latest Javascript Code Kata

Sometimes I do a code kata at codewars.com. That is a fun way to solve computer science related problems, learn on the way to solve them and especially learn from the solutions of others.

Today I completed the kata “Make a spanning tree” using Javascript. I occasionally use Javascript to write an event handler or so but I don’t have much experience in “modern” Javascript. Here is what I learnt from looking at the solutions of others.

Destructuring

I know this from my Scala class and Clojure.

You can assign array elements to variables:

so “…rest” is assign the rest of the array.

This is nice syntactic sugar also when working with nested arrays. Eg when “edges” is an array of pairs:

There is object destructuring:

and even assigning to new variable names

See MDN web docs for more.

Spread operator to create an array using an array literal

Using an array literal to create an array from two other arrays:

Objects are associative arrays (aka Maps)

Although I already knew this, kind of, this refreshes my JS
knowledge.

First, you can add properties to Objects without declaring them in
the first place:

Second, instead of the dot-notation you can use array index
notation using the property name as the index:

One solution uses this in order to save the weighted edges in an
object just like i did in the proper Map object:

Third, methods are kind of properties, too. In the same solution,
“minOrMaxFunc” is cleverly choosen (“minOrMax” argument is either
“min” or “max”):

it creates an objects with two methods: “min” and “max” and then
accesses the one that is given in the argument. If “minOrMax” is
“min”, a reference of the “min” method is returned.

Strings are arrays

Destructuring works with strings:

and you can index strings:

“var” vs. “let”

Of course, the solutions written in “modern” JS use “let” and
“const” all over the place. I just reassured myself about the
difference between let and var:

First, variables declared in a block using “var” are visible
outside that block and are “known” before being declared:

a block might be a for-loop.

Variables declared using let are not visible outside the block and
are not “known” before declared:

Third, you might not redeclare a variable using let:

So basically, “let” is a sane way to declare variables.

Also on:

Becoming a better programmer

Henry Stanley wrote a blog post “Becoming a dramatically better programmer“. He outlines areas where programmers who want to get better can focus on. That is learning skills directly on the one side and learning meta-skills on the other side. Meta-skills: learn about “deliberate practice” – do things that challenge you and get feedback -, identify common mistakes you make and learn to do deep work – focus on work with no distractions.

My advice for deliberate practice are sites like CodeWars where you can solve exercises (“katas”) in a couple of programming languages. It is challenging (CodeWars has martial art like grades of mastery) and you get feedback by being able to look at solution by other users and thus being able to compare them to your solution.

GOTO Berlin 2017

The last two days I spent at the GOTO Berlin 2017 conference. It’s a conference “by developers for developers”. Three out of four keynote speakers were women (last year four out of four); I have got the impression that inclusivness is an import part of the conference. There seem to be more female attendancees than on other tech conferences.

I enjoyed the conference: the keynotes, the talks, the food and the beverages, the people.

The first keynote on Thursday was held by Anita Sengupta about “The future of Mars exploration“. In the first part, she focused on the Curiosity mission. She developed the parachute that was used during the decent on Mars. Cool. Prof. Sengupta showed us some actual video shootage from the mission. In the second part of her talk she talked about the challenges that a human mission to mars would face, especially radiation.

The evening keynote on Thursday “Number crush” was held by Hannah Fry. She showcased some interesting data from human (and cow) behaviour. It’s hard to summarize her talk in a few sentences. Make sure to check out her website.

Raffaelo D’Andrea held the morning keynote on Friday on autonomous drones. He was part of Kiva Systems, a company that build robots that brings stuff in a warehouse to human packers. It got aquired by Amazon that uses this technology in its warehouses. It’s astonishing to see all these robots moving around bringing stuff from A to B. The main theme was the autonomous drones D’Andrea developed in a company called Verity studios. They are used on broadway, in Metallica concerts. A key concern for him are safeness (no drone is going to crash) and reliability. Very impressive!!!

Susan Landau held the evening keynote on Friday on “Cybersecurity in an Insecure Age“. She talked about end-to-end encryption and locked phones. A thing that was new to me is “tainted leaks” where documents from “a target” are stolen, messed with and then “leaked” in order to discredit the target and generally generate mistrust.

Attendance Joy Clark did some cool sketch note of the key notes and some talks.

I especially enjoyed the talks by Dan North (“How to break the rules” and “Agile revisited“) Gregor Hohpe (“Adopting DevOps? You are Aiming at the Wrong Target!” and “Enterprise Architecture = Architecting the Enterprise?“), Steve Smith (“Measuring Continuous Delivery“) and Adam Tornhill (“A Crystal Ball to Prioritize Technical Debt“).

Also on:

When in doubt, refactor at the bottom

When in doubt, refactor at the bottom by Eric NormandEric Normand (The Practical Dev)
But every ten-line bit of repeated code has nine two-line bits and eight three-line bits. There's probably something there to extract. Start there, with smaller abstractions. Start refactoring at the bottom!

I like Eric Normand’s idea: when in doubt, refactor few lines of code rather than more lines. Extract 2 or 3 lines and give them a name (method or function). I am aware he usually uses Clojure where you often see short functions. But it applies to other programming languages as well.

How to Benchmark Alternative SQL Queries to Find the Fastest Query

How to Benchmark Alternative SQL Queries to Find the Fastest Query (Java, SQL and jOOQ.)
Tuning SQL isn’t always easy, and it takes a lot of practice to recognise how any given query can be optimised. One of the most important slides of my SQL training is the one summarising "how to be fast"

Lukas Eder from JOOQL posts some code to benchmark SQL statement. Comes in flavours of PostgreSQL, Oracle and SQL-Server. Handy.