Re: best practice for variable declaration

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Alvin Bruney [MVP] (vapor)
Date: 04/08/04


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


Relevant Pages

  • why Youssefs evident commerce compels, Ahmed seizes as for static, tart timetables
    ... If the illegal experiences can return specially, ... civil chip may declare more tents. ... The reporting in connection with the ... I am necessarily surrounding, so I ship you. ...
    (sci.crypt)
  • Re: SORN petition
    ... you may sell it and not declare the sale ... forgetting to stick to the speed limit should not be a fine either? ... thats not a lot of resource. ...
    (uk.rec.motorcycles.classic)
  • Re: Excel VBA Query Using Access Database Hangs on 2nd Execute
    ... > Is there a need to declare the connection string as Const. ... the file was saved on the server and Excel closed. ... I have two computers side by side, both connected to the server. ...
    (microsoft.public.excel.programming)
  • Re: What is best SQLCeConnection strategy?
    ... To declare a SQLCeConnection variable as a Static member of ... or should I just declare it as a Public ... member of the above mentioned class containing Connection ... connection once or open before a execute an query and close after it? ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: VB6 SQL 2005
    ... Dim cn As Object 'declare connection ... Dim rs As Object 'declare recordset ... Dim strSQL As String 'declare sql command ...
    (comp.lang.basic.visual.misc)