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
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
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.
It is a common practice to declare variables near first use.
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.
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?
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.
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.At my university I was taught last year that variable declaration and initiation should be done at the top for readability and overview.
I have no idea what you mean by "overview".
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.
In C declaring the variables at the beginning of a scope was required until the advent of the C99 standard.At my university I was taught last year that variable declaration and initiation should be done at the top
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
Writing short functions that do one thing and do it well is good practice, so that's a Good Thing taught by your instructors.Originally Posted by Kirderf
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.Originally Posted by Kirderf
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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