Jump to content
• Advertisement

Separating Declaration and Definition?

This topic is 3619 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering what was everyones opinion and also the original purpose, for programming language to make you separate your data from your code? I know Ada does, I believe Pascal does, and if I remember correctly it was also common style in C to put all your variables and such at the top of your function. Now I always see logic code mixed with variable declarations and such. Why dont people follow that style of separating the two any more? Why did people originally separate them?

Share this post

Share on other sites
Advertisement
Basically, because compilers written that way were smaller and less complicated. This was a big deal back when memory was measured in kilobytes rather than gigabytes.

Share this post

Share on other sites
Quote:
 Original post by Falling SkyWhy dont people follow that style of separating the two any more?

Because the new style is both more readable and sometimes faster, for example:
Foo x;x = Foo(1, 2, 3);// ...Foo y(4, 5, 6);

In this code, x is default initialized and then assigned to, which is less efficient than y which is initialized directly with the values.

Share this post

Share on other sites
Quote:
 Original post by Falling SkyNow I always see logic code mixed with variable declarations and such. Why don't people follow that style of separating the two any more? Why did people originally separate them?
When I first started using C, that was the way it had to be done. You declared your variables at the top of the function, and then wrote the logic for them after that.

When C++ allowed you to mix them, C compilers added the functionality. I think this may have come in C99.

I usually declare my big/major/driving variables at the top of a function, and I only declare minor or temp variables as needed, like something for a loop, which I will declare right in the for statement, unless I have a reason not to.

Share this post

Share on other sites
When im writing something from scratch I will put all variables at the top and initialise on declaration, until that algo is written. After I've tested it I may move the variables to the scope where they are used, or I may not, depending on which I think will be more readable if I come back in a month. But I almost always initialise on declaration, i.e. int x = 0;

Share this post

Share on other sites
It's because if you have all your variable declarations at the top of a scope, you can compile in one pass. That's really good for the computers that C initially ran on (ie, slow with little memory). Nowadays, it's not such a big problem.

Share this post

Share on other sites
BTW this has nothing to do with separating declaration and definition (that's the header file stuff). This is separating local variable declarations (like int i;) and statements (like i = 5;).

Share this post

Share on other sites
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. Hence delaying such definitions in a function until they are needed, may improve the flow of your program say, if an error occurs in a function and thus needs to return immediately, thus making all that code to set up and initialize all those objects at the start of the function, a complete and utter waste of time.

Of course, this isn't too much of an issue for simple data types, but I have switched from defining all my objects and variables at the start of my functions, to defining them only when they are needed.

If I want a function to remain particularly tight, I may even use function arguments, passed by value and who have served their purpose, to hold new values in the function, thus totally bypassing the need to define yet more local objects.

Share this post

Share on other sites
Quote:
 Original post by CodeStormIf I want a function to remain particularly tight, I may even use function arguments, passed by value and who have served their purpose, to hold new values in the function, thus totally bypassing the need to define yet more local objects.

As a compiler designer, I have three words for you: Stop. Torturing. Optimizers.

Your average optimizer can and will notice that you don't use a value anymore. It's textbook program analysis and has been for two decades now. If you reuse a variable for another purpose, you defeat the optimizer, forcing the compiler to keep around the original variable until you assign to it again because otherwise something might break.

Share this post

Share on other sites
Note that Pascal and C were designed around 1978 with strong influence from Algol.

The basic idea behind declaring everything upfront was that compiler could layout memory without knowing the algorithm itself, and then use those fixed locations within function.

C still has register keyword, which served two purposes. It told compiler to allocate that particular variable in register, and that all access to that variable should be done exactly as specified in code, since each access could have side effects. As such, it also performed the function of volatile (see Duff's device).

In Pascal, there's distinction between function and procedure. The reason for this is return value. In Pascal, result of function is a variable which exists throughout the entire function. This avoided parsing the function body to determine what and how it returns, if at all. While not strictly necessary, given the automatic int substitution, perhaps the intention was to define structure explicitly.

Nowadays, none of this is necessary. Current recommendations are to define variables at the point where they are needed. While it likely has little to no impact on compiler's optimization capabilities, it makes code clearer.

Those that believe that re-using variables is smart have likely never worked on Pascal or Delphi projects, or were lucky enough not to notice it. There's nothing more horrible than having i being for loop index, accumulator elsewhere and integer representation of a pointer storing the callback, all in same function. It's horrible, just don't do it.

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

In Java, unused variables are not compiled into class file. In addition, variables that are never read are reported as warning or error. Objects need to be initialized with new, so there is no accidental construction, unless user performs it. And if they do, compiler will report (variable is assigned but never read). So that is non-issue.

I never looked into CLR details, but aside from stack allocated types, I would presume it's similar to JVM.

Long story short - don't re-use auto-allocated or stack-based variables. They really are a non-issue for any reasonable compiler.

Share this post

Share on other sites

• Advertisement
• Advertisement

• Popular Contributors

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 11
• 10
• 13
• 103
• 11
• Forum Statistics

• Total Topics
632976
• Total Posts
3009672
• Who's Online (See full list)

There are no registered users currently online

×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!