Crafting as Code

Jenn - Feb 4 '20 - - Dev Community

Knitting and crocheting has had a recent comeback. I can be spotted knitting at conferences and meetups. What makes knitting so similar to programming?

The patterns!

Hello World

Squares and rectangles are the "Hello world" of knitting and crochet. These first projects are easy to travel with, often use a small number of stitches, and can be completed quickly enough to give the crafter an idea if they want to keep making things.

A typical dishcloth pattern

Gramma's Dishcloth

Cast on 3 stitches.
Knit one row.
K1, Kf&b, K1.
*K2, yo, knit to end.* (For no holes,*K1, kf&b, knit to end.*)
Repeat from * to * until there are 45 stitches on your needle.
K2, yo, k2tog, knit to end; repeat this row two more times.
(Knit three rows plain for no holes.)
^K1, ssk, yo, k2tog, knit to end.^ (For no holes,^K2, k2tog, K to end.^)
Repeat from ^ to ^ until 5 stitches remain.
K2, k2tog, K1.
K1, k2tog, K1 (3 stitches remain).
Bind off, cut yarn and weave in ends at corners.

Knitting is manipulating arrays

Each row is an array carried on a needle. Stitches are popped, spliced, and shifted as per the pattern.

Knitting patterns are code!

Stitches are array methods

In knitting there are two main stitches: knit and purl. They are made in opposite directions of each other (knit is in front of the needle, purl is in the back). They can be thought of as 1's and 0's.

All the different knitting patterns and stitches are combinations of these two stitches.

The above pattern has five different stitches:

  • K: knit
  • Kf&b: knit the front and the back of the same stitch
  • K2tog: knit two stitches together
  • Ssk: Slip, slip, knit (this also knits two stitches together)
  • yo: Yarn over

Knitting is usually completed from left to right and then the work is flipped over. A pair of rows acts like a stack.

A dishcloth is a multidimensional array

In the pattern above, think of each row as an array. Each row is pushed into our parent array and knit stitches will be represented by a 1. All code is in JavaScript.

Casting on is creating our initial array with three stitches.
var dishcloth = []
dishcloth.push(Array(3))

Knit one row: This is filling the first array.

dishcloth[0].fill(1)

The dishcloth isn't very impressive at this point:

[[1, 1, 1,]]

K1, Kf&b, K1: Knit one stitch, knit and insert a stitch into our array, then knit the last stitch.
dishcloth.push([1,1,1,1])

K2, yo, knit to end: This is our first loop. Knit two stitches, insert a null value, and knit to the end.
dishcloth.push([1, 1, null, 1, 1])

Repeat from * to * until there are 45 stitches on your needle: Repeat the previous row until the array length is 45.

Here is an example of code that could be written for the instructions so far.

You may notice that I reversed the odd numbered rows, I did this to represent how a pair of rows in knitting acts like a stack. This is how the dishcloth gets a decorative edge on both sides.

Other similarities to programming

Patterns can require stitches to be knit out of order (cabling), be multithreaded (knitting with multiple colors at the same time), and use different languages (decorative crochet borders).

There are several kinds of debugging. Two popular ones are:

  • Frogging: Is removing one or more rows (it is called frogging because you "rip it, rip it", knitters love bad puns).
  • Tinking: is knitting backwards to the bad stitch.

Patterns can be written out or drawn as charts.

Every knitter has their own opinion on the best tools, e.g. wood, acrylic, or metal needles and yarns. It can be compared to the language and editor wars.

Knitting is code, you just have to learn the language first.


Photo by Rebecca Grant on Unsplash

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .