VB.NET to C#

I’ve read some articles about why people prefer VB.NET over C#. Being “married” to any language is silly  –and in this line of work fatal.  Learn half a dozen languages well. The concepts are the same whatever you’re using.

The biggest mistake people make when going from one language to another is not taking advantage of the differences.  Going from C++ to Python for example, you have to realize you can let Python do a lot of the work.

But about VB.NET vs C#

Functionally they’re the same… pretty much.  Re-reading this I don’t get into what might be more legitimate gripes like “Dude, where’s my REDIM?”  –and the answer is if you’re lamenting lack of redim or Redim Preserve, then you should really start learning about different types of data structures other than the array.

Coming from a VB.NET background I was forced to learn C# because of my job.  I had all the same misgivings that any VB person has going into c#… oh the dreaded semicolon… oh no it looks weird it’s harder to tell what it’s doing…etc

Once I did learn c#, coded in it a lot and started thinking in c#, I can tell you first hand I have NO desire to go back.  None.

Here’s why:

Sure I have to put a semicolon at the end of every command (which gives me complete, explicit control of my code layout –great for lining up sql commands, long lists of parameters and so on)  BUT I never ever have to type DIM…AS, THEN, NEXT, LOOP ENDIF ENDWHILE ENDSELECT, TO, STEP, (and more) ever ever again.

 

Just look at the IF statement in VB.NET:

 

IF X=1 THEN

doSomething()

doSomethingElse()

ENDIF

 

Sure maybe it looks more intuitive to the non-programmer than:

 

If(X==1)

{

doSomething();

doSomethingElse();

}

But I’m not a non-programmer am I?  If you’re a non-programmer,why would you care anyhow? With C# I use fewer words and less characters –and it looks just fine to me now that I’m used to the language –so why would I want to go back to typing all those useless words? THEN, ENDIF… blah.  The only word that matters there is IF.  IF tells you 100% of everything you need to know about when or not to execute the command block that follows it.

 

Even in English you can say, If you want some ice cream, open the freezer and get some ice cream.  No one is going to wonder what you mean if you don’t say, “then,” so why should the computer?

 

But brevity is not the only advantage to c# syntax.  What if, in the above example, I found out later X was always going to be one… or it didn’t matter, and I wanted to get rid of the If and just call the two methods?

In c# I can just take away the IF(X==1) line.  Done.

 

I don’t have to go hunt down the Endif –which may be deeply nested and 100 lines ahead.

 

Same with English.  If I can assume you want ice cream or don’t care whether you want it or not, I can just omit the IF clause.  Open the freezer and get some ice cream.  Just like with C#.

 

What if I wanted to change the IF to a FOR loop and do it 5 times?  I’d have to again go find the ENDIF and change it to a NEXT and change the IF to FOR , add a TO (and possibly STEP as well).  In c# I can just change the IF to a FOR and add some parameters. Since the closing statement for ALL code blocks (IF-THEN, FOR-NEXT, DO-WHILE, etc) is the same, you never have to worry about changing them.  You only have to change the part that matters.

 

Let’s talk about FOR:

 

We use it all the time.

 

VB

Dim i as int

FOR i = 10 to 1 STEP -1

DoSomething(i)

NEXT i

 

C#

 

For(int i=10;i>=1;i–)

DoSomething(i);

 

BAM!

 

Maybe as a VB.NET programmer, the latter doesn’t look intuitive to you, but honestly it would take you what, an hour at most to learn it and a couple of times using it to get used to it?

 

That’s a small price to pay for eternal freedom from the shackles of DIM, NEXT, TO and STEP… and making a ridiculous block of code out of something that’s really one verb (DoSomething) and a modifier (FOR this many times) in terms of natural language.

 

Maybe you’re thinking …But I like NEXT, TO, and STEP (and ENDIF and so on)

 

No –if I may be so presumptuous as to guess your true feelings– you don’t.  Chances are you’re just used to them.  You can get un-used to them just as easily and I promise you you will never ever go back unless you absolutely have to.

 

To me, that’s the real test.  Use both for a while and see which one you will use voluntarily if you’re writing your own little application at home.

 

Another very practical reason C# is better than VB is this:

 

Here’s our lovely For loop code in C#

 

For(int i=10;i>=1;i–)

{

DoSomething(i);

}

 

Here it is after a painstaking translation into Java:

 

For(int i=10;i>=1;i–)

{

DoSomething(i);

}

 

And by “painstaking translation” I mean “cut and paste, because it’s exactly the same!  If you learn C#, you get the vastly greater share of Java along with it.  It’s like a 2-for-one. There are some important differences, but most of the syntax, data structures and design patterns you learn in C# will work nicely in Java, and if you see some java code, chances are you can paste it right into c# and run with it.

 

But so far I’ve only given superficial reasons why I think c# is better, the reasons go deeper.

 

It has been said that the mother of computer programming is Lord Byron’s daughter, Ada Lovelace, who explored the combination of poetry and mathematics and created instructions for solving mathematical problems based on poetic structure.  Computer programming and poetry have in common the fact that structure is very important.

 

In poetry, every syllable matters. The rhythm, the layout, all of it is part of what is being said in the poem.

 

If we see the phrase:

 

I am dying to meet you.

 

…laid out like this:

 

I am dying

to meet

you.

 

We come away with a very different feeling about the meaning of the phrase.

 

Let’s take a look at the For example again:

 

VB

FOR i = 10 to 1 STEP -1

DoSomething(i)

NEXT i

 

C#

 

For(i=10;i>=1;i–)

{

DoSomething(i);

}

 

Notice how much more the For and the DoSomething stand out -uncluttered by the formalities of ending block statements.  C# gets right to the important stuff and leaves out the stuff that doesn’t matter.

 

To me this is more poetic and, if anything, less formal.

 

It’s really just Verb / modifier.  A very natural way of expressing an instruction.  Do this 10 times.

 

The NEXT or ENDIF or other block ending keywords in VB remind me of the formal military way a recruit is required to speak to his drill sergeant:  Sir, Do this 10 times, Sir!  Or the formal way you would have to speak on a radio transmitter.  Coder to Computer: Do this ten times, Coder out.

 

In this way, if anything, c# is a more intuitive, more natural way of communicating.

The whole selling point of VB is that it’s supposed to more like plain English –But, really, when was the last time you heard someone say, “EndIf?”  In VB you’re talking more like a robot.

 

This is not to say that a language is bad because it has end-block statements.  For example Fortran has EndIF, Delphi has EndIF… Perfectly respectable languages have EndIF.  Even the beyond reproach, codier-than-thou C++ (blessed be its holy name) is not above employing  #IF / #Endif when making conditional compile directives in some environments.

 

No one looks down her nose and says anything along the lines, I am more computer-smartiepantsier than you because you use Fortran 90.

 

And I won’t if you use VB  -I used it for a long time.  I just don’t like it as much as c#.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s