True Memes of development:  variable naming

True Memes of development: variable naming

At first you may wonder "could this be true". Then you might think "why a platypus?". This is one of the simplest of the memes and yet one of the truest.

Variable naming has been said to take 60% of the time of many developers, and I feel that to be true.

The first problems to plague most students are syntax, typos, and then poorly named variables. Poorly named variables are the most insidious problem, because the pain doesn't manifest right away. It happens days or months later.

The first problem: while trying to figure out your own code it can be hard to follow your own logic. If you name a variable as 'a', it's going to be very difficult later on to remember what it was for. I see this more often with for loops, when people name their index something like i or x. Or even worse, with nested loops, i and j which look very similar, to add extra confusion.

If you're going to name your indices something useful, name them as an index of something. If it's a list of people you are iterating through, name your index personIndex or personPosition

The second problem comes from your coworkers and bosses. How will they be able to quickly understand what your code is doing? A lot of people peg their future on good comments, but good comments are a distant third to good variable names and good function names.

Name your functions with a verb in mind. Functions do things to something. What does this function to what. What does it start with? If it's a function that removes all negative numbers from an array and returns an array, this is a decent function name removeNegativeNumbersFromArray. It may seem silly and overly long, but there will be absolutely no confusion as to what it does and what it works on.

The third problem: you reading the same code in 6 months time. It will be hard enough to remember what you did even if you name it well. Well-named variables is a gift you give your future self. Name them well now, and they will serve you well later when you go to update the code or, hopefully, on a job interview when someone asks you how you did something amazing. The last thing you want to say is "uh, I don't know".

Take the time and name your variables well. Some guidelines:

Go bigger before you go succinct. Over time you can learn how you like to shorten those long names, but long names will be generally understandable.

Keep with a naming convention. Don't swing wildly between camelCase, PascalCase, kabob-case, or any other format you prefer. SCREAMING_SNAKE_CASE anyone? Stick with one type so you don't wonder which type you used for this particular variable when reusing it. And when you start a job, find out what convention they prefer and use that.

Add verbs to function names, not to variable names. Verbs convey a sense of doing, which is what functions are for!

Try to talk about what a variable will be used for more than the contents of it. If you have an array of ages of people, don't call it arrayOfNums even though that is very accurate. peopleAgeList will be not only better to give you a sense of what you should put in it, but also what you shouldn't put in it. And if you suddenly decide to store string ages, you don't have a variable that is wrongly named.

Got any more memes, job postings, or technologies you would like to see me cover? Leave me a comment below!



For those wondering about the platypus and "shaving hours off development time".  The platypus is probably "if I had to name this thing, what would I name it?  fuzzyDuckBeaverMammal? And the "shaving hours" is for those that don't use good variable names because it takes too long.  You shave hours off development time now and probably have to spend days of development time later.  It's an aspect of what we call "tech debt"

To view or add a comment, sign in

More articles by Daniel Paschal

  • curiouser and curiouser: a tale of css pseudo-elements

    A programming language is a set of rules. One of our jobs, as developers, is to understand those rules in order to make…

  • Google SEO: structured data

    SEO, or search-engine-optimization, for a long time was a game of figuring out what the search engines were looking for…

    2 Comments
  • CSS: font-display

    Custom Fonts on pages can be a cause of consternation when they don't load quickly. In the past, tricks usually…

  • css: font-face multi-source

    File this one under "that's cool, but probably not that useful" When specifying a font-face, you would normally specify…

  • More physical products meeting end of life due to support changes

    There have been a recent spate of product brickings that you should be familiar with. In this day and age, it is quite…

    1 Comment
  • Charter/Spectrum's security shutdown hints of interoperability problems to come

    It's not uncommon for companies, as they do mergers or shut down or simply determine a product isn't performing well…

  • Git 2.25.0 has landed

    Release notes here Some love for sparse-checkout (checking out a more skeleton version of a project) git log formatting…

  • My first experiences with AWS Lambda: a guide to trying node.js lambda

    Prologue I am first and foremost an monolithic person. I love starting up my own full servers.

  • History of Javascript

    Mocha, I mean Livescript, I mean Javascript, I mean ECMAscript has been around in some form since 1991. It has a very…

    2 Comments
  • nth-child/nth-last-child tricks

    CSS is rapidly on its way to being a very robust 'language'. There are many selectors that people are unaware of that…

    2 Comments

Others also viewed

Explore content categories