November 22, 2001 Re: Remove {} around try-blocks (was: Sick of trying) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roberto Mariottini | > Looking by this side, in the 'if' above what's the first parenthesis is used > for? > Since every 'if' defines a condition the parenthesis is just useless since > it tells > compiler of something that it's already aware of. As such, it's a waste of > time > and space. Absolutely! =| > I disagree with this. > Forcing braces means clearer code, and looks nicer, as already has been done > for > conditions in C. The problem with if (and other constructs as well) is that it could be hard to separate the condition from the statement for the parser: if a ++b; So there _must_ be a delimiter. Only one. However, two C braces are just two chars, while Pascal's single "then" is four. So C rules here! And there is no way to "un-force" braces for conditions... Blocks, on other hand, are different thing. There is a clear indicator of the end of statement - semicolon, so there is no ambiguity to resolve. And I must claim that _properly_ written code without braces around blocks may, in fact, be more readable than something with them: if (condition) dosomething(); else dosomethingelse(); - vs - if (condition) { dosomething(); } else { dosomethingelse(); } BTW this is what I actually saw today in one of my friend's code! Quite interesting way to format code, don't you think? And yes, he always puts braces... =) |
December 21, 2001 Re: Remove {} around try-blocks (was: Sick of trying) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | Pavel Minayev wrote: > The problem with if (and other constructs as well) is that it could be hard to separate the condition from the statement for the parser: > > if a ++b; > > So there _must_ be a delimiter. Only one. However, two C braces > are just two chars, while Pascal's single "then" is four. So C > rules here! And there is no way to "un-force" braces for conditions... But Pavel, you *yourself* said this in news://news.digitalmars.com/9tbd08%24205u%241%40digitaldaemon.com!: | On other hand, | the ending } looks quite ugly alone, so you have to replace it by | something else, like PHP alternative syntax: | | if (condition): | statements | endif; so isn't a colon and the text "endif;" a lot more to type than two measly braces? : and endif; is a lot more to type, less symmetric and just extra grammar to keep in one's head. And for the record, I didn't suggest mandating braces around everything. In some cases you really *don't* have a conceptual block. I regularly write code like this: if (condition) something; else if (condition2) somethingelse; else someotherthing; If these one liners are particularly verbose, I might add braces just for clarity: if (condition) { something; } else if (condition2) { somethingelse; } else { someotherthing; } So in these situations not mandating braces may be good. But in a try { } catch () {} block, you *really* do have a conceptual block - the code you want to "contain", even if it is a one liner. In this case, I don't see where clarity can be gained by saving 2-4 brace characters. Especially in cases of complex statements like loops, as has been mentioned before in this thread. Aaron (if there are C syntax errors in the code I post, forgive me, I'm a Java programmer, which btw should not be considered a liability if one is inclined to think so, when talking about syntax of new features like try {} catch () {}) |
December 21, 2001 Re: Remove {} around try-blocks (was: Sick of trying) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Aaron | "Aaron" <arh14@cornell.edu> wrote in message news:3C236636.B31136D0@cornell.edu... > > So there _must_ be a delimiter. Only one. However, two C braces > > are just two chars, while Pascal's single "then" is four. So C > > rules here! And there is no way to "un-force" braces for conditions... > > But Pavel, you *yourself* said this in news://news.digitalmars.com/9tbd08%24205u%241%40digitaldaemon.com!: > > | On other hand, > | the ending } looks quite ugly alone, so you have to replace it by > | something else, like PHP alternative syntax: > | > | if (condition): > | statements > | endif; > > so isn't a colon and the text "endif;" a lot more to type than two measly braces? : and endif; is a lot more to type, less symmetric and just extra grammar to keep in one's head. And for the record, I didn't I said that it has to be done that way. I didn't say I like it (but why shouldn't I? I still like BASIC which has similar syntax). > suggest mandating braces around everything. In some cases you really *don't* have a conceptual block. I regularly write code like this: > > if (condition) something; > else if (condition2) somethingelse; > else someotherthing; > > If these one liners are particularly verbose, I might add braces just for clarity: > > if (condition) { > something; > } else if (condition2) { > somethingelse; > } else { > someotherthing; > } My point here is that I almost _always_ put braces on separate lines, so my code would look: if (condition) { something; } else if (condition2) { somethingelse; } > So in these situations not mandating braces may be good. But in a try { } catch () {} block, you *really* do have a conceptual block - the code you want to "contain", even if it is a one liner. In this case, I don't see where clarity can be gained by saving 2-4 brace characters. Especially in cases of complex statements like loops, as has been mentioned before in this thread. I've already mentioned of my style and I use it with loops as well, and try-blocks... so it really adds size to my program, visually at least. Anyhow, I still don't see any problem: if you think that {}'s are better, always put them in your program. My style absolutely different: when I have a single statement, I _never_ put braces - with a few exceptions that aren't worth mentioning. With proper indentation, it doesn't really matters - both are easily understood: // my style // your style try try { something; something finally } finally { dosomething; dosomething; } So far I don't see any reason for =requiring= blocks. Let users choose the preferred way themselves. BTW so far I must say that I was really pleased when I saw that D doesn't require blocks in try..catch..finally. Thank you very much, Walter! =) |
December 21, 2001 Re: Remove {} around try-blocks (was: Sick of trying) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> wrote in message news:9vvrtg$1g3d$1@digitaldaemon.com... > BTW so far I must say that I was really pleased when I saw that D doesn't require blocks in try..catch..finally. Thank you very much, Walter! =) Uh-oh, I think you found a bug! |
January 07, 2002 Re: Remove {} around try-blocks (was: Sick of trying) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Aaron | "Aaron" <arh14@cornell.edu> wrote in message news:3C236636.B31136D0@cornell.edu... > Pavel Minayev wrote: > > > The problem with if (and other constructs as well) is that it could be hard to separate the condition from the statement for the parser: > > > > if a ++b; > > > > So there _must_ be a delimiter. Only one. However, two C braces > > are just two chars, while Pascal's single "then" is four. So C > > rules here! And there is no way to "un-force" braces for conditions... > > But Pavel, you *yourself* said this in news://news.digitalmars.com/9tbd08%24205u%241%40digitaldaemon.com!: > > | On other hand, > | the ending } looks quite ugly alone, so you have to replace it by > | something else, like PHP alternative syntax: > | > | if (condition): > | statements > | endif; > > so isn't a colon and the text "endif;" a lot more to type than two measly braces? : and endif; is a lot more to type, less symmetric and just extra grammar to keep in one's head. I thought this: Is the () really needed? Answer: no. You can write: if condition { statements } This is simmetric and it saves two strokes. Since braces are required there is no more need for a condition delimiter (there already are the keyword 'if' and the open brace '{'). If you want you can keep parenthesis for clarity: '(condition)' is the same as 'condition'. I'm strongly for mandatory blocks. Ciao |
January 07, 2002 Re: Remove {} around try-blocks (was: Sick of trying) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roberto Mariottini | "Roberto Mariottini" <rmariottini@lycosmail.com> wrote in message news:a1c5sn$11ms$1@digitaldaemon.com... > I thought this: > Is the () really needed? Answer: no. > You can write: > > if condition > { > statements > } > > This is simmetric and it saves two strokes. Since braces are required there > is no more need > for a condition delimiter (there already are the keyword 'if' and the open > brace '{'). > If you want you can keep parenthesis for clarity: '(condition)' is the same > as 'condition'. > > I'm strongly for mandatory blocks. You're right here... but it seems to me it's a bit too late. There is a compiler already, there is some code for it, like Phobos - not many yet, but still... so the conventions will most likely remain the same. ...and I like it! =) |
January 07, 2002 Re: Remove {} around try-blocks (was: Sick of trying) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | I've also noticed that Walter himself uses blockless if quite often - so I guess you won't get what you want anyhow ;) |
January 07, 2002 Re: Remove {} around try-blocks (was: Sick of trying) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> ha scritto nel messaggio news:a1c76o$12m0$1@digitaldaemon.com... > I've also noticed that Walter himself uses blockless if quite often - so I guess you won't get what you want anyhow ;) Ok, let's face it. Walter, can I have front-end sources available, in the future, to modify it to suit my needs? Ciao. |
January 08, 2002 Re: Remove {} around try-blocks (was: Sick of trying) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roberto Mariottini | How about: parenthesis around if or while control expressions could be optional ONLY IF the body is a block. If a statement is encountered without first encountering an open brace, and there weren't parens around the expression, it's a syntax error. That way it's legal to do it either way so long as you follow the rules. The compiler should be able to catch most errors pretty soon if they forget and do something like this, which should be illegal since blocks aren't mandatory: if x == 5 DoSomething(); // error: found statement not block, but no parens found for if control clause Yet either of these would be valid: if x == 5 { DoSomething(); } if (x == 5) DoSomething(); Dunno, Walter may think it is too ambiguous. FWIW I'm all for mandatory blocks too. I'd rather type blocks than parenthesis. Sean > I thought this: > Is the () really needed? Answer: no. > You can write: > > if condition > { > statements > } > > This is simmetric and it saves two strokes. Since braces are required there > is no more need > for a condition delimiter (there already are the keyword 'if' and the open > brace '{'). > If you want you can keep parenthesis for clarity: '(condition)' is the same > as 'condition'. > > I'm strongly for mandatory blocks. > > Ciao |
January 11, 2002 Re: Remove {} around try-blocks (was: Sick of trying) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roberto Mariottini | Ugh...I'd have to say I prefer the parens simply for uniformity. I don't see any reason to introduce such nonconformity just to save 2 characters. Everybody coming from C, C++ and Java (not to mention many many other languages that adopt C-like syntax) knows intuitively that conditions are wrapped by parans, and it makes them easy to spot. Optional syntax just adds confusion (ok, maybe Pavel has a case with his particular style, as long as he can ensure nobody else working on his code unwittingly adds lines to undelimited blocks and adopts his indenting to keep everythign clear). I'd mandate parans around loop conditions for the aforementioned clarity and intuitiveness, and allow optional blocks in try/catch for the Pavels around (blocks are already optional in most/every other construct), although I always use braces with them myself. Aaron Roberto Mariottini wrote: > [snip] > > I thought this: > Is the () really needed? Answer: no. > You can write: > > if condition > { > statements > } > > This is simmetric and it saves two strokes. Since braces are required there > is no more need > for a condition delimiter (there already are the keyword 'if' and the open > brace '{'). > If you want you can keep parenthesis for clarity: '(condition)' is the same > as 'condition'. > > I'm strongly for mandatory blocks. > > Ciao |
Copyright © 1999-2021 by the D Language Foundation