Why You Should Avoid Putting Types in Variable Names

This article was automatically translated from Japanese by AI. Read the original Japanese version →

You should avoid including types in variable names—though I accept there may be dissenting views. First, let me tell you about a recent refactoring (?) ordeal that gave me a really hard time.

Suppose you have a User class. I'm sure most of you would write something like this somewhere in your code:

user = User.new(name: 'hoge', age: 12)
print user.name

Even when using Ruby on Rails, if there's a User class in the Model layer, you'd almost certainly write code like the above (after all, even the Ruby on Rails Tutorial does it!).

Now suppose for some reason we need to rename the User table to Customer. Say the service switched to a paid membership model, so Customer feels more accurate than User (?). And this turns out to be way more painful than you probably think.

Table renaming aside, replacing the class name itself goes smoothly enough. With an IDE or similar tools, that only takes a few minutes. The problem is what comes afterward: renaming all the variables. Take the user variable in the snippet above—how do you even track them all down? Sure, you can search for the User class and check nearby code, or grep for the string user to find most of them, but it's obviously a difficult thing to batch-replace safely. There might be slight variations like tmp_user, or names like usr where you aren't even sure if it's a typo or an abbreviation.

From that experience, my conclusion is that putting types in variable names makes refactoring harder.

That said, in dynamically typed languages it's tough to keep types out of variable names completely. For method parameters in particular, the name is the only clue to what kind of object it is. Without it, how would you guess which methods parameter x responds to? The same goes for instance variables and class variables.

Still, for variables declared inside a method (local variables), couldn't we leave the type out? All you have to do is keep the variable's scope short enough. What about a rule like this instead: Make sure every local variable's declaration and the end of its scope fit on the same screen.

In my coding environment, my display shows around 40 lines of code at a time, with lines around 120 characters wide. Keeping every variable's scope within that window—meaning splitting methods or breaking up blocks to make it fit—seems totally doable, right? Then you can give variables simpler, type-free names like tmp, a, b, x, y, id, obj, new_obj, or old_obj. That way, when a table name or class name changes, you won't get bogged down updating variable names.

Having written all that out, though, I feel like in practice it'd end up making the code harder to read anyway, so I'm a little conflicted. Thanks for reading this far.

Update 2017/06/23 15:07

After finishing the post, it occurred to me that the real reason for the pain above was probably failing to consider likelihood of change in the design. By "likelihood of change," I mean that if you're building an app in Ruby on Rails, the name ActiveRecord (probably) won't change, whereas the classes you define yourself are far more likely to change. In general, code from gems or abstract layers is less likely to change, while code directly tied to business logic changes much more often.

With that in mind, while user is still a poor variable name, record or rc will almost never need to change. They're abstract, yet resilient to change. If you use names like that while keeping scopes short, it doesn't seem like readability would suffer much at all. Maybe the real takeaway is simply: don't put volatile types in your variable names...