Declare an object inside or outside a loop?

Yuval Adam :

Is there any performance penalty for the following code snippet?

for (int i=0; i<someValue; i++)
{
    Object o = someList.get(i);
    o.doSomething;
}

Or does this code actually make more sense?

Object o;
for (int i=0; i<someValue; i++)
{
    o = someList.get(i);
    o.doSomething;
}

If in byte code these two are totally equivalent then obviously the first method looks better in terms of style, but I want to make sure this is the case.

Dave Markle :

In today's compilers, no. I declare objects in the smallest scope I can, because it's a lot more readable for the next guy.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JavaScript variables declare outside or inside loop?

Is it better to declare a local inside or outside a loop?

C++: Declare a vector inside or outside of a loop

Should I declare a variable inside a function or outside?

How to declare datarow outside foreach loop?

Loop Inside vs Outside Function

Declaring variables inside or outside of a loop

Inside and outside of for-loop in JavaScript

Complexity of if statement inside or outside for loop

python dictionary inside/outside for loop

drawImage() works outside of loop, but not inside?

Node.js Handlebars {{object}} does not render inside {{each}} loop, but renders outside of loop (and other {{object_names}} render within the loop)

semicolon inside and outside Promise object

Variables inside and outside of object methods

Python - variable inside for loop disappears outside of loop

Is it faster to declare variables for loop criteria outside of the loop condition?

Why declare a va_list inside a for loop?

Why is it okay to declare a STRUCT inside a nested loop?

Declare class attributes inside a for loop in python

Should we declare methods inside or outside the constructor in a class definition in JavaScript?

Where to declare auxiliar functions for a React functional component. Inside or outside?

Why declare/initialize a same property both inside and outside of constructor?

Creating an Object inside a loop

Declare object inside the environment.ts file

cannot declare objects inside an object of a Class in javascript

Declare Global Object Inside Function C++

Angular - declare and initialize array inside an object

Multiple declarations same of variables inside and outside for loop

For in loop inside or outside UIView.animate block?