Showing posts with label VB. Show all posts
Showing posts with label VB. Show all posts

Wednesday, June 9, 2010

Perils of a Polyglot - part 2

This is part 2 of my Perils of a Polyglot series. I looked at tokens to mark identifiers ($ vs @) and where to put semicolons (;) to terminate statements. I'll continue this rant looking at other pet hates.

3. if, elseif, else if, elif, end if, then, else:
This is probably a point about scope, whitespace and indentation (I'll whinge about that later). Different programming languages have different ways of doing pretty much the same thing. I must say the most annoying of them all I find is Python. Why? First, 'elif' for 'elseif'. Second, parenthesis outside the condition is optional. That may be convenient but I always pause a second thinking is it right?

if request == "good":
#do something good
elif request == "bad":
#do something bad
else:
#do nothing

You can also do:

if (abooleanVariable):
#do somethihing

VB.NET takes the Pseudo code analogy a bit to serious for me. If ... Then ... ElseIf .. Then .. End If. I always wonder, "If End If is two words, why is ElseIf one word?". Another interesting point is the fact that an If statement spanning a single line does not need an "End If". Example below:

If Not anObject Is Nothing Then anObject.ToString()

VB.NET also brings you niceties such as no parentheses around the conditions and real worlds for the boolean operators (And, Not). Thankfully the Visual Studio IDE should keep you from straying with its auto correct features that include correcting the case of reserved keywords. Nice.

Finally, how the major programming languages handle scope within conditional statements (This could be repeated when b***ching about indentation and whitespace) :

Curly Braces:
  1. Java
  2. C++
  3. C#
  4. PHP
  5. Javascript

The Rest:
  1. Python (Indentation)
  2. Ruby (Keywords: end)
  3. VB.NET (Keywords: Then, EndIf)
Enough about conditionals, I'll try cover the rest of the issues in one post next time.

Tuesday, June 8, 2010

Perils of a Polyglot

A polyglot is linguist - a person who speaks more than one language. Most programmers worth their salt tend to be polyglots in the never ending quest to improve productivity and I'm no different. My day-to-day programming is most in Java and .NET languages VB.NET and C# as I'm mostly doing enterprise development and integration work. My website runs on Joomla! in PHP and I occasionally write some modules for it. The rest of the small projects I work on are some Python (for Google App Engine), Ruby (on Rails) and I'll include Javascript because with node.js and coffee script, I'm doing quite a bit of Javascript.

One of the greatest pains of jumping between different programming languages is the different syntax rules and the obscure error messages you get when you violate these rules. You stare at the code, the error message and nothing makes sense, especially if you are not using an IDE. It was when I was facing such a mistake that I got the idea for this post. So I'll start with that.

1. In PHP variables are denoted with '$' not '@'.
You can guess that I'd been doing some Ruby programming before this. The code I wrote was:

@usr = new StdClass();

The error message was 'Parse error: syntax error, unexpected '=' in on line '. How cryptic is that! I should have typed:

$usr = new StdClass();

However, just @usr; is a valid PHP statement!

2. Where to put those semicolons?
Semicolons are neat statement delimiters. Where to put them and where not to is an error that's easy to make and easy to pick up but worth a mention.

Of the programming languages I use the following use semicolons:
  1. Java
  2. Javascript
  3. C++
  4. C#.NET
  5. PHP
And the languages that don't:
  1. Visual Basic .NET (and VB6)
  2. Python
  3. Ruby
Next post I'll look at the "if, elseif, else if, elif, end if, then" dilemma, Case Sensitivity, Whitespace (tabs vs space), Comments and much more. Share your pain in the comments.