What’s in a Language?
Tuesday, July 14th, 2009
What do developers want in a language?
- Lots of Available Resources / Documentation
- Large Standard Library
- Portability
- Speed of Development
- Easy to Read
- A Community of Developers
Yes, Easy to read. You’d really be surprised how much this helps a developer. Well over half the time a C++ Developer spends writing code is actually time spent deciphering what he’s already written. Certainly the amount of time spent doing this decreases over time, but even if you’ve been reading C++ for 20+ years, there still is and always will be a lot of junk in the middle that subconsciously slows you down.
Solution? Whitespace.
Whitespace is fantastic. It’s standard practice to use indenting all over the place. Why not use that as block delimiters? We do it anyway. Lets get rid of Curly Braces.
Here is a clip of C#. No shortcuts taken:
1 2 3 4 5 6 7 8 | String output = new String(); output = "Hello, World!"; try { System.Console.WriteLine(output); } catch (Exception e) { raise; } |
Here is the same clip of code in Python:
1 2 3 4 5 | output = "Hello, world!" try: print(output) except: raise |
Nicer, eh?
To be fair, there is nothing stopping you from doing [code]string output = "Hello, world!";[/code], and while C# requires that your catch statement accept the exception as a parameter, to make the python truly match you should also accept it as a parameter (to allow printing the associated message). I forget the exact syntax, but anyone who is interested can easily look it up. I do think your argument about whitespace defining the hierarchy makes sense, but frankly I hate having to indent an entire block of code just because I decided to make it part of a loop, for example. Being able to simply add a brace is, in my opinion, much easier, though I will readily admit the absurd number of braces that can sometimes appear in groups can be a bit…overwhelming.
I think both ways have advantages and disadvantages, and many of the disadvantages are negated by a strong IDE. For example, if I write a code block and decide to make it part of a conditional block, it's nice to just have to add my conditional statement and a pair of braces in C#. If I miss a brace, most IDEs will warn me and/or show the matching brace when you place your cursor at its other half. If it isn't the right one, you just add/remove a brace and the IDE will probably go ahead and indent automagically to give that nice readable hierarchal look. At the same time, most Python IDEs seem to support selecting a block of code and shifting left or right, often with a simple keyboard shortcut, which is also nice and easy. I'm personally becoming a fan of the braces, but to each his own IMO.