1988

How to Learn a New Programming Language

I like learning new programming languages. Over the past year, I’ve discovered the joy of Elixir, Go, and Haskell. Each one has different strengths and idioms, and discovering them is often the hardest part of learning a new language. How can I ensure I’m really using the language, not just making an overcomplicated “Hello, World” program?

I’ve found that the best way to really use a language is to build a project. The project should be simple to explain, so that I can keep the end goal in mind as I struggle with the new language. And it should require real world features like JSON parsing, so that I run into edge cases and general weirdness that I wouldn’t run into if I built something simpler.

Coming up with this kind of Goldilocks project is difficult, especially in an unfamiliar language. In the past, I’ve given up on languages because I couldn’t figure out what to build with them. What if there were a standard project that I could use to learn a new language?

Good news: that project totally exists.

I’ve built it in three separate languages and it covers a substantial portion of real-world usage in each language:

What’s the Goldilocks project?

The project is based on Ian’s girls_just_want_to_have_puns Ruby gem. It uses the Rhymebrain JSON API to find the best rhymes for a given word (like “mart” for “heart”), filters a list of idioms to only the ones that have a rhyme in them, then replaces the rhyme in each phrase with the original word to create a pun.

For example, given “heart”, it will return “put the horse before the heart” and “off to a good heart”. (I didn’t say they were great puns.)

So far I’ve built this project in Go (gorls_just_want_to_have_puns), Elixir (elixirls_just_want_to_have_puns, with Jason Draper), and Haskell (girls-just-want-to-have-punctors).

It’s been effective and illuminating in every case.

“Wait. Where do I even start?”

With new languages, it can be hard to break down a problem into the smallest steps, because you’re so used to taking comparatively large steps with a language you know. So I broke it down for you.

Here’s a painstaking list of steps:

  1. Create a punny name for your project based on the name of the language. (There are some ideas at the bottom of this post.)
  2. Create a new empty project using the language’s preferred method
  3. Ensure that one command (go run, mix escript.build, etc) will build and run your empty code. Run that command very frequently to check your progress.
  4. Choose a hardcoded word like “heart” to make puns on.
  5. Print out the raw unparsed JSON from Rhymebrain for that word.
  6. Create a new type to store each rhyme from Rhymebrain from the JSON.
  7. Parse the raw JSON into a list/array/vector/etc of that type.
  8. Rhymebrain assigns each result a score, which measures how good a rhyme it is. Filter the list to only results that have the highest score (for example, all results with a score of 300).
  9. Print out every line from the phrase files.
  10. Print out only lines from the phrase files that have one of the rhymes in them. For example, Rhymebrain returns “part” as a rhyme for “heart”, so the results should include “part and parcel” but should not include “crash a party”.
  11. Print out phrases with each rhyme replaced with the original word.
  12. Create a Pun type to store the original phrase and the pun phrase.
  13. Create a Pun for each phrase and print it out as “off to a good heart (pun of off to a good start)”.
  14. Ask someone who knows the language better than you do to critique your code. Don’t skip this one.
  15. Bonus: Let the user provide a command-line argument and use that as the pun word instead of hardcoding “heart”.

All that might sound too hard as a first project, which is totally fair. In that case I recommend my friend Edward’s post on making a Markov chain generator.

Projects around the web

If you made your own pun generator, let me know on Twitter or via email! I’ll add you to the list below.

Here’s every version of this pun project I know about:

Language Project
Ruby girls_just_want_to_have_puns
Go gorls_just_want_to_have_puns
Haskell girls-just-want-to-have-punctors
Elixir elixirls_just_want_to_have_puns
Swift girls-just-want-to-have-op-pun-als
Rust girls-just-want-to-borrow-puns
Rust Rurals-just-want-to-have-puns
Clojure clojirls-just-want-to-have-puns
Java girls-just-want-to-have-punfactory-factory

Project name ideas

The long name means you can come up with a different pun for every language:

Go forth and learn!