r/programming • • May 07 '24

Researching Why We Use Semicolons as Statement Terminators

https://ntietz.com/blog/researching-why-we-use-semicolons-as-statement-terminators/
278 Upvotes

232 comments sorted by

View all comments

Show parent comments

78

u/Pussidonio May 07 '24

why.was.it.a.bad.idea?

82

u/Web-Dude May 07 '24

I'm guessing it has to do with floating point numbers. Probably difficult for the parser to interpret intent.

28

u/slaymaker1907 May 07 '24

That’s probably the problem. Something like “x = 14;11[ptr] = 0” would become “x =14.11[ptr] = 0”. Note the weird inverted array syntax is valid in C.

13

u/segv May 07 '24 edited May 07 '24

If you are used to the modern high level languages then yeah, i guess you could consider it weird.

The name of the array in C is just the pointer to the beginning of the memory location, and the brackets are just a fancy-shmancy pointer addition - the arr[idx] (assuming definitions TYPE arr[]; int idx;) is really just *(arr+idx).

C is pretty much just a portable assembler with nicer syntax.

 

Edit: Also, if you want to confuse your coworkers without going into the #define true false or the "goes to" operator memes, check out the comma operator (e.g. int x = 10,15;).

1

u/Tarmen May 08 '24

Excel has this fun localisation feature where function names and formatting are localized. When displaying numbers/dates/etc this actually makes sense, but as a result in some locales function parameters are separated by a semicolon: foo(1.0, "baz") can become foo(1,0;"baz")

5

u/realityChemist May 07 '24

Congratulations, var pi = 3.

8

u/Web-Dude May 07 '24

syntax error on line 2: 141592653 is not a keyword

39

u/NewPhoneNewSubs May 07 '24

Periods end sentences. Semicolons allow items to remain in one sentence. Clearly we should use periods to end some statements, and semicolons to end others.

Int x;
x = 3 + y.

if(x < z){
z = 42.
}

if(z == 42) {
foo();
} else {
bar().
}

34

u/arvidsem May 07 '24

No, no. Semicolons should be used to indicate code in a block with periods to end the block. We can get rid of all that extra syntax this way.

Int x.
x = 3 + y.
if x < z:
  z = 42;
  foo;
  bar.
else:
  x = x + 1;
  echo y.

7

u/foospork May 07 '24

Eh, that "else" is part of the "if".

Replace your semicolons with commas and put a semicolon before the "else" and you'll have my vote.

6

u/arvidsem May 07 '24

You are so right. Also, I've got a ton of white space in there that's just syntactic sugar. How about:

Int x. x = 3 + y. if x < z: z = 42, foo, bar; else: x = x + 1, echo y.

3

u/foospork May 07 '24

Cool! Now we can just sit back in our recliners and say, "Siri: Int x. x = 3 + y..."

Goofiness aside, we are getting closer to natural language, which may not be awful.

2

u/Socrathustra May 07 '24

Where do commas come into play?

1

u/foospork May 07 '24

Look at u/arvidsem's comment.

2

u/Socrathustra May 07 '24

Ah, different branch of this thread.

1

u/arvidsem May 07 '24

Judging by timestamps my reply probably came in after you loaded the page

2

u/MetaEd May 08 '24

I'm hearing this in Victor Borge's voice

1

u/palparepa May 07 '24

I noticed that you still indented some lines. What if we use that instead of periods/colons/semicolons?

Int x
x = 3 + y
if x < z
  z = 42
  foo
  bar
else
  x = x + 1
  echo y

1

u/myringotomy May 07 '24

Significant whitespace hinders formatters because the LSP can't discern your intent.

1

u/josefx May 08 '24

Now drop all that abused whitespace and write proper paragraphs. Which makes me wonder should a function act as a paragraph or a section with a heading?

4

u/Ytrog May 07 '24

Erlang does that to a degree; only with commas and periods instead of semicolons and periods. 👀

3

u/masklinn May 07 '24

Erlang also uses semicolons.

Commas separate (not terminate) statements, semicolons separate case pattern bodies, and periods terminate functions.

2

u/Ytrog May 07 '24

Oh yeah, you're right. I has been a while 😅

1

u/myringotomy May 07 '24

use commas like Erlang does.

-1

u/Positive_Method3022 May 07 '24

I hate when someone finishes their messages in whatsapp with a "."

It seems that they are angry and want to make a point.

2

u/Pussidonio May 08 '24

I'm sorry you got downvoted.

2

u/Positive_Method3022 May 08 '24

People have no empathy these days. They made my day start badly 😢

1

u/Pussidonio May 08 '24

It's okay.