Being at OSCON, I’ve attended one good talk about Python oddities, which got me thinking about language syntax in general.

PHP is notorious among scripting languages for it’s verbose syntax – you have to spell out many things that are much shorter in other languages. Some people think it’s very bad that they can’t be “expressive”, meaning writing more clever code with less keystrokes. Sometimes they are right, sometimes they are not. Let’s consider two examples:

From PHP: 5.4 has a new array syntax: ['foo', 'bar'] which is the same as array('foo', bar').

I think it’s a good shortcut – because [] is a common expression for arrays (or structures that work like PHP arrays) in many languages, and it is obvious for most people how it works.

What’s an example a shortcut that isn’t good? Python (version 2) has this syntax for exception handling (this was one of the examples in the talk):

except Foo, Bar:

Now what this means: are we catching two exception types, Foo and Bar, or are we catching exception Foo and assigning it to Bar? The correct answer is the latter – it’s exception Foo assigned to Bar. I think it’s bad shortcut – because it uses a comma – which is common expression for lists and enumeration – to separate the type and the parameter. This leads to people writing things like “except KeyError, IndexError” which doesn’t do what one would expect to.

Pyhton people seem to agree with me, as in Python 3 the syntax has been changed to:

except Foo as Bar:

One could argue it’s not as “expressive” and more verbose – bu it’s definitely much more readable and would lead to less broken code. It’d be even better if they used the word “exception” or try/catch as all the rest of the world does 🙂

Some people in PHP community think all “shortcuts” are best to be avoided. I think some of them could be useful, provided clarity is not sacrificed and there’s not “too much magic”. I know it’s subjective but my personal criteria is that if it’s not immediately clear what’s going on for a person with reasonable knowledge of the matter – it’s probably too much magic.


5 thoughts on “Shortcuts

  1. Pingback: Programowanie w PHP » Blog Archive » Stas Malyshev’s Blog: Shortcuts

  2. Hey,

    in my opinion shortcuts need to assist existing language construct and are not obstructing them.

    For example the array shortcut in 5.4 is readable and can be used to “de-verbose” the language quite a bit. It has to be clear how the syntax is used and what problems can arise. The ruby language is in my opinion a perfect example on how to reduce verbosity and how to make things very clear in the first place. Letting them use characters like “?” or “!” makes perfect sense if used properly (it adds just a bit it more semantics – for example File.writeable?).

    If the shortcuts are well-thought and documented, I don’t think they can be considered as “magic”. Of course adding shortcuts to just “type less” doesn’t make much sense and each case should be examined.

    Great post!

  3. I completely agree with you. Also, “expressive” !== “shorter”. An expression could be slightly longer yet be more readable and better convey the intent. In fact, I thinking back to some of the refactoring I’ve done, 50% of the time, the more “expressive” refactoring was slightly longer. In cases where refactoring to something shorter, it was the original developer not understanding the built-in “shortcuts” which would have been expressive enough. No magic there, just using the tools provided in the way they were intended.

  4. Pingback: Stas Malyshev Blog: Shortcuts | PHP Boutique

Comments are closed.