In production code, or code that might be delivered to a customer I try and adopt an enlightened pro developer style of coding. I.e. I only do stuff that I think adds value.
In personal project coding I have several alternate styles. By habit I normally code in the style of Father Ted (sub style – Father Jack Hackett). Which dictates the first procedure is called ‘feck’ and the second is called ‘Arse’ (by LAW!). The traditional ‘Hello World’ becomes (somewhat obviously) ‘Feck Off’. Yes/No dialogs become ‘Ah go on, go on now’
My most recent project (indexing the codematic site) I coded in the style of Bodie and Namond (inspired by his explanation of the game of chess) from the Wire. So the first routine is ‘FindDemBitches’ quickly followed by ‘DeleteDemMuthas’. Which contained the immortal line
Dim DatLiddleBitch as Range
What coding styles so you use? And do you do an edit replace as soon as that ‘personal code’ finds its way into production? Or perhaps before??
(I remember one time a user contacting my to ask why they had just had a error message that said ‘Guess again, donkey’.)
cheers
Simon
Monday, 9th November, 2009 at 1:00 pm |
I tend to go with whatever I was watching with the kids on nickleodian the night before – which means spongebob figures quite often – although I did go through a Dirty Harry phase
Monday, 9th November, 2009 at 1:48 pm |
There’s a small UK bank that still has
rngFilledInByTwats
in one of their SWAP data entry upload files
and a rogue clippy on the Finance Director’s Word install that is forever saying
“Ah, it looks like you’re typing your resignation, do you need help with that?”
Never quite did get around to replacing those before I left ;-)
Monday, 9th November, 2009 at 2:20 pm |
“So, did I loop 6 times or only 5?”
do while “punk”=”feels lucky”
Monday, 9th November, 2009 at 3:35 pm |
Almost every VBA procedure I post to newsgroups is named foo, and when I need a second it’s named bar. And as anyone who’s seen my code can attest, I’m an ardent advocate of conservation of keystrokes when it comes to variable names.
Monday, 9th November, 2009 at 4:05 pm |
I might be amused by variable names like that, but in three months when I revisit the code, I’m sure to be confused. Descriptive variable names are for the programmer, not the client.
Monday, 9th November, 2009 at 5:05 pm |
Desriptive variable names are one thing, but too often I’ve seen object references assigned to variables where the variable name was the object reference with the periods removed.
An example of my style: ur always means used range. Unless I’m using used ranges from multiple worksheets simultaneously, I see no reason to qualify it further. Same for sr meaning selected range. As for For loop counters, I see no reason to depart from using i, j, k, etc. If it was good enough for FORTRAN in the 1950s, it’s good enough today! Seriously, I actually find a(i, j) more readable than a(iRowCounter, iColCounter) [or the excruciating iColumnCounter].
Most of my udfs involve serious numerical programming or regular expression manipulations, and my comments usually reference pages in textbooks. Those texts usually use single letter array and index tokens, and I just follow the text.
I do tend to be a bit wordier with subs, but not by much.
Monday, 9th November, 2009 at 6:58 pm |
Something to consider is the possibility that ones’ sub/function/udf name could duplicate the name in use by some other piece of code.
I learned the hard way that “SortSheets” is not a really good name to use.
Monday, 9th November, 2009 at 7:25 pm |
Harlan -
That’s perfectly acceptable. If my sub only references one chart or onw range, I use cht and rng. For most looping I use i, j, k for loop counters (yeah, I remember fortran too). And for demos, sna(), foo(), and bar() are as good as anything else.
But descriptives like rngRawData, rngChartData, or rngCalcOutput are very useful without being excruciatingly verbose.
Monday, 9th November, 2009 at 9:15 pm |
Alistair – spongebob is a great idea, I have used ‘you are a gooby goober’ in a messagebox, but not thought of doing a fully themed project – next time.
Will – subverting clippy is exactly the right thing to do.
Jon – like I said not in production code.
Harlan – I use short names for loop counters too
Jim – good point, and further evidence that ‘IndexThatBitch’ is a good name as it’s unlikely to clash with other loaded code (unless Alistair was watching the wire whilst coding his index system)
Tuesday, 10th November, 2009 at 1:25 pm |
@Simon: “like I said not in production code.”
I don’t see the point in changing my coding habits just because I’m writing something for internal use. A huge amount of “production” code, that is, client work and especially commercial add-ins, started as work done for my own use.
By using the same style for my own code as for production code, I don’t need to write any code twice, to make sure I don’t offend anyone or to make sure that shortcuts I may have taken won’t backfire.
Thursday, 12th November, 2009 at 12:01 pm |
Saw this in one of the first access databases that i opened at a large bank
On Error GoTo WTFH
i.e. What the f… happened, lol
+ all the usual VBA cowboy stuff, Global variables etc etc etc
Never actually met the guy, just changed all WTFH in the code and recomplied
I my experience 90% + of VBA coders are cowboys
Its the main reason why C# is my main code direction these days
Just tired really
You know, you get a contract, sit down, sort it out and all around you are
a bunch of cowboys
Thursday, 12th November, 2009 at 12:08 pm |
yehaw Steve!
Thursday, 12th November, 2009 at 12:16 pm |
p.s
fast forward 3 years…
In my experience 90% + of C# coders are just cowboys
Its the main reason why ?? is my main code direction these days
lol
Thursday, 12th November, 2009 at 12:17 pm |
Hi Simon
;-)
cheers man
Thursday, 12th November, 2009 at 12:31 pm |
??
VB.net??
ha ha!
don’t tell me C++?
you know it makes sense, its too hard for all the numpties.
ITs not lisp is it?
c’mon Steve tell us what it is, email me if you don’t want to start a stampede
Friday, 13th November, 2009 at 2:19 pm |
no, I will just keep you hanging…
:)
actually C# all the way baby
the thing is u have to pick one and stick to it
otherwise you never get to know it in depth
for me:
1. SQL (various flavours) cause i work with databases
2. VBA (no VBScript, VB6, VB.NET) just pure VBA, stopped all other flavours
of VBx, just VBA and only for tactical solutions
3. C# for all the important stuff
Friday, 13th November, 2009 at 2:35 pm |
by the way…
all code i do these days in VBA is class based
the upfront set up time is greater but man does it make a BIG difference towards the end of a large build when deadlines are looming
except for the rare occasions of module based public functions for xl spreadsheet UDFs and access queries UDFs
after all a module in VBx is just like a static class in C#
;)
cheers
Saturday, 14th November, 2009 at 10:10 pm |
behave, no ons uses classes in VBA they are crap.
Sunday, 15th November, 2009 at 5:14 am |
yes, of course
its not a proper class based system
but then again VBA is half baked for just about everything
what i find tho is that it does satisfy one of the main points of classes
encapsulation
just makes coding easier, all i have to worry about is the entry points
to the class and everything else is contained within the class
it also makes the calling code much clearer
(i am talking about projects with 2+ thousand lines of code)
on a side note
I have been using .NET Reflector (Red Gate) a lot recently
Take Reflector and have a peek at the .NET framework like System…etc
Open sourced :)
Now take Reflector and point it at itself Reflector.exe, the source has been obfuscated with dotfuscator available in visual studio
The irony – i love it.
Cheers
Sunday, 15th November, 2009 at 10:47 am |
If you got rid of all those
dim x as myCrappyVBAToyClass
…
set x = new myCrappyVBAToyClass
x.initialiseProperly(ParamsThatShouldBePassableInNewStatement)
…
…
set x= nothing
and replaced with Usefulmodule.usefulMethod(…)
your 2k+ projects would probably only be a couple of hundred lines.
Do you charge by the line? ;-)
I see a lot of VS devs using classes in VBA, but they are so weak compared to C# personally I don’t bother. I switch off OO mentality and switch on functional/procedural mentality for VBA.
Are you using vs2010? C# 4.0?
I may go back to C# then as the Office story starts to make more sense.
cheers
Sunday, 15th November, 2009 at 1:14 pm |
Yes
VS 2010 Beta 2, C# 4.0, Framework 4.0
and VSTOooooooooooooooooooooooooo
Its a lot better now
The idea is to leverage my knowledge of the office object models
so office VBA -> VSTO
I won’t stop using classes in VBA
Sunday, 15th November, 2009 at 1:28 pm |
p.s
is it still called VSTO?? who cares
MS seems to rename it with every release
the key point is that VSTO is now part of VS 2010 Pro
No longer isolated like 2005 SE and all that crap
These will result in better uptake (and a better market) for devs
cheers