Declaring variables inside-versus-outside loops

Monday, 10 May 2010 11:39 GMT

IN .NET, there is no difference between declaring variables inside loops and declaring variables outside loops. Here is the proof.

Here are two programs inside.cs and outside.cs, appropriately named to reflect the two ways respectively.

inside.cs versus outside.cs

And, here is the ILASM obtained by disassembling inside.exe and outside.exe.

ildasm output of inside.exe and outside.exe

As you can see, the two are the same.

So, please leave those variables inside loops when they are not needed outside. Having the variables closer to their point of use makes the code easier to read.

Eddy.

I know I said I would not write about coding, but this one has been bugging me for some time — a collaborator keeps changing my code to pull the variables out of loops, arguing that this will save memory and improve performance.

Add a comment

Why so serious?

Saturday, 24 April 2010 20:21 GMT

I STARTED with a blog dedicated to programming some time in 2003, dabbled with other blogging projects, and now appear to have settled with the present incarnation. Looking back, it is strange to see how my blogging preference has shifted so much.

Although I still do a lot of programming, I have given up on writing about it. I feel that programmers, or those who write about the profession, are being too liberal about throwing acronyms, patterns, and what-nots around, and I do not want to be part of that.

Just yesterday, I came across a blog post about “a new Locator pattern”. Well, here was me thinking that the purpose of a pattern was to describe a distinct and recognised solution to a common programming problem. Let’s get this straight, you have ONE Locator pattern with VARIATIONS in implementation. Someone claiming “a new Locator pattern” is missing the point, surely. Imagine this, in the middle of a technical discussion, you suggest that a developer uses the Locator pattern. “Err, which one already?” Yep, that’s how ridiculous pattern abuse has become, all thanks to self-proclaimed programmer-blogger-gods!

And, this is why I prefer to post pictures. Like this one.

priscilla*

Eddy.

1 comment


« NewerOlder »

Powered by WordPress and Eddy Young.

DISCLAIMER: This site is supported by advertising. As a result, cookies may be installed by advertisers in order to track usage and trends. If you do not want this, please disable cookies for this site.