Re: best practice for variable declaration
From: Alvin Bruney [MVP] (vapor)
Date: 04/08/04
- Next message: Keith R: "Re: Com Office Objects"
- Previous message: Uri Dor: "Re: XPath"
- In reply to: Jon Skeet [C# MVP]: "Re: best practice for variable declaration"
- Next in thread: Jon Skeet [C# MVP]: "Re: best practice for variable declaration"
- Reply: Jon Skeet [C# MVP]: "Re: best practice for variable declaration"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 8 Apr 2004 07:25:51 -0500
Consider a variable, a simple type for now, declared up top like so
{
variable here;
//processing done here
//if block with a return statement
//variable used here
}
In the above scenario, the space allocated for variable here is wasted if
the if block executes. It has to be discarded without being used. It would
have been more economical to declare variable here after the if block. For a
simple type allocated on the stack, who cares? This is the trivial case. For
a database connection declared on the heap in a web environment, this is an
entirely different story. As an example, SQLConnection cn = new
SQLConnection(connstring) substituted for variable here actually flags a
connection from the pool as occupied even though it may not necessarily be.
This sort of pattern may lead to resource contention on a website
experiencing heavy load - amazon.com for example. In fact, if the connection
were to be opened right after definition which arguably would most likely be
the case, this could quite certainly lead to a memory leak in the event that
either the if block executes and returns without closing the connection or
an exception is thrown at some point after opening the connection or the
//processing done here block of code takes a relatively long time to
complete. The resource wastefullness of this approach is compounded for more
complex types and custom objects which are very expensive to create such as
in custom types which require object pooling.
-- Regards, Alvin Bruney [ASP.NET MVP] Got tidbits? Get it here... http://tinyurl.com/27cok "Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message news:MPG.1adf0aea2165d59998a618@msnews.microsoft.com... > <"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote: > > I disagree here. This school of thought is Delphi driven which in turn came > > from the pascal world. The horrible abuse of this method is well documented > > and I won't go into it here. I'll just say that it is more efficient to > > declare variables as close to their use as possible except when conditions > > force other behavior. Declaring variables at the top of a routine is > > wasteful of resources and pushes uncomfortably close into *global variable* > > approaches. > > I don't believe it's actually wasteful of resources at all - could you > give an example of what you mean? > > I certainly believe it makes the code harder to understand (and write > in the first place), but I don't believe it has any impact on the > actual resource usage. > > -- > Jon Skeet - <skeet@pobox.com> > http://www.pobox.com/~skeet > If replying to the group, please do not mail me too
- Next message: Keith R: "Re: Com Office Objects"
- Previous message: Uri Dor: "Re: XPath"
- In reply to: Jon Skeet [C# MVP]: "Re: best practice for variable declaration"
- Next in thread: Jon Skeet [C# MVP]: "Re: best practice for variable declaration"
- Reply: Jon Skeet [C# MVP]: "Re: best practice for variable declaration"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|