Separating Declaration and Definition?

Started by
13 comments, last by Zahlman 15 years, 5 months ago
Quote:Original post by Antheus
Note that Pascal and C were designed around 1978 with strong influence from Algol.

Note that C was in production use by 1970. It was used to port the Unix operating system to new hardware that year.

Also, note that FORTRAN, in first production use in 1952, allowed variable to be declared at first use anywhere in the body of code, just like with C++ (FORTRAN did not, in fact, allow you to declare variables separately from first use until, what, Fortran 77?). The enforced placement at the top of a function definition was never due to the restructions of running on slow processors with little memory. It was purely due to theoretical reasons about how to write programs better.

As Antheus said, it was the heritage of Algol. Algol was designed by a committee of academics who knew far more about how to write good software than the folks on the ground who actually wrote and maintauned that stuff. Those folks mostly used COBOL wherein you declared all your variables separately in the Data Division and used them later in the Procedure Division. We all know Algol caught on like wildfire and replaced COBOL.

Stephen M. Webb
Professional Free Software Developer

Advertisement
Quote:
Quote:In class driven languages like C++/C#, the cost of initializing an object of a class can be expensive, if the class describes very complex objects


For C++ in particular, you don't pay for what you don't use. So feel free to declare as many stack allocated variables as you wish.
Even more, C++ is free to not allocate structure it doesn't need. Compiler is perfectly capable of analyzing what causes side-effects and what doesn't. As such, the cost of unused objects is zero and trivial objects may be only partially constructed. Example:
void foo(const Message & m) {  print(m.c);};struct Message { int a, b, c, d; };...Message msg;msg.a = 10;msg.b = 20;msg.c = 30;msg.d = 40;foo(msg);// Compiles intoprint(30);// compiler is clever enough to know there are no side-effects
I think he means code like this, not unused variables:
class big_class{...};function(int u){    big_class a, b, c;    a = c;    if(u) return;    c = b;    if(u == 42) return;    b = a;}
My complier does create the classes where they are declared.

Quote:Original post by Bregma
Also, note that FORTRAN, in first production use in 1952, allowed variable to be declared at first use anywhere in the body of code, just like with C++ (FORTRAN did not, in fact, allow you to declare variables separately from first use until, what, Fortran 77?).

Unfortunately, that's not an apples to apples comparison. Fortran didn't even have stack allocation in it's original incarnations; every variable was effectively a global. Fortran variables also encoded type in the first letter of the identifiers and restricted identifier length to, IIRC, 4 characters originally, then up to 6 characters in Fortran 66, and didn't even have case (every character was uppercase, leading to great confusion between O and 0 in Fortran programs). When you get to Fortran 77, you finally got to have variables that didn't obey the first character type rule, however, in order to declare a variable that didn't you were required to declare them separate from first use.
You can reuse variables or not, because either way the compiler likely doesn't care. For a compiler that uses Static Single Assignment, each definition (write) of a variable will be seen as a new variable anyway, which simplifies calculating live ranges. You're not harming or helping the compiler, although you may be confusing yourself in the long run.
Quote:Original post by Falling Sky
Why dont people follow that style of separating the two any more?


Because they don't have to.

Quote:Why did people originally separate them?


Because they had to.

This topic is closed to new replies.

Advertisement