Fog Creek Software
g
Discussion Board




Variable declaration location preference/rule?

In C, I know it was once required that all non-global declarations appear at the beginning of the enclosing block, meaning either at the start of a function or at the start of a control structure.

I prefer to declare and define my variables as needed. This is supported by GCC with or without the -ansi option.

My question is, how widespread is the support for this practice? Is support still rather new? Do people more than anything else use the older style because they prefer it or because the declare/define as you go syntax isn't well-supported yet?

If I want to write a portable app that will compile with any modern ANSI compiler, what should I do?

Vans Davis
Thursday, February 26, 2004

Depends on your target compiler. The C89 standard is still the only one recognized by most compilers, and it clearly says that you can only declare variables at the beginning of a block (anywhere after an opening curly brace).

The current standard is C99 and lets you declare variables anywhere but few compilers support it, so I wouldn't count on it.

However, unless you're doing embedded systems the compiler is likely a C++ compiler anyway which would support such declarations either as a non-standard enhancement, or simply by compiling in C++ mode.

Chris Nahr
Friday, February 27, 2004

> If I want to write a portable app that will compile with any modern ANSI compiler, what should I do?

You answered your own question

If compatible with standard C is the goal, follow the standard.

I don't see it as such a hardship to declare variables at the start of the block.  If that is your worst problem, I envy you!

S. Tanna
Friday, February 27, 2004

It seems ver logical to me to declare the variables very close to where you are using them. If your compiler(s) and platform(s) support them, please do so, it will make the maintenance of your sourcecode much simpler.

Mark Tetrode
Friday, February 27, 2004

It will will it?

In five years time when someone else comes to maintain the code are they going to know where all these squirrelly definitions of local variables are?

Simon Lucy
Friday, February 27, 2004

I used to declare all the variables at the start of a function but I switched recently to declaring them closer to where I use them.

This is what is suggested in the book "Code Complete" which is what made me change it.

Simon: When you're maintaining code with "squirrelly" defs, what do you think makes it harder?  Personally I think it's easier because you don't have to hunt through a bunch of vars that have nothing to do with the piece of code you're concerned with.

Wayne
Friday, February 27, 2004

if you create 500-line functions then declare it closer where you use.

if you create a 5-line one, it will be in the beginning in any case


Saturday, February 28, 2004

> In five years time when someone else comes to maintain the code are they going to know where all these squirrelly definitions of local variables are?

They're easier to find in the middle of the function than at the top: partly because they're closer to where they're used, partly because syntax highlighting makes the type declaration stand out(1). They're easier to maintain when you're refactoring (cutting and pasting) code. Most importantly, with this convention you virtually never need to declare a variable without simultaneously initializing it: this avoids "uninitialized variable" errors, *and* makes it easier to understand the declaration when you see it (because you see not only the variable's name and type, but also the value that is initially assigned to it).

Not so 'squirrelly': I'd say it's widely accepted as a good practice, in C++ at least.


(1) For example, in the following fragment, my editor highlights the "int" in blue:

blah();
x = 7;
int y = 16;
more_blah(y);

Christopher Wells
Saturday, February 28, 2004

*  Recent Topics

*  Fog Creek Home