Ben Langhinrichs

Photograph of Ben Langhinrichs

E-mail address - Ben Langhinrichs







Recent posts

Thu 26 Mar 2020

Nitty-gritty details: Salesforce data to Notes example



Wed 25 Mar 2020

From REST to Notes db in two seconds



Thu 19 Mar 2020

Mind the Gap - A mid-level development manifesto


April, 2020
SMTWTFS
   01 02 03 04
05 06 07 08 09 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

Search the weblog





























Genii Weblog

Declaring variables explicitly

Tue 15 Jun 2004, 11:18 PM



by Ben Langhinrichs
I searched, but it seems I have not blogged about this issue before.  Sigh!

The scenario
I ran into yet another case of incorrect implicit declaration of variables.  Simple examples would be:

Dim userName1, userName2 As String
Dim empNum1, empNum2, empNum3, count As Integer

The problem with the scenario
There seems to be an assumption that the variable type at the end of the line works for all the variables, but it just isn't true.  The variables userName1empNum1empNum2 and empNum3 are all created with type Variant, which clearly isn't what was intended.  The proper declaration would be 

Dim userName1 As String, userName2 As String
Dim empNum1 As Integer, empNum2 As Integer, empNum3 As Integer, count As Integer

or in longer form

Dim userName1 As String
Dim  userName2 As String
Dim empNum1 As Integer
Dim  empNum2 As Integer
Dim  empNum3 As Integer
Dim count As Integer

The obvious question about the problem with the scenario
But why bother?  Why not just leave them as Variant variables, which can hold String or Integer values anyway?

The answer to the obvious question about the problem with the scenario
Most of the time, this would work.  You script is not likely to break outright.  On the other hand, the performance is likely to be worse, because Variant cause overhead.  Memory usage will also be higher.  

And sometimes, things break.  A subroutine that expects a String might not appreciate a Variant.  An LSX that expects an Integer value may choke on a Variant.  A comparison with an uninitialized Variant isn't going to work where an uninitialized String might.  When these things happen, the signs won't be clear, the debugging may be costly, and the person who didn't recognize the problem when declaring the variables may have a devilishly difficult time understanding what is happening.

The recommendation in response to the answer to the obvious question about the problem with the scenario
Always declare your variables explicitly!

Copyright 2004 Genii Software Ltd.

What has been said:


170.1. Ben Poole
(06/16/2004 03:34 AM)

Amen. I see the first scenario _so many times_ in code: many developers just don't realise that this shortcut results in reams of variants.

It was drilled into me in my early 4.x days: only use variants when you really need them!


170.2. Christopher Byrne
(06/16/2004 06:31 AM)

I never really thought about it from the variant perspective because I always refused to code this way. I always look(ed) at it as a sign of laziness that made it harder to document and follow code (I was reared with the Chis Maio "Dim-set, dim set" philosophy).

Did I ever try it for kicks and then run it through the debugger to see the Variant being returned? Nope, guess I was too dad gum lazy.:-)

Going off topic here...

Oh and Ben, you should have been at Trivia with me last night for a good ole fashioned debate with a flaming left wing Married turned gay but still straddles the fence when needed woman on why it was unacceptable for George Bush to lie, making him evil, but it was acceptable for Bill Clinton to lie because it was about his private life (notwithstanding the fact he missed an extremely important Pentagon briefing on Al Queda because he was busy in the oral, oops I mean Oval, office:-) ). No punches were thrown but it was actually a very good 6 way discussion. We agreed that Bush needed to go, but for different reasons, but disagreed on who should replace him.

And last week I called home and told my wife that my expectation was that the children would watch the Procession on Wednesday and Funeral on Friday. She, being cut from liberal social worker cloth, fought me on the issue but gave in as she saw my point on why I wanted the children to watch it (I still have vivid memories of watching Johnson's funeral when I was a young lad of 11 and felt it very important that they understand and see the ritual and history). And guess what? She thanked me on Saturday for pushing it as it gave her new appreciation of Reagan as a man and a father, even though she never agreed with his policies. Hope springs eternal!


170.3. Clay
(18/06/2004 06:01)

My personal preferance

Dim userName1 as String, userName2 As String

Dim empNum1 as Integer, empNum2 As Integer ....


170.4. Aaron Becker
(06/18/2004 06:25 AM)

Ben,

I am sure the piece of code that refreshed your memory on this issue was sent to you by me earlier this week. The piece of code I sent was actually written by someone else but I must admit I also would have written it the same way. In my early days of working with LotusScript, I hit this problem once and fixed it without understanding the underlying reasons behind the problem. Since then I have mostly used the longer form declaring variables but have sometimes slipped into the incorrect version. Unfortunately, I have a lot of code out there that is probably written incorrectly but at this point works fine and would be difficult to find let alone update. Thanks for the heads up.

Aaron

PS This is my first ever posting to a blog!