Apollune

Not-so-short ideas that cross my mind just before I'm going to sleep (tw: @_apollune_)

The right length for variable names

Something I enjoy doing is devoting some extra seconds to decide the right name for variables.

I used to struggle with the arbitrary ways to use foo and bar in examples meant for newbies. Also found difficult to follow code where short names where abundant (picture a snippet full of interactions between x, c, r and z1).

I received as feedback once that it's better to stick to the variable names most commonly used for things in the language you're writing in (Go comes to mind as one language where popular libraries promote the use of one-letter names).

r := mux.NewRouter()
// Only matches if domain is "www.example.com".
r.Host("www.example.com")
// Matches a dynamic subdomain.
r.Host("{subdomain:[a-z]+}.example.com")

Example from mux, a router written in Go

While I see the importance in following the best practices that the communities around tools suggest, I've found myself in ”multicultural” codebases with business contexts that called for critical thinking, flexibility and practicality.

Imagine inheriting a Go service that devs with different experience levels and backgrounds contributed to, imagine that you're in charge of recruiting the new team skilled in a different stack that will maintain it and work on tools to replace specific parts of it.

Maybe you hire a Python dev open to fix a thing or two in Go. Should they be expected to know and keep following the naming conventions from the Go community? Even if they're going to be alone maintaining it as a sporadic side quest for a long time? What if there's a solid chance of the system not being needed anymore in the next months?


Some years ago, when I was just an enthusiastic girl that hacked together things using Phoenix à la Rails, I interviewed for a Ruby position at Conekta. I carry with me a question that I was surprised to answer correctly: how different would be to document a system made in Elixir for people used to work with it vs a team new to its ecosystem and functional programming?


I wouldn't say I know what's the right length to name a variable and I don't even strive to one day enumerate you the infallible steps to find those elusive perfect variable names.

I just try to picture the next person that will interact with my code by considering the company the system belongs to, the median English level of my peers, the experience I perceive with jargon I know but others might not, the vocabulary (even if misused) already present in the codebase, the tools my team has experience with.

Right now I'd like to say that for me, it depends on the social context the variable will live in.


But don't leave yet! The Universe might have a say in the ideal length for the next variable you need to name: click here to get a personalized recommendation.

Mine was 10 😬