“Knitting is basically coding”. Its a comparison I have heard banded around by a couple of senior tech experts and one that I frequently use myself to either tech scared knitters or craft scared coders; but how true is it?
A knitting pattern is essentially a computer program: it defines what you need to do in the correct order to achieve something. The difference between the computer program and a knitting pattern is the thing that carries out the pattern. In one it is a computer (with no knitting needles), in the other, a human (with knitting needles).
Really? It’s that simple?
Yes, and to demonstrate this, I will now write out two different knitting patterns, in pattern style and ruby style, so you can see that there is very little difference!
“Cast on 126sts.
Work in K 1, P 1 rib for 1.25ins.
Work in stocking stitch for 3ins, finishing at end of a P row.
Next row: K two stitches together, K across row.
P two sts together, P across row.
Rep until you have 10 sts.
Thread yarn through top 10 sts, pull together and stitch down the side to finish piece.”
hat = Hat.new
hat.rib_stitch(length: 1.25, knit: 1, purl: 1)
until hat.stitches == 10
And in human language:
Cast on 126 stitches onto your needle
Work in 1X1 rib stitch until your work measures 1.25 inches
After this, work in stocking stitch (purl one row, then knit the next) for a further 3 inches
On a knit row, knit the first two stitches together and knit to the end of the row.
On the next row, purl the first two stitches together, and continue along the row.
Repeat this (decreasing) until there are only 30 stitches left on your needle
Then cast off
Thread the yarn through each of the top stitches and pull tight to gather the top of the hat. Then stitch the two sides together to finish the hat.
So – can people who knit code and vice versa?
Yes! It’s all about taking it one step at a time and not being put off by the big picture. Tell me to knit a jumper and I will run a mile – tell me to just knit one stitch – then if I repeat that a gazillion times to make a jumper – much easier to deal with!
Its the same with programming:
“Can you build me a website?” – “Errr…”
“Can you just take a couple of these lines of code and repeat them until you have a website?” “Yes!”