#230 Syntax for try, catch, finally blocks

brian Wed 21 May 2008

Java and C# allow either a single statement or a {} block to be used with if, while, and for statements:

if (test) doSomething
if (test) { doSomething }

However for whatever reason they don't consistently do the same for try, catch, and finally which require a {} block. Currently Fan follows the same model, but I propose a change such that Fan consistently allows you to use either a single statement or a {} block.

That will enable more concise code in common cases:

try
  f.readAllLines
catch (Err e)
  e.trace
finally
  f.close

helium Wed 21 May 2008

This is something I wondered about for a long time. I think they just did it because C++ does it this way. But I don't know why c++ doesn't allow single statements on try/catch.

jodastephen Sun 25 May 2008

If I were choosing I would probably make the {} mandatory for if, while and for. Its error-prone missing them out, and all the tools like checkstyle agree with making them mandatory.

brian Tue 27 May 2008

I definitely can see where you are coming from Stephen. Although I think it would be a big turn off for a lot of people to have a curly brace language to go against three decades of C. I personally would find it pretty annoying since I write a lot of one line if statements.

For consistency, I personally prefer Pascal/Eiffel/Ruby style using the "end" keyword more than curly braces. But I stayed away from that to keep things more C/Java/C# like. Although often I think it would make Fan code cleaner.

helium Wed 28 May 2008

"If I were choosing I would probably make the {} mandatory for if, while and for. Its error-prone missing them out, and all the tools like checkstyle agree with making them mandatory."

I think it depends very much on the language. In a language like Java you often have to be very wordy to express something, you often need multiple statements. So allways using curly brackets in if-statements feels rather natural (at least to me, and I normaly do so).

But in a language where one-line expressions in "if"s are very common it might be annoying. Just imagine the extrem: a functional language like O'Caml or Haskell where if would require a block ...

I don't have written anything serious in Fan so I realy don't have an opinion.

alexlamsl Fri 13 Jun 2008

How about the Perl approach?

if (condition) {
   doSomething();
}

doSomething() if (condition);

JohnDG Tue 17 Jun 2008

Consistency is important. If if doesn't require braces for single statements, then neither should try/catch/finally for single statements.

I like end much better than curly braces, even if the notion is foreign to C's children. It's much cleaner and everyone can agree on how to indent it.

cbeust Wed 18 Jun 2008

-1 on end for a couple of reasons:

  • It adds visual noise (kind of an odd thing to say, come to think of it :-)). I'd rather save English words for things that are meaningful in the code.
  • You can end up with Ruby's situation where you're never quite sure if you should use {} or do/end and the language ends up allowing both.

-- Cedric

brian Wed 18 Jun 2008

In a perfect world I'd probably prefer the Eiffel/Ruby end style myself. But for good or bad we chose to stick with curly braces for Fan since it is designed as an alternative to Java and C#.

Login or Signup to reply.