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.

No comments: