# Thread: Question on understandability of code: How long can a variable name be?

1. ## Question on understandability of code: How long can a variable name be?

I'm in a C++ class that has put emphasis on understandability (the ability for other users previously unfamiliar with your code to be able to decipher and edit your code).

Additionally, we were instructed that long variable names can be confusing and should be avoided as well.

My question is, are my variable names too long??

I've got to name three separate variables that are all years.
They are all in different centuries, thus I have them named:
Code:
`int seventeenth_century_year, eighteenth_century_year, nineteenth_century_year;`
Grant it, I could use the "downhill effect" of C++ coding to my advantage, and just
make the int named year and re-establish its value further down the code as needed because nowhere in the code would 2 different year values needed to be calculated together. e.g.
Code:
```int year, var2

year = 1700

var2 = sqrt(year)
//outputs
//outputs
//outputs

year = 1800
var2 = sqrt(year)
//outputs
//outputs
//outputs

year = 1900
var2 = sqrt(year)
//outputs
//outputs
//outputs```
Note that there is no user input in this program, so the variable value cannot be manipulated that way.

(This is a school assignment for an intro C++ class, and the overall goal is to improve our development of algorithms pre-coding.... Not so much to improve our ability to code)

Thanks in advance!

2. Originally Posted by alizzle
Additionally, we were instructed that long variable names can be confusing and should be avoided as well.
Maybe not so much confusing as tedious to type and read.

Originally Posted by alizzle
My question is, are my variable names too long??

I've got to name three separate variables that are all years.
They are all in different centuries, thus I have them named:
Code:
`int seventeenth_century_year, eighteenth_century_year, nineteenth_century_year;`
I would consider them a little too long.

Originally Posted by alizzle
make the int named year and re-establish its value further down the code as needed because nowhere in the code would 2 different year values needed to be calculated together.
That sounds okay. You might just move each block of code to a separate function, or if feasible to a single function that can be reused.

If you did want to use different variable names, then declare your variables near first use instead of declaring them at one go at the top.

By the way, 1700 is the last year of the 17th century and 1701 is the first year of the 18th century.

3. But if you're using a code editor with auto completion features, the "tedious to type" argument is less important.

Though if they're too long, they can become tedious to read as well

Popular pages Recent additions