Gabriel Sechan wrote:
----------------------------------------
Date: Sun, 6 Jan 2008 17:29:12 -0800
From: [EMAIL PROTECTED]
To: kplug-lpsg@kernel-panic.org
Subject: Re: Introducing Cobra
On Sun, Jan 06, 2008 at 05:11:01PM -0800, SJS wrote:
Then again, putting all the variable declarations at the top of the
method/function gives you much of what you want.
function name( args ... ) {
LongArduousType foo;
. . .
foo = new LongArduousType(...);
This is considered bad style in most modern languages. In fact, it is
forbidden in some. The idea isn't to move the initialization up to the
top, but to move the variable declaration down to where it is first
assigned. You don't ever declare variables without giving them their
initial value.
It is? I've never heard that. In fact, I frequently hear (and agree with) the
exact opposite- put every declaration at the top so that you can easily find
the declarations of all variables. As a bonus, you can read the variables used
and their types at the begining. Typically, if I know what you're going to be
using and the name of the function, I can guess how you're going to do
something before you do it. It makes the code far more readable. The only
real exception is index variables, like loop counters.
This is honestly the first time I've *ever* heard of it being considered bad
style.
It is considered bad style in C++ because of the semantics of stack
allocation.
Declaring everything at the top of a function means that everything gets
allocated on the stack immediately upon function entry and is held until
function exit.
By declaring variables near first use, you don't allocate/construct the
object until needed and the object can go out of scope and release its
memory sooner (at the end of its block rather than at the end of the
function).
Of course, modern languages running inside VM's with garbage collection
don't have to care so much. They can delay the allocation until right
before use and they can deallocate as soon as the last reference
disappears. Whether they *do* this is a different discussion, however.
-a
--
KPLUG-LPSG@kernel-panic.org
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg