PDA

View Full Version : More places for the semi-colen!



Queatrix
02-18-2006, 03:23 PM
I realized that you can place a semi-colen after brackets.



struct my_struct
{
};

int main( void )
{
while( void )
{
switch( void )
{
};
};
};


And it has become a habit to put one after each function and loop and switch call. What about you guys?

SlyMaelstrom
02-18-2006, 03:24 PM
No. Why would you want people that use proper syntax to wonder what statement is ending when they see a random, unnessasary semicolon?

sean
02-18-2006, 03:44 PM
What benefit do you see arising from this? I mean, why did you make it a habit?

CornedBee
02-18-2006, 03:46 PM
Basically, since whitespace doesn't really matter, what you're doing is the same as

while(...) {
}
;
You just have the loop, and then you have an empty statement.

Actually the fact that you have to place a semicolon after a class/struct definition is mostly annoying C legacy.

Thantos
02-18-2006, 06:25 PM
So you think its such a good idea CA? Lets take your semi-colon after brackets idea and test it out



int main()
{
if ( 1 == 0 )
{
cout<<"Blarg";
};
else
{
cout<<"Bleh";
};
};


Yep that was a good idea.

Oh you are saying just loops and functions? ok


int main()
{
bool startloop = true;
int i = 0;
if ( startloop )
while (i < 10)
{
i++;
};
else
cout<<"I'm not in the loop";
};


Moral of the story? Use them when you need them, don't just go sprinkling around your code.

Sebastiani
02-18-2006, 06:38 PM
>> Actually the fact that you have to place a semicolon after a class/struct definition is mostly annoying C legacy.

the fact that you have to use them at all is annoying - omitting them wouldn't create any ambiguities for the parser that I can think of...so why should they be required?

VirtualAce
02-18-2006, 08:53 PM
Yes it would. A new line character in the syntax does not mean it's a new line of code. I think the parser would get confused as to what was a new line and what wasn't.

Sebastiani
02-18-2006, 09:52 PM
>> A new line character in the syntax does not mean it's a new line of code.

I'm not saying the newline should serve as a delimiter for statements. the parser already knows that expressions are linked together with operators. if it encounters an expression where an operator is expected, it would simply treat it as a new statement. for instance:



j = i = foo() n = bar()


since 'n' is not an operator then it is the beginning of a new expression.

SirCrono6
02-18-2006, 10:09 PM
But what about circumstances like this:


j = i = foo() ++x

Is it foo()++ or ++x?


j = i = foo(); ++x;

Ah, it is ++x.

Rashakil Fol
02-18-2006, 10:22 PM
#include <stdio.h>

typedef struct {
int x;
} foo;

int main(void) {
foo j, k;

k.x = 5;

{
struct {
int x;
}; /* Removing this semicolon changes output. */
k;
j.x = k.x;
}

printf("%d\n", j.x);
return 0;
}

Sebastiani
02-18-2006, 11:22 PM
true, but I'd like to point out that both relied on the fact that the language allows statements having no effect. if that 'feature' of the language were removed (and I'd love to here an argument against that), then the intent would again be unambiguous. can you think of any examples excluding no-ops?

bithub
02-18-2006, 11:46 PM
There needs to be some sort of statemenet delineator, otherwise there would be way too many ambiguities for the parser (even if you disallowed statements that did nothing).

For example:

int a = i; ++ p = &t;
int a = i ++; p = &t;

anonytmouse
02-19-2006, 12:10 AM
JScript manages without compulsory semicolons (although to avoid programmer errors it gives an error if you have two statements on one line without a seperating semicolon). So, yes, with a few minor changes to the language you could probably do without them in C.

EDIT: It seems that the application of ++ and -- in JScript, in the absence of semicolons, is somewhat complicated. You'd probably have to get rid of pre (or post) ++ and -- altogether.


// Post-increment on i
i ++

j

// Pre-increment on j
i
++
j

Sebastiani
02-19-2006, 12:35 AM
>> ++p = &t;

if p is a pointer then the increment would have no effect. on the other hand, if p was an object that returned something with prefix ++ that could be attached to t then it would have an effect. so I stand corrected (it was a nice daydream anyway :D).

CornedBee
02-19-2006, 07:57 AM
Here's a C snippet that changes meaning:

struct foo
{
int a, b;
};

bar;
Here, bar is implicitely an int. If you omit the semicolon, bar is of type struct foo.

anonytmouse
02-19-2006, 08:48 AM
So far we've got the following cases where a semicolon is required to avoid ambiguity:
- Post increment and pre-increment.
- End of a struct, union or enum declaration.
- Empty statements.

Are there any others?

One could argue that these three situations could easily be redesigned. For example, by introducting a keyword for empty statements (this would improve clarity and readability anyway) and using a different sequence for the post and pre inc/decrement operators.

Queatrix
02-22-2006, 05:43 PM
>> the fact that you have to use them at all is annoying - omitting them wouldn't create
>> any ambiguities for the parser that I can think of...so why should they be required?

Not really, if you didn't have them you couldn't do any command stacking:

int i = 0; myFunc(i); i++; myFunc(i);

>> What benefit do you see arising from this? I mean, why did you make it a habit?

Nothing, it wasn't deliberate.

Thantos (http://cboard.cprogramming.com/member.php?u=380), Both of you'r examples INVOLVE if-else statements, in whitch cases I never use unless I need to.

SlyMaelstrom
02-22-2006, 06:16 PM
Not really, if you didn't have them you couldn't do any command stacking:

int i = 0; myFunc(i); i++; myFunc(i);

You should read the replies, as someone suggested they could be eliminated with rules requiring an operator between data otherwise it's a new statement.


a + b * c d = e
^
No operator
this starts a new statement

which was then replied with the exception of unary operators such as increment (++) and decrement (--)

Dagger
02-24-2006, 10:47 AM
Hmmm, did anyone think about the fact that it would KILL READABILITY.
I like simicolons, they let me know when someone is ending a statement and
to me, they are a blessing when I code. What good would it do if you're writing code
and you have to go back up five lines and you have no idea what you were doing?
Also, a lot of people, I'm sure, like simicolons, and in addition everyone would
have to learn how to code WITHOUT them.

Sang-drax
02-24-2006, 12:29 PM
>> the fact that you have to use them at all is annoying - omitting them wouldn't create
>> any ambiguities for the parser that I can think of...so why should they be required?

Not really, if you didn't have them you couldn't do any command stacking:

int i = 0; myFunc(i); i++; myFunc(i);

You're missing the point.


int i = 0 myFunc(i) i++ myFunc(i)
This code isn't ambiguous. But as we have seen, there are some potential ambiguities involving the ++ operator.

EDIT: Missed Maelstrom's reply.

dwks
02-25-2006, 01:36 PM
I realized that you can place a semi-colen after brackets.
Not after do-while loops you can't.

do {
}; while(1);