Solving Exercism's Resistor Color Duo in TypeScript

Today as a code kata I completed the ‘Resistor Color Duo’ TypeScript exercise on Exercism. Here’s my work. This exercise asks us to write a TypeScript function that takes two color names (e.g. ‘brown’ and ‘black’) and returns the electrical resistance they would produce on a resistor as a number (10 in this example). My Goals I approached this challenge with the following goals: Solve the problem Learn TypeScript better Leverage implicit typing Here’s the spec and my solution. ...

October 24, 2022

Print Last Exit Code

The exit code of a command in a Unix-based system is an important and easy-to-miss piece of data. It isn’t printed to standard out; you have to go looking for it. I find it useful to inspect this information when debugging or considering chaining unfamiliar commands. To see the exit code of your last command, echo its variable ($) value: $ echo "something" "something" $ echo $? 0 # Success! # '1' example $ rm nonexistent-file rm: nonexistent-file: No such file or directory $ echo $? 1 # Failure. The two exit codes you must remember are 0, command was successful, and 1, a catch-all for errors. Zero is success, not-zero failure. ...

October 21, 2022

You Can't Change Nothing

I saw a meme this week: a person debugging code, “My code doesn’t work. Let’s change nothing and run it again.” This is something that I’ve done. It seems pointless. But that’s not quite correct. Repeatedly “running it again”, by loading a page, printing something, or starting an automated test, without making a prediction– yes, this is a bad practice. But sometimes trying something twice is a solid technique. The reason is that even if you don’t make a change, that doesn’t mean that nothing has changed. ...

October 21, 2022

Read the Docs

When learning a new language or framework, the first thing I do is read the official documentation cover-to-cover. Choose the latest version, or if you’re stuck with an older version, go with that. For some tools like Go or React, this can be done in an hour or two. For others like Ruby, it’s more of a challenge, so try to focus on the standard library or official guides. Read quickly, try stuff out, jot down a few notes. ...

October 19, 2022

The Factory Pattern, Explained in Ruby

In this post, I’ll explain what factories are and why you want them, using Ruby. The FactoryBot gem is ubiquitous in Ruby and Ruby on Rails testing. If you aren’t familiar with it, you might be wondering, what’s the point? Wouldn’t it be simpler to build objects myself? An Example Let’s say we’re unit testing a full_name method on a User class. full_name takes two pieces of data, first_name and last_name, and combines them with a space. Let’s also assume that we’ve tried to ensure data integrity by requiring the presence of these two fields. ...

October 17, 2022

Default to Programming Convention

Trying to enforce unconventional coding preferences on a team is an uphill battle. When in doubt, default to convention. Contrived Example Consider this contrived example. Imagine that you don’t like Ruby on Rails delegators, which let you call a method on an instance through another instance. Here’s an example: class Greeter < ActiveRecord::Base def hello 'hello' end end class General < ActiveRecord::Base belongs_to :greeter delegate :hello, to: :greeter end General.new.hello # => "hello" .hello is not defined on class General, yet we can call it because it is delegated to Greeter. Pretty cool! ...

October 14, 2022

Recommended Reading

Here are some books that have helped me understand programming. This list is a living document that I plan to update as I read. Recommended Reading Beck, Kent and Andres, Cynthia. Extreme Programming Explained: Embrace Change. XP won, and this book from Kent Beck is the primary source. I revisit it often to find or refresh my ideas about software delivery. Hoover, Dave and Oshineye, Adewale. Apprenticeship Patterns: Guidance for the Aspiring Software Craftsman. Dave and Adewale provide a template for helping yourself or your teammates grow in their early careers. Some ideas that have greatly influenced me: wearing the white belt, exposing my ignorance, and being the worst person on a team. ...

September 28, 2022

How to Introduce New Ideas

How do you introduce new ways of working? We want to bring a new idea to our engineering teams, such as a new command-line configuration, testing tool, or design pattern. But engineers can be discerning and stubborn customers. The idea needs to stand on its own. And, you have to sell it. Here are some approaches that I’ve tried to introduce new ideas to a team. Combine them all, and you’re on a good path. ...

September 8, 2022

Computers Can Be Understood

A big mental leap I made early in my programming was starting to view computers as something that could be understood. What is a computer? It’s a piece of silicon. You can recreate a basic version of it with water or even Minecraft redstone. Each abstraction on top of that piece of silicon is a set of instructions that combine into something remarkable. Something that you, with enough time, can understand. ...

September 6, 2022

Vim Scratchpad

Today I’d like to share a small part of my practice: using a persisted scratchpad text file for jotting things down. When coding, often I’ll need to save a piece of data or type something out. This could be the identifier of a record, or a list of steps my pair and I think we need to take to fix a problem. Instead of creating a new file each time, I keep a scratchpad in my root directory, named ~/buffer. I’ve mapped Vim to open it from normal mode with <leader> g: ...

August 25, 2022