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

This is a discussion on Question on understandability of code: How long can a variable name be? within the C++ Programming forums, part of the General Programming Boards category; I'm in a C++ class that has put emphasis on understandability (the ability for other users previously unfamiliar with your ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    8

    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!
    Last edited by alizzle; 09-28-2010 at 07:44 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,266
    Quote 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.

    Quote 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.

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 01:22 PM
  2. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21