[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: programing styles



Mike is correct.

As an addendum to this topic, I would suggest the use of the Iterator class 
for your for loop rather than the traditional iterate by count as in your 
examples.  This provides the benefit of using the collection interfaces and 
thus allows you to more easily interchange the type of collection you are 
dealing with.  Something like this:


private void myproc(Vector vect){
         MyObject obj = null;
         int y= 0;

         for (Iterator it = vect.iterator(); it.hasNext;) {
                 obj = (MyObject) it.next();
                 y = obj.getY();
         }
}


Matthew

At 03:13 PM 11/1/2002 -0500, Mike Millson wrote:
>Isn't #2 better for garbage collection, since you are not creating and
>destroying an object on each iteration of the loop? My understanding is that
>the number of objects you create directly impacts speeds. The less objects,
>the less garbage collection, and the faster the application. Given that, I
>would think #2 is much more efficient and the style to use.
>
>Mike
>
>-----Original Message-----
>From: Magesh Umasankar [mailto:umagesh@apache.org]
>Sent: Friday, November 01, 2002 2:42 PM
>To: ajug-members@ajug.org
>Subject: Re: programing styles
>
>
>I would say the first one is - keep variable
>declarations as local as possible - better
>for garbage collection also, in most cases.
>
>In your case, you gain nothing by declaring
>them outside the for loop as you don't seem
>to be using them outside the loop.
>
>Cheers,
>Magesh
>
>**************************************************
>*  The surest sign that intelligent life exists  *
>*  elsewhere in the universe is the fact that it *
>*  has never tried to contact us.                *
>**************************************************
>----- Original Message -----
>From: "Gudavalli, Manidhar" <manidhar.gudavalli@eds.com>
>To: <ajug-members@ajug.org>
>Sent: Friday, November 01, 2002 2:34 PM
>Subject: programing styles
>
>
> >
> >
> >
> > HI
> >
> > Which  style of coding is best ?
> >
> > [1]
> > private void myproc(Vector vect){
> > for (i=1; i < vect.size() ; i++){
> >
> > MyObject obj = (MyObject) vect.elementAt(i);
> >   int y = obj.getY();
> > }
> > }
> >
> > or
> > [2]
> > private void myproc(Vector vect){
> > MyObject obj = null;
> > int y= 0;
> >
> > for (i=1; i < vect.size() ; i++){
> >
> >   obj = (MyObject) vect.elementAt(i);
> >     y = obj.getY();
> > }
> > }
> >