blog.jj5.net (2003 to 2005)

I'm sorry Shaun, but you're wrong..

Mon Jul 5 11:17:00 UTC+1000 2004

Categories:

I'm just working on some code I wrote about a year ago.

At the time that I was writing the code, I had a good mate around who I respect as a very capable programmer.

I was showing him C# and code that I was working on. After hanging around for a while we went out, we were off to the pub or a movie or something, and while in the car he commented that he thought it was sloppy that I wasn't declaring all my variables up the top of my method.

I was torn.

I'd been doing that for years, and had only recently abandoned the practice in C#.

I made some feeble comment that “you didn't need to do that anymore, besides, we have block level scoping” and he just grunted as in “yeah, whatever you say John... it's dodgy and you know it”..

So, I felt dirty. Cheap. Sleazy.

When I got home I started declaring all my variables at the top of my scope.

Today, I'm looking back on the code that I wrote then, with all my variables nicely listed at the top of my block level scope, all nicely tabbed with the type, name and initial value.

It's crap! It's a maintenance nightmare.

Firstly, all the tabbing became messed up after some refactoring took place and some types and names changed with 'find/replace'.

Secondly, variables I don't use in some circumstances are declared and initialized needlessly.

Thirdly, it's difficult to read.

Basically, declaring your variables at the top of your method (or even your block level scope) sux.

Declaring them inline with your code is the way to go. It's easier then to pull bits of your function out and stick them in another function, your compiler can help you to make sure your not using a variable prior to its initialization, etc.

So, Shaun, I listened, I changed my ways, but... you're wrong.

Declaring at the top of the method sux. I'm not doing that anymore, no matter how guilty you initially made me feel! ;)

John.


Copyright © 2003-2005 John Elliot