views:

914

answers:

13

It is interesting that some languages do not use semicolons and braces, even though their predecessors had them. Personally, it makes me nervous to write code in Python because of this. Semicolons are also missing from Google's GO language, although the lexer uses a rule to insert semicolons automatically as it scans.

Why do some languages not use semicolons and braces?

+10  A: 

The designers for those languages presumably believe that the braces and semi-colons are needless cruft, when line continuations can (usually) be detected due to a statement not being complete, and blocks can be detected by whitespace.

Personally it makes me nervous too, but then the lack of checked exceptions in C# had the same effect on me for a while... I suspect that when you get used to such a scheme, it can improve readability (which is the point). It does mean you need to be more careful with whitespace, of course.

Jon Skeet
Since starting to write Python, I've started getting nervous when I see single-line `if` statements without braces. I've had actual problems because of missing braces. I've yet to have a problem with whitespace when using Python. Obviously, YMMV.
Dominic Rodger
@Dominic: I always use braces in C# and Java even for single-line if statements :)
Jon Skeet
@Dominic IMHO code is more readable with braces even if there is one line in if statement, but it is just my opinion.
Incognito
@Jon Skeet of course it would be great to have checked exceptions in C#. But it is used as I know only in method descriptions but not in the whole code where you used to have braces :)
Incognito
@Jon, then I'll reluctantly think of you as a respectable programmer ;)
Dominic Rodger
@Incognito: I don't think it would be great to have checked exceptions in C# at all. I can't stand them when I have to work in Java. I can't think of a single error I've *actually seen* in C# code which would have been saved by checked exceptions.
Jon Skeet
@Jon I agree that it matters of how you used to it. But taking into consideration that many developers have roots coming from C or almost majority of the languages are using braces why there is a such extreme need in python to remove them which is just unusual for developers having background from other languages. There must be any solid advantage for that.
Incognito
@Jon also when we check C# design we can easily see that it is targeted for Java and C++ developers. So migration from those languages will be smooth. Even the namespace names are very similar with Java. It is an advantage, but in Python we can see the opposite.
Incognito
@Incognito: If you're coming to Python from Java or C, you've got *much* bigger things to get used to than braces and semi-colons. Don't think of it as Python *removing* semi-colons - language design should be an *additive* process, adding features/syntax where it makes sense. The Python designers clearly decided that braces didn't add enough benefit.
Jon Skeet
While I think from the perspective of readability python's whitespace scheme is fine, it can cause grief when you are cutting and pasting code at different indentation levels. I have seen several bugs resulting from this. Of course, that was several years ago using emacs. Perhaps now there are smart Python editors that prevent you from making that kind of mistake?
Christopher Barber
@Jon and why you think that checked exceptions are useless?If you personally haven't used them it doesn't mean that they are useless.It is good while working with other developers code. You can know what exceptions can be raised while method call... There are also some advantages.
@MigNix: What makes you think that I haven't used them? I code in Java at Google all day long, and have been in and out of Java since 1995... and I find them irritating. There are occasional benefits, but I find they're generally outweighted by the disadvantages.
Jon Skeet
@Jon Saying you didn't used them I meant that you don't see any use in them. Anyway I believe it is quite subjective. As for me it is another way to see it as conract between caller and method. Caller of the method will know about all possible exceptions to be handled.
@MigNix: Except that they won't know which runtime exceptions might occur - and in my experience most of the exceptions which *are* declared to be thrown *can't* actually be properly handled by the caller... they're just propagated up the stack, sometimes being transformed into other exceptions along the way. Yes, it's somewhat subjective - but having extensively used similar languages both with and without checked exceptions (C# and Java) I'm in a pretty good position to give a balanced viewpoint.
Jon Skeet
A: 

I really don't understand why you ask. You hate writing Python code? Well, don't! Nobody has canceled C/C++/etc.

Sorantis
No of course there are a lot of really great thing is Python, but when you used to work with C-like languages almost always you type a code with ';' or {} and go for backspace :)
Incognito
Personally I agree with you. But when it comes to languages you either accept the rules or search for alternatives. It makes no sense to complain about it.
Sorantis
@Sorantis of course I have to accept the rules and it is not the matter of complaining. I would like to know is there any reason for that.
Incognito
The reason might be a design decision, taken by language creators. As simple as that.
Sorantis
This would be better suited as a comment instead of an answer.
Joachim Sauer
Saying that you don't understand the motivation for a question isn't useful as an answer to that question. If you don't understand the motivation and that's important to you, make a comment to the original question. Answers should actually attempt to answer the question.
Bryan Oakley
+4  A: 

"Syntactic sugar causes cancer of the semicolon." Alan Perlis

Andrey
+15  A: 

You can argue that when you use semicolons and braces, you still indent your code with whitespace and new lines - for readability reasons. Therefore those delimiters can be considered redundant in this sense.

Daniel Vassallo
yes, and anywhere you have redundancy you also have a potential source of errors if the compiler reads the braces and the humans read the indentation.
Duncan
Here is interesting article http://schlitt.info/opensource/blog/0720_python_good_bad_evil_01_missing_braces.html
Incognito
+3  A: 

Some people think that semicolons and curly braces are not exactly human-readable text. I personally favor the Pascal-style begin end blocks, it seems more natural and easier to understand in terms of sheer meaning, even for a non-initiate in programming languages: "See, is says begin, then some stuff, then end, so it must be something that begins here and ends over there, some sort of block, huh...". Nevertheless, semicolons and brackets are usually easier to parse, so that's why it's easier to use them instead of indentation or other constructs; designers that consider the language easier to understand without them, but easier to parse with them, apply tricks like the one you mentioned: the lexer uses a rule to insert semicolons automatically as it scans.

luvieere
Actually, it's trivial to parse begin and end. In fact Pascal was designed as a language that was very easy to parse, so that students could write simple compilers for it.
anon
The downside to begin/end is that they take a little bit longer for humans to scan them and distinguish them from other keywords and variable names. Where non-letter characters like '{' stand out more.
Christopher Barber
+21  A: 

Every programming language must have some way of distinguishing the end of a statement, function call parameter lists or a block of code from the next one.

Some languages use ; and {} (C, Java)

Some languages rely on known sizes of parameter lists (x86 assembly code)

Some use parentheses to form s-expression (Lisp, Clojure)

Some use whitespace (Python)

Some use special keywords like begin .... end (Pascal, Delphi)

So basically this is mostly just a language design choice. There is always some equivalent of ; or {}, even if it doesn't look the same at first glance...

mikera
As I know Lisp was the first with such syntax, but python designers go even ahead and removed () also.
Incognito
Haskell doesn't rely on known sizes of parameter lists. In haskell you can either use `;` and `{}` or significant whitespace.
sepp2k
@sepp2k you're right of course that Haskell does use some semicolons - in this instance I was referring to Haskell's rules for function application to disambiguate chained function calls without the need for explicit bracketing, which I tend to think of as the functional equivalent of "statements". Edited to make a bit clearer.
mikera
@mikera: Still not true. For one thing all functions in haskell are unary. But even ignoring that for a moment, `id`, `id id` and `id id id` are all valid haskell expressions (that also happen to all evaluate to the same result).
sepp2k
mikera
@mikera: `addup x y = x + y` defines a function of type `Num a => a -> a -> a`. So does `addup x = \y -> x+y`. As a matter of fact there is no way for a user of the addup function to find out whether addup has been defined the first or the second way. For example you can do `add42 = addup 42` no matter how addup has been defined. f x y and (f x) y are always equivalent. Basically chaining function applications is haskell's way of emulating multiple arguments. Haskell does not use arity to determine where an expression ends, it uses indendation or semicolons (or `in` or other syntax constructs)
sepp2k
@sepp2k: OK I see what you mean. I would still describe that as a binary function however! Have edited Haskell out of the original post to avoid confusing people either way :-)
mikera
+7  A: 

Two reasons: There are so many different ways to put braces around code blocks (see indent styles) that reading/parsing code written by others can be quite hard. Python code, on the other hand, always looks similar, and the indentation level gives a very clear visual clue for the structure of the code. As a side effect, it forces you to keep your code structure simple since deep nesting makes your code vanish off the right side of the screen :)

As for the semicolons - I've been bitten often enough by for(i=0;i<=100;i++); errors that I'm glad I'm not falling into the same traps in Python...

Tim Pietzcker
Yes it enforcing a common coding style and it is good in many programming communities. But developers are different and sometimes when you got code with deep indentation it really becomes terrible.
Incognito
It does, and you can spot terrible code (at least as far as code structure is concerned) a mile off.
Tim Pietzcker
+8  A: 

We have been using indentation to indicate statement groupings as a readability aid for a long time. This occasionally causes problems when the indentation and the actual statement groupings (indicated by {};, begin/end, whatever) are in conflict; we read one meaning, but the code actually says something else.

Python took the simplifying approach. If we find indentation a help in clarity of expression, why not make it the way the language itself determines groupings. When we write code, we express intent to other readers, so looking at what writing gurus say is often useful:

A sentence should contain no unnecessary words, a paragraph no unnecessary sentences, for the same reason that a drawing should have no unnecessary lines and a machine no unnecessary parts. ~William Strunk, Jr., The Elements of Style, 1918

So, maybe a programming language should have no unnecessary syntax elements...??

Pierce
It is matter of taste. For someone it is unnecessary syntax element for the other it is very useful way to structure the code.
Incognito
+3  A: 

Semicolons and {} have semantic meanings (variable lifetime, mostly) as well as just syntax. In C++ I've written code that looked like

{
    lua_table tab;
    {
        lua_string str;
    }
}

They were of great use because using the Lua stack from C++ sucks terribly.

DeadMG
In Lua you have exactly the same with `do ... end` blocks. In a brace-free language!
kaizer.se
Lack of braces does **not** necessarily mean lack of blocks. Python is the perfect example.
Joachim Sauer
but python doesn't have scoped anonymous blocks.
kaizer.se
+3  A: 

For some people, semicolons and braces look like noise that makes difficult reading the 'actual' code.

As you can have parsers to recognize blocks either based on punctuations or in indentation (no technical issue involved) the use of one or the other alternative is just a question of the programmer preference.

My impression is that this preference could be mainly due to the previous programmer background.

joaquin
+4  A: 

Using delimiters like semicolons and braces, or not, is just a matter of taste. In practical terms, compilers can work without them, so, why use them in modern programming languages? As I said, is a matter of taste, and... a long-time established de-facto syntax that ressembles C. It is difficult to fight against that.

There is one field in which braces and semicolons are useful: code generation. When you generate code that is expected to be compiled/interpreted in a kind of reflective behaviour, it is normally more comfortable to write braces (in, say, just one, single line) than to write the structure needed by a programming language such as Python, for example. Think of a function with a couple of unnested loops. You would have to keep track of the number of tabs needed at each line.

Baltasarq
+5  A: 

Is there any reason why some languages do not use semicolons and braces?

Some designers believe that "syntactic noise" such as semicolons and braces distract the reader from the code. There are various ways to eliminate them:

  • Python and Haskell use significant indentation.

  • Clu and Lua use very carefully engineered grammars.

  • Standard ML uses keywords to introduce each construct plus let-bindings, which eliminate the need for most semicolons while also providing a handy way to declare local variables.

  • The Bourne shells use significant newlines to eliminate semicolons

  • Scheme uses an extremely regular syntax which in which the only syntactic markers are parentheses. Longtime Schemers like Olin Shivers claim that after a few weeks, your brain adjusts and you no longer see the parentheses.

The fact that there are so many designs, with so much variation, suggest that many language designers view semicolons and braces as syntactic noise to be eliminated if possible. By eliminating syntactic noise, designers make programs easier to read and understand all at once. and many programmers feel more productive, as if the signal-to-noise ratio has improved and they are channeling their code more clearly. (I won't say they're right and I won't say they're wrong, but I will say that has language-design decisions go, this one is pretty easy to defend.)

So is there a reason why language designers do use semicolons and braces?

Many of the modern semi-colon-and-brace languages are designed explicitly, or in some cases not so explicitly, to appeal to C programmers. After all, if it has semicolons and braces, it must be easy to learn. Right?

Norman Ramsey
Personally my argument is that it would be easier for other developers who has experience in such languages like Java, C#, C, C++, Perl, PHP ... to code in Python having braces which are usual for them. Of course I am not trying to say that this is a huge problem and that the absence of braces are a blocking point, but percentage of the developers on languages mentioned above is a solid argument to be considered in language design. Also IMHO braces make code visibly more structured. And the main question is that there must be reasons in order not to consider all this...
Incognito
@Incognito, I think you're exactly right that curlies and braces are perceived to enhance learning. I've edit my answer to note that eliminating syntactic noise makes programs easier to read and understand all at once and makes programmers feel more productive.
Norman Ramsey
+4  A: 

Is there any reason they should use them?

sovanesyan
We have discussed some reasons here. Please have a look at them.
Incognito