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.

No comments: