Thread: Variable declaration - what is considered good practice?

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    21

    Variable declaration - what is considered good practice?

    Hello and merry christmas!

    Is it considered good programming practice to follow the ANSI standard and declare all variables in the beginning of a function? Except for perhaps a for-loop?

    Regards

  2. #2
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    It is good practice to restrict the scope of your variables to the minimum needed. A loop counter is only needed in a loop so declare it at the top of a loop. A variable used in a whole function, declare at the top of the function. A variable only used in a small block, declare it at the top of the block. It's always good practice to follow the standard. That's the only way to defined portable behaviour.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    It is a common practice to declare variables near first use.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    The standard tells you nothing about style. "Following" it for style is meaningless.

    The best modern practice is NOT to declare your variables at the top of a block, but instead to declare them as close to their first use as possible.

  5. #5
    Registered User
    Join Date
    Sep 2016
    Posts
    21
    Okey. At my university I was taught last year that variable declaration and initiation should be done at the top for readability and overview. Perhaps people from other continents doesn't agree on that? Scope variables should however of course be declared where needed to. Why isn't it considered good to have them at the top?

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by Kirderf View Post
    Perhaps people from other continents don't agree on [the placement of variable declarations]?
    No. But people from different eras may not agree.

    It depends on whether you're using the old-fashioned C89 standard where variables MUST be declared at the top of a block, or the newer C99 standard (or greater, or any version of C++) which removes that pointless restriction.

    Your university is simply behind the times, a common failing.

    At my university I was taught last year that variable declaration and initiation should be done at the top for readability and overview.
    Why should I have to look at the beginning of a block just to see what type a variable is? There's no advantage to putting them there. It makes it less readable, not more so.

    I have no idea what you mean by "overview".

  7. #7
    Registered User
    Join Date
    Sep 2016
    Posts
    21
    No, it isn't completely because of C89. We are still using variable declaration when making for-loops. When I say overwiew, I mean that you see all the variables that have their scope for the whole function and can see what they are initiated to.

    EDIT: We are also not allowed to write long functions, so looking at the top of a function doesn't require any scrolling.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,431
    At my university I was taught last year that variable declaration and initiation should be done at the top
    In C declaring the variables at the beginning of a scope was required until the advent of the C99 standard.

    However most modern compilers, including one major compiler that doesn't truly support the current standards, support declaring variables closer to the first use along with other modern C "features" such as single line comments "//" and today it is considered a better practice to place the variables closer to first use and avoiding large unwieldy functions.

    Also note that many universities are still teaching old or depreciated methods because of rigid rules governing the modification of a course and the course materials like textbooks.

    Jim

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,070
    Quote Originally Posted by Kirderf
    We are also not allowed to write long functions, so looking at the top of a function doesn't require any scrolling.
    Writing short functions that do one thing and do it well is good practice, so that's a Good Thing taught by your instructors.

    Quote Originally Posted by Kirderf
    When I say overwiew, I mean that you see all the variables that have their scope for the whole function and can see what they are initiated to.
    The rationale behind declaring variables near first use is that that is the point, or just before the point, the variable can be set to a useful value. This eliminates the possibility of the variable being incorrectly or uselessly accessed before being set to a useful value, or even the need to set some kind of safe otherwise-not-useful default value, e.g., to set a file pointer to be a null pointer, only to set it to the return value of fopen later.

    If your function is sufficiently short, then you don't need an overview of what variables are used precisely because it is sufficiently short that one can see that at a glance.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2016
    Posts
    21
    Okay. You seem to have a lot of experience. What would you reaction be if you got to work on code where someone had written all the declarations at the top in:
    !. A long function. (have to scroll)
    2. Just fits on the whole screen you use.
    3. Short. (Less than whole screen, more like half the screen)

    I really like others opinions.

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,687
    It depends a lot on personal preference also. Some people still use C89 because of technical limitations so that would restrict them to the original practice of variable declaration. Others follow rules set by employers and colleges. Whether or not they decide to change these lessons to suit their own personal code is again a matter of personal taste. There is np "wrong" way to declare variables as far as the compiler is concerned - if the given syntax matches the used standard then that's all it cares about. Another thing that can tip which style used is commenting. Some people like to place a comment after each variable declaration to explain it's usage - others may write a longer comment before a function explaining what it does and why it returns said value.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-30-2014, 04:55 PM
  2. what's good naming practice?
    By happyclown in forum C Programming
    Replies: 2
    Last Post: 02-08-2009, 11:53 AM
  3. Good Practice With Include Files
    By CrazyNorman in forum C Programming
    Replies: 3
    Last Post: 07-28-2007, 02:23 AM
  4. Good C programming practice
    By Juganoo in forum C Programming
    Replies: 4
    Last Post: 12-21-2002, 03:20 AM
  5. good programming practice
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 02-26-2002, 08:09 AM

Tags for this Thread