Slowing Down to Win the Race

Dan Eder
3 min readOct 6, 2019

--

Photo by Craig Adderley from Pexels

Tonight I sat down to work on a Hangman problem from Exercism.io. It’s the kind of exercise every beginning coder should be able to knock out, but it had stumped me for weeks. My code had gotten so complex I had to put it down and step away. Then I couldn’t get back into it once I put it down.

A few weeks passed. Then tonight, I sat down, deleted my old file, and in about 2.5 hours I knocked the whole thing out from scratch.

What was the secret weapon? A pen and paper.

Sometimes we’re so excited to work on a new coding project we forget the planning phases.

The result is like stepping on a flight bound for 3 weeks in Nepal and forgetting to pack toilet paper — or anything else. You end up stranded, praying for a miracle with every new line of code you try. You can practically smell the flop sweat on the screen.

This hangman project was that kind of challenge. The first time around, I had tried to solve for everything in my head, as it came up. I tried a method for keeping track of remaining guesses, got a little stuck on that, then ditched it to pick up and switch over to another method on how to evaluate whether a guess was a correct one or not. All the while I was commenting out bits of code I was only partially done with, leaving a future (pissed off) me to try to decipher what exactly those lines were supposed to do.

This time, I sat down and took a few notes before turning on the computer. The first thing I wanted to get down was all the mini tasks that would be needed to complete a game of hangman:

  • Establishing a hidden word
  • Asking for a guess
  • Saving user input
  • Tracking the remaining guesses / telling the user
  • if correct: eliminating that character from the word and continuing on
  • if incorrect: returning to the guess and continuing on
  • Letting the user know when she lost
  • Letting the user know when she won

This list was a game changer. Now I could just check off things as I did them, like I was shopping for groceries.

Even better, I didn’t have to follow the game plan exactly. I could knock out things that I knew were easier first, then build on them when figuring out the harder stuff (like how to tell the person they actually won —the house doesn’t like to do that).

Ultimately, this was an awesome 2.5 hours, prob the best I spent coding in a while. I learned about flow of control / how where you put an expression inside of a function makes all the difference. Also used a while loop (love those, but don’t use them so often), successfully used list comprehension again, also this neat little expression to eliminate items from a list when I had gotten a guess that was in that word.

hidden_word = [x for x in hidden_word if x != guess]

This knocks out all instances of the guess in one go, rather that iterating through with a ‘for’ loop like my clod ass was going to do (before I found this idea on Stack Overflow). Pretty sweet! I’m going to submit this to Exercism.io even if it’s not exactly what they were looking for. It got the job done, which is nothing to sneeze at (gross expression).

--

--

Dan Eder
Dan Eder

Written by Dan Eder

Full Stack Web Developer, polyglot, songwriter, always learning

No responses yet