This is why linting and auto-format on save exists in IDEs. Don’t make things harder on yourself.
import " " as ;
I;don’t;think;that;helps
I was brought up on C, did a module of Java at uni, and am doing an algorithms course which is python heavy.
My other half - who’s quite handy with Python - looks in sheer horror at my code which is littered with semicolons.
I was stumped for half an hour figuring out why the Python interpreter was bouncing an error before it had even reached the main program logic… turns out a { before the block of code royally ruins the interpreter’s day.
Still, I live and learn.
can I interest you in some delicious linters
I was taught python by my school but I would rather write in other languages but the difference in formatting still gets me after years
That’s actually me removing semicolons from Python.
One of the nice things Kotlin did for the JVM is add a semicolonless option. Semicolons are entirely optional in 99% of code, we just use them because older languages decided to use them.
The AST knows where a statement ends with or without a semicolon, that’s why you get those “you forgot a semicolon” errors instead of “unexpected character” errors.
Some languages actually use semicolons. Rust does, for instance; an expression without a semicolon can be used as a return value, therefore adding value to the presence of the semicolon character. That said, with how few expressions are return values, I do wonder why semicolons are the default rather than adding a special character to indicate return values.
I know computer programmers are deadly afraid of change, so I guess they’ll stick around at the end of every single line for years to come. In Java, semicolons are nothing but a hassle these days, though.
That said, with how few expressions are return values, I do wonder why semicolons are the default rather than adding a special character to indicate return values.
you mean like return/break/etc.?
because Rust was designed to remind you of functional programming despite not being very functional, and because semicolons allow way better syntax rules in Rust and are generally pretty vital for good, readable lowish-level code. it also allows Rust programmers to use newlines/indents and stuff to pretty up their code a lot without littering it with random
\
and|>
andbegin
end
and such everywhere, which, given how dense Rust code can be and how much it uses iterators and weird trait magic, is a big plus for readabilityAs in yield/return, yes.
I don’t see why you would need semicolons in any of those cases. The only languages I know that have you put a backslash at the end of the line to continue am expression are already using semicolons, while Python allows splitting expressions across lines without them.
I don’t work on any widely-used languages (I’ve made my own but not anything important) but I do think the designers of Zig and Rust have very good reasons for using semicolons – I read some reasons from the Rust devs themselves somewhere but I can’t remember them other than it vaguely being about how Rust is expression-based and intended to be lightweight and how whitespace significance can create confusion around how to read and write certain things and bla bla bla…
but my personal opinion, what I generally I would imagine it’s for other than readability, is because the code can look a lot cleaner when an expression returned from a block is just the expression, and not expression plus some token like
return
. It’s especially nice in long closures or extremely short and simple blocks. I would rather consistently have to write expressions broadly likelet a = { b + c };
rather thanlet a = { return b + c }
. The semicolon has significance as a “result discarder” so expressions can be the default, so it’s on the surface a lot more functional-friendly.Also this is more specific but I hate the way WS languages generally handle quotes
For bonus points take their code and completely go wild with utterly random indentation and line breaks, just to drive the point home. 😈
Pfff just randomly replace tabs with 4 spaces and see the world burn
4 spaces is the preferred pythons is happy with either but the standard is 4 spaces. Vscodium ensures that.
Your editor is happy with either, but is it happy with both?
My editor will take either and convert it to 4 spaces. Python can use spaces or tabs but not both in the same file.
Buahahahah I know what to do next commit
Its the python equivalent of a Greek question mark
Plot twist https://programming.dev/post/12825985
I have a plugin in VSCode to remind me not to forget the semicolons
Folks acting like Python is the only language without semicolons. 😏
I got tripped up so many time in the last 3 weeks using PHP after years in ML family languages. I am already newlining & it’d be poor style to put more than one statement on a line so what is the point of these semicolons?
In some languages
a newline does not
necessarily indicate
the end of a statement.In others, sometimes it could, but would leave things ambiguous
as to whether the statement was ended or not.And so, punctuation is necessary.
But newlines + indentation are supported by a lot of languages & when it is, it’s easier to read since the prevailing convention is already to newline, then in indent. When you follow the usual coding styles or autoformatted & removed the semicolons, you’ve gained nothing for readability & added noise. I much prefer the languages that take this convention & bake it in so you don’t have to have that that visual noise—and in these languages, I never felt the parsing rules were ambiguous.
it’d be poor style to put more than one statement on a line
Unlike Python, most languages do not endorse a specific concept of style. You’re free to dabble in all the bad style choices you like, on the off chance that once in a blue moon they prove to be situationally useful.
Why haven’t custom parsers become more of a thing? All the compiler or interpreter really needs is a valid parse tree. You could even have some kind of special command or directive to switch styles, if a section would be really ugly otherwise.
I think that in many languages, semicolons are more of a formality. Javascript has a few weird edge cases that cause very silly bugs when you omit semicolons, but it works fine without them in most cases. Kotlin is mostly semicolonless and I find Kotlin code a lot more readable than equivalent Java code.
It would not be that hard to parse semicolonless code in a language designed for it. The parser knows when a statement or expression is over, even if you split it among multiple lines. Debugging mismatched parentheses would become hell without an intelligent compiler, though.
Language designers just like the explicit end-of-statement character. I’ve seen languages that use a full stop to separate statements and expressions and I honestly don’t understand why we don’t do that in more languages; it makes semantic sense.
However, in the end, it’s not exactly hard to use semicolons, so I don’t see the problem with keeping them around either. Some people get real uppity about semicolons, either in favour or against them, though.
The best languages about really embracing punctuation as you would in English is Prolog & Erlang with their periods, semicolons, & commas.
PythonAmateur programmersHow does one manually add a semicolon? vs. automatically?
Manually would be if it prompted you every time you every time the program inserts one. No, wait…
JS formatters add them if the project requires it