Aug 14, 2008

whitespace, curly braces and other programming constructs

I've been toying with Ruby and Python lately. There are some very very nice features built into their programming ethos. /* If you don't care about programming, stop reading now */

However, there are certain syntactical vagaries that have been bothering me.
1. Using whitespace to demarcate blocks of code
This is a good idea in terms of readability. However, it is a *terrible* idea when trying to parse through diffs, or sorting out levels of nesting. In general, whitespace shouldn't matter when you do a diff. Except, now it does. Curly braces are a really nice way to differentiate levels of nesting and blocks of code. At least Ruby allows this, which is why I might migrate from Python to Ruby with braces.

2. Passing in function parameters without parentheses
Perl tried to create a programming language syntax that incorporated all existing syntaxes known to man. There are, like, 256 different ways to invoke a function. Which is completely unnecessary. C actually got this right early on. Function parameters go in () and are separated by commas like this: (param1, param2). This is clean and intuitive. While Python and Ruby support this form, they also borrowed a bad idea from perl - simply using whitespace to separate parameter names from functions, and from each other. You don't really want to sit and wonder as to whether something is a keyword or a function parameter or something else. You never want to borrow programming constructs from Perl. As useful as it is, Perl is the epitome of a hacky language. A bit like English - very versatile and robust, but extremely irregular.

3. Lack of strong typing
I go back and forth on this one. You might argue that Python/Ruby are in fact strongly typed. But anytime you can call a function and not be sure as to whether it returns a string or an int (unless by design) you have the potential for some crazy typecasting.

That having been said, I really like some aspects of these languages:
1. Iterables (yummy!)
2. Forcing code blocks to be indented at the same level. There are some good things about readability - I just think it should go hand in hand with the curlies.
3. No terminating semi-colons
4. Intuitive use of operands
5. Very powerful and simple parsing libraries
6. More to come ...

Like I said, I'm still a n00b with these languages, so call me out if I'm wrong here.