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.
I like to place variable declerations at the beginning of the innermost enclosing block.
i.e.
Class variables at top of class
Method variavles at top of method
Block variables at top of block
For loop variables in for decleration
Stop doing it, and you'll realise how useless that practice is. You waste a lot of time maintaining such code.
It was a useful practice in C where you needed to make sure you new how a variable had been initialized, but in C# it's a useless practice, and adds confusion and mess.
I only declare them at the top of the method/scope if they are used outside of the scope that they are initialized in. Otherwise I declare and use them as they are required.
My way is the best way! ;)
John.
I just use globals, and prefix all my variables with g_ to remind me they're global. EXCEPT: When I use classes, I think destructor's are for careless, lazy, fat german people, so I put a destructor in all my classes that goes
int* t = NULL;
*t = 37;
Then if a destructor is inadvertantly called my program crashes (if it isn't trapped in an infinte loop already or run out of memory)
Yeah, global variables are too easy for me. I like more of a challenge so I just keep all my data in a file. I just create heaps of files and prefix g_ to remind me of you g, and write them into c:\windows\system32 (so that dudes don't find them, coz it's really confusing in there and no-one can ever find files if you put them in there). The best thing is that my apps can all totally recover from any crash, because all the state is always on disk. If I run out of disk I usually just look for useless stuff on the disk to delelte, like stuff in "c:\program files\micro*"..
I usually just store each variable in it's own HTML backend like that. I always keep my counters in there too, so I use like c:\windows\system32\g_i.html for loops. Sometimes when I'm doing really complicated stuff I need an inner variable, so I don't get confused I usually store my second counter (g_j.html) in binary so I don't forget about it. But most people aren't as smart as me, so they probably wouldn't understand if I tried to explain it to them.. I'm not sure if I use big endian or little endian, I always get them ones the wrong way around!
I have code in my program that detects accidental infintie loops so after 1,000,000 loops I always just reboot the computer. I keep that variable in c:\windows\system32\g_check_loop.html.
When I say infinite loop .. i don't mean to suggest that I use the loop construct. I just get the code I went to use, and cut and paste. and if i need a loop counter, i just hard code a constant in each cut and paste. inifinite loops are just when I accidently put a wrong goto in there
my clients love it when I give them a 40MB C executable. i usually rename a few windows dll's and stick it in the install package too even though my app is just one exe to make it look more complicated
"i usually rename a few windows dll's and stick it in the install package too even though my app is just one exe to make it look more complicated"
Wow! That's a great idea. Thanks G!
My clients are going to be super impressed next time I ship a version. (could take a while, I've been trying to use a file to share data between apps (a counter, I have like five different apps working on the same loop so I can get like 5 times as much work done, and they each need to read and update the counter in the HTML file), but I keep getting errors when I try to open it from each app.. I think windows must have bugs in it or something)
John.