Hash Fetch Instead of If/Else

Conditional logic has its place, but often there’s a better alternative. Today, we’ll look at a Ruby solution: a hash with .fetch. My thanks to Brian Dunn, who showed me the value of this technique. Imagine you have a method that translates some data, a symbol for a role, into a presentation. A conditional is a popular solution here: def display_name(role) if role == :customer 'angler' elsif role == :employee 'guide' else name.to_s end end I think this method is much improved as a hash: ...

April 16, 2022

First Get It Working, Then Make it Look Good

I recently completed a winter survival course where we built shelters in just ten minutes with only the contents of our packs. The pack I brought was nearly empty, so I made a tent out of my parka. It was ugly, but it could have saved my life. How does this apply to software? When building a feature, first get it working, then make it look good. Let’s say you’re building an app that queries a third-party API to get data. “Get it working” could mean getting the API to return the data you need, and then dumping it onto the page as a raw object with JSON.stringify. It works! ...

April 13, 2022

My psql Config

RC files, thought to stand for “run command” files, are read by a program during startup. The file that configures the PostgreSQL REPL psql is called .psqlrc. In this post I’ll share my personal psql configuration. Note: many of these configurations came from my time at Hashrocket. We documented some these preferences via PG Casts. Here’s my .psqlrc. \set QUIET on \pset null 'ø' \x auto \timing \set HISTFILE ~/.psql_history-:DBNAME \set HISTCONTROL ignoredups \set QUIET off Let’s look at each setting. ...

April 12, 2022

Solved! How to Exit Vim "Ex" Mode

How does one exit Vim “Ex” mode? But first… how did you even get into “Ex” mode? Probably by accidentally typing Q in normal mode: *Q* *mode-Ex* *Ex-mode* *Ex* *EX* *E501* Q Switch to "Ex" mode. This is a bit like typing ":" commands one after another, except: - You don't have to keep pressing ":". - The screen doesn't get updated after each command. - There is no normal command-line editing. - Mappings and abbreviations are not used. If this is true you probably missed the initial instructions: ...

April 6, 2022

How to Read a Stack Trace

A stumbling block for many people when debugging is reading the stack trace. Today I’d like to discuss this important skill. What is a Stack Trace? What is a stack trace? According to Wikipedia: “In computing, a stack trace… is a report of the active stack frames at a certain point in time during the execution of a program.” It’s the answer to the question: “when this system is being observed, what is happening?” Reading stack traces is a core programming skill. ...

April 5, 2022

RSpec.describe vs. describe: Which Should I Use?

Ruby’s RSpec describe block has two common syntaxes. Which should you use? # With `RSpec` module name RSpec.describe TestedClass do end # Without module name describe TestedClass do end Disclaimer: they both work. But I’m assuming since you’re here, you’re curious about the distinction, or want to make a reasoned choice. TL;DR: I suggest using the RSpec module name. The Options The global availability of describe is called the Global Namespace DSL. RSpec made describe available on Object, the default root of all Ruby objects. The intent was to let us write specs without explicitly specifying the RSpec module. ...

April 2, 2022

Your Demo Doesn't Need Auth

When building a demo application, I believe you can often skip authentication. Picture this: a coding school student team demoing a project. Smart and creative folks with lots to offer. Yet they spend half of their limited building and demoing time on auth. Here’s a sample script from the demo: “So we click ’login’ and now we’re on the login page, which we know because the header says ‘Log in.’ Then, we can either log in, sign in, or reset our password. We’re going to log in with a username and password I set up before the demo. I just fill in the username ‘demo-user’, password ‘password’, click ‘submit’, and now… wait for it… I’m on my homepage. See my username in the navbar? That only shows up when I’m logged in.” ...

April 1, 2022

How Urgent Is This Bug?

I remember the first bug that I shipped to production. I was upset that I’d broken something and was anxious to fix it. But I noticed something curious: the calm demeanor of a senior mentor helping me. They refused to meet my intensity. While the world burned, they wanted to instead discuss the bug and its relative importance. Engineers don’t want to break things. We don’t want to make mistakes or look dumb. We don’t want to ship a sup-par experience or disappoint customers. When I was a junior engineer I reacted strongly to bugs that I caused or contributed to, no matter how small. ...

March 30, 2022

Debugging Tip: Learning From Bugs

You were stuck, and now you aren’t. Congratulations! Before you move on, it’s vital to stop and learn from it. It’s the best way I know to get better and spare your mind for increasingly harder problems. “What Did I Miss?” Every time you get unstuck, ask yourself: when I was stuck, what did I miss? Was I misreading a message? Was I hypnotized by an irrelevant detail? 😵‍💫 Did I make a bad assumption and reject information that didn’t fit into my assumption? “What did I miss?” ...

March 25, 2022

How to Make Renames Easy

Names in software are hard. But what’s worse than a bad name? Sticking with it because you can’t change it. I want us all to be able to effortlessly and fearlessly fix bad names. That’s the topic of this post. Considering a “Bad” Name What’s a bad name? Here’s a contrived, but not production-unrealistic, example: function longName() { if(useLongName) { return `${this.firstName} ${this.lastName}` } else { return this.firstName[0] } } A few issues I can raise with this name: ...

March 22, 2022