I usualy write my code in C and it’s my habit to end statements whit semicolon.
I know it’s optional but I didn’t see anyone that uses semicolons in this form.
Is there any pros or cons to use them ? If it’s just a optional thing, why don’t you guys use them ?
Just don’t use them, it’s a bad habit. Semicolons are only needed if you are writing several statements on a single line.
Why it’s a bad habit ?
The way I see it semicolons are optional because they’re not needed. If you use them out of habit, but not using them for a specific reason, I’d argue it’s a bad habit because it makes the code less readable. Please don’t hurt me!
One of the reasons I like Lua is because it’s so abstracted and minimal. It feels like, compared to other languages, you are writing less code but getting the same work done. It’s a matter of taste.
As for semi colons, I’ve used Lua for six years or so and I haven’t once come across a good use of semi colons. So while it’s safe to keep them, why not live a little and cast off the chains of other more strict languages?
Possibly so you don’t completely fall out of the habit if you’re planning to return to C in the future?
That is true, but I have no such desires at the moment. I’ll probably ditch Lua only if I find a language more abstracted still.
I never have a situation when I would need semicolon in Lua, but you can see on stack overflow, that there could be a need for that, when writing in one line to avoid ambiguity - in that case pros are obvious, but you can avoid it by simply writing in multiple lines, so I don’t see any sense of using them.
And in daily job I use C++ and still have a proper habit there.
+1 please don’t use ; especially when contributing to public code. I’ve never seen a general use where it is actually better than simply putting code on their own lines. A line for each block of code is generally easier to read, clearer, and prettier.
It’s not even needed when you have multiple statements on a single line! Both of these work equally well:
for i=1,10 do print("foo", i) for j=i,0,-1 do print("bar", j) end end for i=1,10 do print("foo", i) for j=i,0,-1 do print("bar", j) end end
There is an explanation of the need for them in statements in a single line, when you use parentheses to switch between two possible function calls, but again, this is a case forced to be in a single line, but it’s always better to just use multiple lines.
I constantly code in Lua, C(++/Obj/#), Java, Python, a little bit of Go and I have no problem switching to different syntax. So the habit is overrated and even harmful (it hurt my eyes most when I saw Lua code written in Java style). You should always try to code in the way it’s meant to be in the particular language.