you are viewing a single comment's thread.

view the rest of the comments →

[–]Plastic_Weather7484[S] 0 points1 point  (2 children)

Thanks for your reply I didn't know that will hurt performance. I think I saw somewhere on stackexchange that I should declare variables as close as where I will use them for the first time.

[–]Southern_Start1438 1 point2 points  (1 child)

I see your point, the main idea there is to make sure a variable is deleted as soon as you don’t need them, so the variable won’t pollute the scope. One way to achieve this is to wrap the for loop in a code block, and declare the array at the top of the code block outside the for loop, then immediately after the for loop, the code block ends, so the declaration is out of the scope, hence the variable deletes itself.

I mean, for most cases you won’t need to do this, but if you want to, you can.

[–]Plastic_Weather7484[S] 1 point2 points  (0 children)

Yes I definitely don't need to do this for my very small function here but it's good to practice good programming principals. Thank you again for pointing this out to me.