Variable Names

Here are some hints about naming variables and functions.

Life hack: It’s easy to get stuck on function naming. Instead of doing that, just write a first draft of the name without thinking about it too much. Then when you call the function the first time then type what you expect the function to be named. If that’s what the function is actually named, good, you are done. If not then rename the function to what you expected it to be called.

Boolean variables should have boolean names: if (valid) {

Do not use negative names: if (not_valid) { <– BAD

Use common standard names. In the kernel “int ret;” is by far more common and “int err;” or “int rc;” so use “int ret;”. Use “int i;” for iterators. If the variable is weird though, then naming it something different. For example, if you have custom error codes then use “int status;”

“tmp” means “temporary”. “temp” means “temperature”. “buf” means buffer. “buff” means a fit dude in a muscle shirt.

Don’t clash with the global namespace. In the kernel, you need to use subsystem prefixes for your functions so you don’t clash with future global functions.

On the other hand don’t use too long names. Long names are an issue if you want to keep the lines under 80 characters long. This is especially a problem for Enterprise Software (derogatory). BIG_COMPANY_PROJECT_I_VARIABLE. This kind of code cannot be written by hand, it has do be done with copy and paste or autocomplete. If you can’t write your code without machine assistance, then how are humans expected to read your code? The human mind has real limits to how it absorbs information.

Sometimes it’s like reading each variable is a game of Where is Waldo to find the information. I recently fixed a bug where someone mixed up mbox->f2hq_lock with mbox->h2fq_lock. LOL.

My Smatch code naming has not all been done super well. I wish that had made things more uniform. I don’t use ASSIGN vs ASSIGNMENT consistently. Also var/sym vs name/sym. I always promise that I’m going to go through and clean things up but so far I have not. Some of that code is over ten years old and it’s a constant process of learning.

That’s all the tips I can think of. If you have tips then put them in the comments.

2 responses to “Variable Names”

  1. I like all your suggestions and use them regularly — except your recommendation against long names. When reading code in very large systems, long names are often very useful.

    > If you can’t write your code without machine assistance, then how are humans expected to read your code?

    This is a non-sequitur. Long names make code harder to write but easier to read, which is the correct tradeoff. Which is easier to read, “parent_to_child_writer_pipe” or “p2c_pipe[1]”?

    > The human mind has real limits to how it absorbs information.

    Yes, and using short names that require decoding via memorized or searched context makes more work for human minds, not less.

    Like

  2. The name parent_to_child_writer_pipe() is fine so long as there isn’t a parent_to_child_reader_pipe(). In that situation the reader vs writer information is tucked into the middle of the sentence instead of a the start or the end. It’s absolutely going to lead to copy and paste bugs.

    Reader vs writer is not too bad even. I have seen sentence variables where you had to find single letter differences between them.

    Like

Leave a comment

Design a site like this with WordPress.com
Get started