How Knitting is Like Coding
--
Fifteen years ago — in high school — I learned the basics of programming, in an Introduction to Computer Science course at Thomas Jefferson High School for Science & Technology in northern Virginia. Around the same time, a classmate taught me to crochet. At the time I didn’t realize how much coding and fiber arts had in common. After all, to quote Rose at What’s In a Brain, “Programming is analytic and something that Silicon Valley geniuses do a lot; knitting is artistic and something that your grandmother does a lot.”
In the last year I’ve stretched my skills in both the fiber arts and technical arenas — I learned to knit (crochet’s cousin) and I delved back into programming, including my recent acceptance into Launch Academy in Boston, MA. Hours of staring at knitting and crochet patterns, and a bit of time writing and editing my own and researching best practices for doing so, has led me to look for knitting-like patterns in my code and coding-like patterns in my knitting.
Don’t Repeat Yourself is a fundamental programming principle which encourages programmers to recognize and eliminate duplication. Well-written knitting patterns have this down:
Round 4: k5, * ky0k, k4, p4, k4, rep from * around.
k5 means “knit each of the next 5 stitches.” The first application of D.R.Y. here is the consolidation of five identical actions into one symbol — wouldn’t it be tough to read if I wrote “k, k, k, k, k”?
The big space- and brain-saver here, though, is the judicious use of the asterisk. A certain pattern is formed — you make a special stitch (kyok means “knit, yarn over, knit” all into one stitch), then you knit four stitches, purl four stitches, knit four stitches. Then you repeat that sequence, possibly dozens or hundreds of times. Nobody wants to read “k, yo, k, k, k, k, k, p, p, p, p, k, k, k, k, k, yo, k, k, k…” filling up three whole pages, let alone try and knit from it. Knitting patterns have historically been printed in books and magazines (with a significant recent shift to downloadable PDFs, but that’s another story) where every inch of space is financially valuable.
The asterisk allows you to make a loop — every time you reach the second asterisk, you go back and start over at the first asterisk. When do you stop? In this pattern, when you reach the end of the round — like a while loop. As long as the round isn’t complete, keep repeating the pattern.
If you think about the whole snippet of directions above as a while loop, can you see the other kind of loop inside? k5 is sort of like saying
(0..4).each do |stitch|
k stitch
end
Or, I could write the whole round in terms of a for loop if I knew how many repeats of the snippet I needed:
Round 4: k5, * ky0k, k4, p4, k4, rep from * around.
(0..2).each do |pattern|
kyok, k4, p4, k4
end
Obviously, I took a shortcut there with some pseudo-code — perhaps each abbreviation is its own loop nested in the bigger one, or calls its own helper method.
Interested? There’s plenty more out there on the parallels between knitting and coding — check out this post on the Codecademy blog, and here Karen Shoop of Queen Mary, University of London, explains how to rewrite a knitting pattern as a regular expression.