10 January 2022

Is it greppable?

is it greppable?

Here’s a useful question to keep in mind when seeking to write maintainable code: “is it greppable?”

Grep is the ubiquitous command-line search utility, which was the seed for a meandering conversation last week.

The purpose of the question is, if I return to this code in the future, can I find a function name, string constant, class, or variable by doing a simple grep for it? If so, then it will be easier to understand the landscape of the project and to make updates more safely. However, if I’m getting cute with metaprogramming or going over-the-top making things DRY, then I might not. In this case, I’ll have a harder time piecing the code back together and I might miss something important in a refactor. Code that’s too DRY (pun incoming) is prone to cracking.

As a trivial example, here are a bunch of similar strings in Python:

# GOOD
STATUS_ACTIVE = 'status_active'
STATUS_INACTIVE = 'status_inactive'
STATUS_TRIALING = 'status_trialing'

a well-intending, but misguided developer could take care of that shared prefix as such:

# BAD - NOT GREPPABLE
_PRE = 'status_'
STATUS_ACTIVE = f'{_PRE}active'
STATUS_INACTIVE = f'{_PRE}inactive'
STATUS_TRIALING = f'{_PRE}trialing'

While the variable names will be easy to find, if someone searches for one of the strings, it could easily be missed. Furthermore the code is harder to read. In some ways, contending with this question can help offset tendencies to elegantly complicate code.

This matters a lot for dynamic typed programming languages (Python, JavaScript, Ruby, etc.) where you can’t rely on static types when refactoring or searching for references, but I think it’s helpful when writing in any language. For example, it came up in the comment discussion when I was reasoning through enums in Typescript.

Of course this isn’t a new idea, for a deeper dive—along with a look at edge cases—check out: “Grep test” by Jamie Wong.

Grep synecdoche

In writing another blog post about grepping code, I found myself wandering off in a digression that felt like a blog version of the yellow boxes in high school history textbooks (the part everyone skips).

Grep is the command-line search utility. It is so ubiquitous and has been around for so long that “to grep” for something means using any tool for searching through code (or text) on a computer. I was looking for a word to describe this phenomena and was tipped off about metonymy by Claire, and clicking around got me to the more specific word, synecdoche, which is defined as “a figure of speech in which a term for a part of something refers to the whole”. I’d love to hear from an English major if I’m subtly off here, but some examples include Hoover being used to mean vacuum or Band-Aid for adhesive bandage. Even though I might say I’m “grepping” for something—depending on the environment I’m in—I might be using ag in my shell (since it’s faster), the built-in search in an IDE (cmd+shift+f in VSCode), or plain old grep when ssh'ed into a remote machine.

In the aforementioned discussion, I played the pronunciation for synecdoche out loud and remarked how close it sounds to “Schenectady”. This time I was tipped off about a delightfully named Charlie Kaufman movie named Synecdoche, New York. The word itself seems quite pretentious, so it’s funny to see that in the response from critics “some called [the movie] pretentious or self-indulgent, but others declared it a masterpiece”.

Happy new year, thank you for joining me for my first new word of the year. May you have meandering conversations and enjoy obscure wordplay in 2022. Back to regular programming next week.

26 December 2021

2021 Posters review

An overview of blog post images from the past year. read full post…
A React hook for running an Intersection Observer in a Function Component. read full post…
Notes on my experience picking up TypeScript after a long sabbatical from statically typed languages and getting comfortable working with union types instead of enums. read full post…

Peter Coles

Peter Coles

is a software engineer living in NYC who is building Superset 💪 and also created GoFullPage 📸
more »

github · soundcloud · @lethys · rss