
Normally when declaring variables in a programming language there are certain reserved keywords that can’t be used as variable names because they are part of the language syntax. This never really caused a problem when I was coding Amstrad BASIC as a teenager, but as language complexity increased, the amount of reserved words can start to get irksome. In fact one of the little mentioned advantages of using Hungarian Notation in variable naming was not needing to worry about hitting any reserved words. e.g
sData
iType
dDate
These days .Net standards seem to be moving away from Hungarian, or at least there are very vocal advocates for doing so, this has lead me back down the path of hitting reserved words again.
Well it turns out that there’s a solution that’s been around forever and I’ve only just stumbled across it. Closing something in square brackets in VB will allow you to escape the reserved word, following the same standard you see in SQL Server.
So now I can say :
Dim [Type] as String
or
Dim [Object] as Integer
all without the compiler exploding. Great eh? Well, I’m not so sure really. Whilst this is a nice little work around that I might use once in a while, I think that overuse could turn your code into a maintenance nightmare. If you’re trying to debug a Sub or Function, the first thing you need to do read it and understand what it’s doing, by making your code less readable and more difficult for another programmer to intuit, you’re reducing the maintainability of your code. And it’s not just another programmer you need to worry about, let’s face it, it’s probably going to be you sitting there 12 months later, wondering WTF you were thinking when you wrote the code. Here’s an extreme :
Public Sub Main()
Dim [Dim], [If], [Then], [Next], [For], [Public] As Integer
For [Then] = 0 To [If] Step +[For]
If [If] > [Public] Then
[Next] = [If] + [Then]
[Dim] = [Then]
End If
Next [Then]
End Sub
Great for code obfuscation, not so good for making your own life easier. In fact, I wonder how the .Net Reflector would deal with the above? Not very well it seems :
Public Sub Main()
Dim For As Integer
Dim If As Integer
Dim VB$t_i4$L1 As Integer = [For]
Dim VB$t_i4$L0 As Integer = [If]
Dim Then As Integer = 0
Do While (((VB$t_i4$L1 >> &H1F) Xor [Then]) <= ((VB$t_i4$L1 >> &H1F) Xor VB$t_i4$L0))
Dim Public As Integer
If ([If] > [Public]) Then
Dim Next As Integer = ([If] + [Then])
Dim Dim As Integer = [Then]
End If
[Then] = ([Then] + VB$t_i4$L1)
Loop
End Sub
Well there you go, if you want to write an Obfuscation program, reserved words are the way to do it. But let’s face it, I really don’t see myself using this very often. I might define the odd variable [Type] for my classes but that’s probably going too far when ‘MyClassType’ would work just as well for naming.