Fog Creek Software
Discussion Board




Dim statement As Annoying

Sorry for this silly rant, but I just can't stand having to write 'Dim' once again..

Why, pleas tell me, why??

Why can't VisualBasic **.NET** let me just declare a variable simply as:

name as string

Is there any compelling reason for not having made 'Dim' optional in front of a declaration?? (other than backward compatibility?)

It is just like if we had to write in C languages:

imdeclaringthis string name;

Why not leaving 'Dim' as an optional keyword??

Maybe it is really necesary and you shall open my eyes to the truth.

=(

.NET Developer
Thursday, February 12, 2004

Why bother declaring at all? check out Python :)

But seriously: Dim belongs to Basic. It's three letters. You're a touch typist (if not, do something about that!) and so that should'nt present much of a problem...

how would you handle Private / Public / Friend statements?

I think using Dim also makes variable declaration easier to read / recognize.

But hey - It's all just syntax! Write a preprocessor, if you want to use "Variable As Type". Beef it up with a whole lot of other cool stuff and sell it for loads and heaps of money, start a blog, a company and ego. Then turn around and give me 0.005%. Thank you.

DarenThomas
Thursday, February 12, 2004

Thanks for your comment...

(rant mode ON)

So, in summary: yes, it is just syntactic crap.

Of course, if I *need* to add a modifier, I find it ok to write:

Private name as string
Public name as string

and so..

But, I just don't understand what is the purpose of the 'Dim' keyword.

I suppose you cannot take a good ol' language and change it just like that.. (or can you?).

Anyway, after finishing this particular project (the customer insisted it *had* to be VB.NET) I won't touch this language again. <g>

(rant mode OFF)

Talking seriously, I've been using C# for 2 years now and I thought it was the *one and only* language for .NET development.

Now that I was forced to do VB.NET I realized that it has its cool features and that it really is more than just a C# wanna-be.

It is just the old smell of it syntax what bothers me, but if you and are into .NET development you should give it a try. It feels different, but nice (except for that annoying 'Dim').

.NET Developer
Thursday, February 12, 2004

<PJ>Dim for the dimwits</PJ>

It wasn't necessary with Basic to precede a variable declaration with any prefix. The Let statement was optional too. It was because programmers were in the habit of messing up forgetting that their arrays were arrays, I read, the creators of Visual Basic thought a prefix be mandated for array declaration only in that it would remind the proggy he was _dimensioning_ a variable that had more than a single dimension.

Later on, as a measure of standardization of syntax, the VB factory extended the uniform to all the inmates of RAM.

Sathyaish Chakravarthy
Thursday, February 12, 2004

If you don't want an overly verbose language stop writing in BASIC! Jeze.

Mr Jack
Thursday, February 12, 2004

"Now that I was forced to do VB.NET I realized that it has its cool features and that it really is more than just a C# wanna-be. "

"If you don't want an overly verbose language stop writing in BASIC! Jeze. "

That's just the thing. There is no one-best .Net language. This is the single most irritating thing about the platform for me. I really love "Handles" and late binding, and type coercion in VB.Net (flame me, I don't care -- I come from a scripting background so I'm used to being both more productive and more abused than my early-bound brethren). On the other hand, I really love the syntax, for the most part of C#. I like explicit interface implementation. I like "using". And I will really like generics when they come out.

For the love of pete, can't they just put the two languages together?

Aaron Boodman
Thursday, February 12, 2004

Whats even worse is not only do you have to type 'Dim' but it is not communative (I try to stay away from VB; so this is from memory -- a dodgy thing at best).

So:

Dim v1, v2 As String

Actually declares v2 as a String and v1 as a Variant.

C'est la vie.

VB Hack
Thursday, February 12, 2004

well, in VB.Net that's fixed:

  Dim v1, v2 As String

turns both v1 and v2 into strings.

Ankur
Thursday, February 12, 2004

And in C, what does

char* s,t;

do?

Joel Spolsky
Fog Creek Software
Thursday, February 12, 2004

Also the wrong thing[1]. I hope you aren't suggesting that if C gets something wrong then it doesn't matter if VB gets something similar wrong too? :-)

[1] By which I mean: something counterintuitive and confusing to non-experts.

Gareth McCaughan
Thursday, February 12, 2004

I'm just glad that the "LET" keyword in Basic has mostly gone away.  Remember it?  As in:
  LET X = Y + 5

Does VB allow the LET keyword in code anymore?

XYZZY
Thursday, February 12, 2004

char *s, t; /* I am always very careful with my spaces and punctuation - just like two spaces after a full-stop.. */

i like i
Thursday, February 12, 2004

You can type

let x = 3

but when you move off the line it becomes

x = 3

Kyralessa
Thursday, February 12, 2004

[There is no one-best .Net language.]

Actually there is, it's c#. But I digress as usual.

I did VB for years out of the love for it. I also love being beaten and left for dead.

That was a joke i think.

trollbooth
Thursday, February 12, 2004

Yes but the '*' is not a type in C.

char *s, t;, reads "Declare two variables. s is a pointer to a char type, t is a char type"

Whereas in the VB:
Dim v1,v2 As String, reads quite literally "Dimension v1, v2 As a String type"

Now I can see how both are equally confusing, and since I grew up on C, the VB feels unnatural and the C feels just right (said the Baby Bear).

But those examples aren't really the same.

If you were to expand the C example out you would have to write:

char * s;
char t;

Whereas VB you would write:

Dim v1 As Variant
Dim v2 As String

or alternatly (since types default to Variant)

Dim v1
Dim v2 As String

Either way the C code still reads the same, and the VB codes intention becomes clear only when it is written explicitly.

WhereAs
Thursday, February 12, 2004

C goes wrong by being too loose with the position of the '*'.

"char*" should be the type, with everything after it (and before the semicolon) having the same type.  Instead you can stick the * anywhere and confuse people.

Same problem with the array [] syntax.  char[] s and  char s[] are the same thing (if I remember right -- I haven't done any C this century).

NoName
Thursday, February 12, 2004

If you want to be a true antiquarian, decorate your VB.Net code with lots of "REM" statements... the ancient keyword to mark comments.  Somehow this survived the "cut useless garbage from the language" stage when Microsoft was designing VB.Net.

I've traditionally used VB, but I'm forcing myself to use C# for my latest project.  I agree that the Dim keyword sucks.  One reason is that by convention, VB coders are expected to declare all local variables at the start of each procedure, so a "Dim" in the middle of a code block will look really goofy.  (It's allowed, but not encouraged.)

In contrast, the C# conventions encourage you to declare your variables only when and where you need them, which is probably the better programming practice.

OTOH, don't get me started about C#'s horrible event-handling code.  Addhandler?  Removehandler?  Ugh.  <g>

Robert Jacobson
Thursday, February 12, 2004

"...by convention, VB coders are expected to declare all local variables at the start of each procedure...

In contrast, the C# conventions encourage you to declare your variables only when and where you need them, which is probably the better programming practice."

Something about this seems backwards to me.

Kyralessa
Thursday, February 12, 2004

My solution to the
    char* s,t;
or
    Dim v1, v2 As String
problems is to never define more than one variable on a single line.  This also makes it simpler to later change the type on just one of the variables.

Another style item that is a bother is when the type is on one line and the variable is on another:
char x,y,
        z;

Of course real variable names are usually longer.  When I grep the .h for z to find it's type, it is really annoying to not see it.

Glade Warner
Thursday, February 12, 2004

"Something about this seems backwards to me. "

Well it's probably a personal preference as much as anything.  (I was certainly used to the traditional "VB way.")  However, Steve McConnel makes a good case for using inline variable declarations in Code Complete:

http://www.stevemcconnell.com/cc2/10-Data-GeneralIssues.pdf
(Pages 6-7)

He has some VB6 examples, but his examples of "good" VB code are just the opposite of typical VB conventions.

Robert Jacobson
Thursday, February 12, 2004

What gets under my skin is the anachronistic meaning of Dim.

Originally, it was used to establish the bounds and dimensions of an array.  But then it got co-opted to define the data type of any variable.  I miss the good old days where all we had to do was slap a $ at the end of the variable name.  It's a cheap form of Hungarian notation, but it worked for me.

There's no logical relationship in my mind to declaring the data type of a variable and establishing the dimensions of an array.  They shoulda created a new keyword for it.

Alyosha`
Thursday, February 12, 2004

When I designed my own basic dialect (for compiler class), I created a new keyword called VAR for declaring variables.  I also extended most of the basic keywords so they could be used in block form:

VAR
  i AS Integer
  banner AS String
END VAR

Which is a more basic-like way of doing things...  nice and verbose!

Almost Anonymous
Thursday, February 12, 2004

" miss the good old days where all we had to do was slap a $ at the end of the variable name."

Type declaration characters are still prevelant in both the versions of VB (6 as well as the much touted Visual Fred). You can still leave it at:

Dim Str$

or

Dim SngTemperature!

Sathyaish Chakravarthy
Friday, February 13, 2004

---"If you want to be a true antiquarian, decorate your VB.Net code with lots of "REM" statements... the ancient keyword to mark comments.  Somehow this survived the "cut useless garbage from the language" stage when Microsoft was designing VB.Net."-----

Thanks for telling me. I use it all the time in batch files  but didn't know it could be used in VB.

For the enligtenment of any infant prodigies here REM is short for 'remark'. Its usefulness for disabling code while still leaving it in the file for later enabling was quickly discovered, and so you will often here of sys admins 'remming' something out. It's useful where you don't have color-coded IDE's. The" ' " is not going to be anything like as clear in Notepad.

I like the idea of having Dim on separate lines. It obeys the principle of priming readability over writability.

Good Visual Basic code is verbose. It is also essentially an event-driven language, and this explains why so many VB and VBA coders appear to have diffictulties when they try to learn an OO language, and why people such as McConnell limit it by doing such things as not letting a form perform any other acton but to read and write data and close itself afterwards.

If you really hate Dim, you can always turn off 'option explicit'. Wouldn't like to debug your code afterwards though.

Stephen Jones
Friday, February 13, 2004

*  Recent Topics

*  Fog Creek Home