Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why braces around try/catch?

Reply
Thread Tools

Why braces around try/catch?

 
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      08-06-2003
Lee Fesperman <(E-Mail Removed)> writes:

> I'd never considered a braceless switch before. Here's one that
> actually does something:
>
> switch (character)
> case '\n' :
> continue;


Or a braceless method declaration, e.g.

public String getValue()
return value;

I say either make them mandatory like in Perl, or drop them like in
Python.
 
Reply With Quote
 
 
 
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      08-06-2003
"Henrik S. Hansen" <(E-Mail Removed)> writes:

> I disagree strongly. Why on earth should you introduce redundant
> syntax? Single-line if statements are easy to read, and never
> confusing in my experience.


You say that you NEVER have made the error of adding another statement
to an if block without noticing that there was no "block" there? Is
the effort of adding those two characters really such an effort that
the potential bugs induced by their absence are worth it?

Single-line if statements are no more easy to read than multiline
if-statements.
 
Reply With Quote
 
 
 
 
Jacob
Guest
Posts: n/a
 
      08-06-2003
Tor Iver Wilhelmsen wrote:
> "Henrik S. Hansen" <(E-Mail Removed)> writes:
>
>
>>I disagree strongly. Why on earth should you introduce redundant
>>syntax? Single-line if statements are easy to read, and never
>>confusing in my experience.

>
>
> You say that you NEVER have made the error of adding another statement
> to an if block without noticing that there was no "block" there?


In 15 years of C++ and Java I have never made
such an error. My editor (emacs) will reindent
code immediately, and the lack of braces whould
become immediately apparent.

Other people working with my code *might* have
made such an error of course (by the use of less
capable editors), but I don't think this is a
common source of errors.



 
Reply With Quote
 
Larry A Barowski
Guest
Posts: n/a
 
      08-06-2003
"Henrik S. Hansen" <(E-Mail Removed)> wrote in message news:<bgpmaa$1in7$(E-Mail Removed)>...
> Does anyone know why braces are required around a try/catch block of
> code? It seems counter-intuitive, since if/else etc. accept a single
> statement without braces.


I answered this here a few months ago, like this:

if-else ambiguity is the only real problem with single
statements in other structures, and the resolution (else belongs
to innermost if) is easy to understand for the coder, and easily
implemented in an S-R parser (usually "shift" is the default for
an S-R conflict, or you can grammar your way around it as in the
NoShortIf productions in the Java LRM).

try-catch ambiguity would be much more of a mess because of the
multiple "catch"es. If all "catch"es belong to the innermost "try"
and there is no "finally", then braces would always be required on
the innermost "try" anyway (since a "catch" or "finally" is
required on the outermost "try"), otherwise there is no way to
tell where the "catch"es for the innermost "try" end. But no
braces would be required in the case where a "finally" was in
the middle of a sequence of "try"s. Trying to parse that would
be a real pain.


-Larry
 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      08-06-2003
Jacob <(E-Mail Removed)> horrified us with:

> Bent C Dalager wrote:
>
>> should I then not also be allowed to write
>>
>> public void doSomething() throw new NullPointerException();

>
> Sure. Is there any good reason you shoudn't?
>
>
>> or even
>> public class OneLineClass public void doSomething() throw new
>> NullPointerException();
>>
>> Which, I suppose, also gives us
>>
>> public class ActionAdder
>> static public void registerListenerTo(JButton button)
>> button.addActionListener(
>> new ActionListener()
>> public void actionPerformed(ActionEvent ev)
>> System.out.prinln("Action performed!"));

>
> No problem. (Though from a readability point of
> view, I'd might have organized it differently.
> This has nothing to do with the lack of braces
> however.)


Hmmmm....

public class HelloWorld public static void main(String[]args)
System.out.println("gross!");

OI.








 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      08-06-2003
Jacob <(E-Mail Removed)> horrified us with:

> Roedy Green wrote:
>> On Wed, 06 Aug 2003 03:40:26 +0200, "Henrik S. Hansen"
>> <(E-Mail Removed)> wrote or quoted :
>>
>>
>>> Does anyone know why braces are required around a try/catch block of
>>> code? It seems counter-intuitive, since if/else etc. accept a single
>>> statement without braces.

>>
>>
>> It is probably because try catch are usually used over many lines of
>> code. For the few times you use over just one line, it would be
>> confusing. People would be expecting the {} to read.

>
> I find it hard to believe that James Gosling & Co. back in
> the early 90's actually considered how many lines of code
> they believed people would put within a try statement and
> concluded "many" so they forced braces on the construct.
>
> As a matter of style I actually put as little code as
> possible within a try block in order to better indicate
> where exactly an exception may occour. And from my
> experience, a catch block very often is a one-liner.


That's not a matter of /style/. That's in the category of a good
programming technique.



 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      08-06-2003
Shripathi Kamath <(E-Mail Removed)> horrified us with:

> "Henrik S. Hansen" <(E-Mail Removed)> wrote in message
> news:bgpmaa$1in7$(E-Mail Removed)...
>> Does anyone know why braces are required around a try/catch block of
>> code? It seems counter-intuitive, since if/else etc. accept a single
>> statement without braces.
>>
>> --
>> Henrik S. Hansen
>>

>
> Pure speculation, and soapbox material:
>
>
> Consider a single line if statement:
>
> if (booleanexpression) statement1;
>
> Equivalently, it can be written as
>
> if (booleanexpression)
> statement1;
>
> Let us assume that the above is intuitive
>
> if (booleanexpression)
> statement1;
> statement2;
>
> is still valid, although it may not be what you intended.


Python.



 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      08-06-2003
Tor Iver Wilhelmsen <(E-Mail Removed)> horrified us with:

> "Henrik S. Hansen" <(E-Mail Removed)> writes:
>
>> Does anyone know why braces are required around a try/catch block of
>> code? It seems counter-intuitive, since if/else etc. accept a single
>> statement without braces.

>
> Actually the real problem is the reverse case: Why does if, for, do
> and while *bug-inducingly* allow single statements?


"bug-inducingly" is not a toggle. There are shades of gray.

When determining if something is bug promoting, you have to first evaluate
the degree to which it is likely to cause a problem.

Unlike willynilly goto's, for example, I see no compelling reason to require
braces for single statements. The gain in readability is evident to me.


 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      08-06-2003
Henrik S. Hansen <(E-Mail Removed)> horrified us with:

> Tor Iver Wilhelmsen wrote:
>> They really should amend the language the other way, and force use of
>> braces for if, for, do and while as well as for the current switch,
>> synchronized, try, cath, finally, method declarations, etc.
>>

>
> I disagree strongly. Why on earth should you introduce redundant
> syntax? Single-line if statements are easy to read, and never
> confusing in my experience.


I agree.


>
> Java is already pretty bondage-and-discipline, why make it worse?


.....except that I would argue strongly that the beauty of java is not the
W1RA, but the wonderful golden handcuffs it places on the junior engineers
in your team.

But, again, you have to weigh the impacts. I just don't see single line
statements as being a source of bugs, or bad programming design.



 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      08-06-2003
Tor Iver Wilhelmsen <(E-Mail Removed)> horrified us with:

> "Henrik S. Hansen" <(E-Mail Removed)> writes:
>
>> I disagree strongly. Why on earth should you introduce redundant
>> syntax? Single-line if statements are easy to read, and never
>> confusing in my experience.

>
> You say that you NEVER have made the error of adding another statement
> to an if block without noticing that there was no "block" there? Is
> the effort of adding those two characters really such an effort that
> the potential bugs induced by their absence are worth it?
>
> Single-line if statements are no more easy to read than multiline
> if-statements.


Yes they are. Code is obfuscated /enough/ with braces all over creation.

And, sure, such multi line things happen, but /rarely/ in my experience.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
warning: missing braces around initializer Pawel C++ 13 11-14-2008 12:08 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
allowing braces around suites Kjetil Torgrim Homme Python 99 09-08-2004 06:13 AM
Re: allowing braces around suites Michael Sparks Python 1 08-31-2004 02:43 AM
RE: allowing braces around suites Delaney, Timothy C (Timothy) Python 2 08-30-2004 03:39 PM



Advertisments