Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Python use of indentation for code scope was initially very polemic and now is considered one of the best language features, because it helps ( almost by forcing us ) to have a consistent style.

Well, I saw this a post where someone posted Java code with ; y {} aligned to the right margin to look more pythonic.

int foo()                                     {
    int sum                                   ;
    bar()                                     ;
    for(int i = 0; i < 100; i++)              {
        sum += i                              ;}
    return sum                                ;}

It was very shocking at first ( as a matter of fact, if I ever see Java code like that in one of my projects I would be scared! ) However, there is something interesting here. Do we need all those braces and semicolons? How would the code would look like without them?

class Person
  int age

  void greet( String a )  
    if( a == "" )  
      out.println("Hello stranger")
      out.printf("Hello %s%n", a )

  int age()  
    return this.age

class Main  
   void main()  
       new Person().greet("")

Looks good to me, but in such small piece of code is hard to appreciate it, and since I don't Python too much, I can't tell by looking at existing libraries if it would be cleaner or not. So I took the first file of a library named: jAlarms I found and this is the result: ( WARNING : the following image may be disturbing for some people )

Obviously it doesn't compile. This would be a compiling version using right aligned {} and ;


What would happen if we could code like this? Would it make things clearer? Would it make it harder?

I see braces, and semicolons as help to the parser and we, as humans have get used to them, but do we really need them?

I guess is hard to tell specially since many mainstream languages do use braces, C, C++, Java, C# JavaScript

Assuming the compiler wouldn't have problems without them, would you use them?

Please comment.

UPDATE By the way I have just remember about this language that does something similar for JavaScript.

share|improve this question

closed as not constructive by Macneil, Mark Trapp, bigown Dec 28 '10 at 23:29

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

I would be happy to use C# syntax without semicolons at least. Semicolons are not for human. It is just for compilers so i hope that this syntax will change if not so i will continue to forget semicolons as i do always. –  Freshblood Dec 27 '10 at 20:16
Or just use Jython :-) –  Paulo Scardine Dec 27 '10 at 21:00
Semicolons are just clutter, same with braces. Professional developers indent their code properly anyway, so why use them anymore? –  Oliver Weiler Dec 27 '10 at 21:05
BTW: python spacing-as-syntax is the worst annoyance in the language. Even with good IDE syntax errors of the "unexpected indent" kind happen too frequently and are annoying as hell. Give me easy-to-read braces anytime. Just providing some perspective :) –  StasM Dec 28 '10 at 4:17
@StasM: No, certainly not. But since most of the countless Python programmers get along without creating lots of errors related to indentation, you should consider whether your problems with it are an inherent flaw of the language. If I started writing significant amounts of code in a language that requires curly braces and semicolons, I would miss of both until I get used to it - but I wouldn't claim that it's impossible to fluently write code in these languages. –  delnan Dec 28 '10 at 18:49

6 Answers 6

The only problem that I see with this is that things that depend on vertical whitespace for proper style formatting would not work. Consider this fluent interface example:

public Collection<Student> findStudents(String name, int age, Gender gender) 
    return em.createNamedQuery("Student.findByNameAgeGender")
             .setParameter("name", name)
             .setParameter("age", age)
             .setParameter("gender", gender)
             .setHint("hintName", "hintValue")

To fix this, the compiler would have to detect the presence of the periods, and backtrack. Which might work for this particular scenario, but long lines of code could not arbitrarily be broken into separate lines anymore, since whitespace is now significant.

share|improve this answer
In Python, we do this with keyword arguments ;) Or the slightly ugly, but acceptable-if-needed alternative: Wrapping the whole expression into parens (lines never end inside parens, brackets or braces). –  delnan Dec 27 '10 at 23:22
@delnan keyword arguments are call chaining are two totally different and not interchangeable concepts, it's just an example... –  StasM Dec 28 '10 at 4:19
In Python you can break long lines by adding backslashes –  Joril Dec 28 '10 at 8:13
@Joril: That... ick... reminds me of BASIC's _ line continuation operator. –  Powerlord Dec 28 '10 at 20:58

The semicolons do seem redundant to me, but I'm not sure how much trouble it would be for compiler writers to get rid of them.

Replacing braces with indentation is a different matter. A good IDE should be able to enter the close brace for you when you type in the open brace. The braces are also redundant if you indent the code to match program structure anyway, but this is actually the good kind of redundancy. As long as you have the braces matched and in the right places, the editor/IDE can re-create the indentation for you if necessary (e.g. after a refactor), but that doesn't work the other way around. Python coders say it's not a problem because they can just re-indent by hand, but I don't know that they've gained anything in compensation other than reducing keystrokes.

If you're interested enough in this issue to post the question, it might be worth your while to spend more time with Python. Pysol might be a good place to start.

share|improve this answer
Using semicolons as statement delimiters may often seem redundant, but on those rare occasions when it's useful to have statements span more than one line, being able to simply add newlines is cleaner than any alternatives I've seen. –  supercat Feb 13 '14 at 22:20

I personally think that, contrary to popular opinion, using indentation instead of explicit delimiters (braces, begin/end keywords, etc.) sucks big time. Good luck with copy/pasting code between different indentation levels and letting the editor automatically re-indent the code. For example:

if some_condition:
  do something
  <paste in some code from elsewehere>

The editor has NO WAY of knowing whether the pasted code should be within the if-block or outside. With braces, you just have to be careful whether you paste the block before or after the closing brace, and the editor can fix up the formatting itself.

I tried to program a bit in Python, and another big problem I found with indentation is that it's extremely impractical to add code in between two (or more) closed indentation levels. In C terms:

if (x) {
  for (...) {
    if (y) {
  // add some new code here

I messed up few times just thanks to lack of explicit delimiters.

In any case: with all those smart editors and IDEs out there, indentation-based syntax solves a non-existing problem, and creates few more additional problems along the way. It's not much better than Fortran's original fixed-column format.

share|improve this answer
I'm sorry, what's the problem with adding code there? ^^; –  Joril Dec 28 '10 at 8:18
The problem is that I get lost in indentation levels without any visual indicators of code grouping. –  zvrba Dec 28 '10 at 12:05
Use a decent editor that allows you to indent/dedent all selected lines. And how often do you copy and paste code that this is a serious annoyance? –  delnan Dec 28 '10 at 18:24
@delnan: Very seldom. It's not been a problem after ten years of Python coding. It's just an excuse. –  Lennart Regebro Dec 28 '10 at 23:03

I say "Yes!". The braces aren't needed. But I'd go one step further and introduce Python colons for clarity:

class Person:
  int age

  void greet( String a ):
    if( a == "" ):
      out.println("Hello stranger")
      out.printf("Hello %s%n", a )

  int age():
    return this.age

class Main:
   void main():
       new Person().greet("")

Very readable! But obviously, this is not to be. Java's syntax was intentionally made to be very much like C/C++ so that programmers would recognize themselves, but not have to deal with C's pointer drawbacks. And that was obviously a good choice, since Java has become a great success.

share|improve this answer
puts on Devil's Advocate hat: Why would you need to add colons? The colons aren't needed. As for clarity, didn't you just argue for the removal of the braces that delimit the start and end of blocks? –  Powerlord Dec 28 '10 at 21:14
Yes, I did. Because the braces add nothing compared to indentation, and worse, they can contradict the indentation. –  Lennart Regebro Dec 28 '10 at 22:59
I fail to see how : adds any amount of clarity. Indentation levels already do that in a crystal clear fashion. If : adds clarity for you, it's because you are already used to it. If Python didn't need them from the start, I suspect your opinion would be different. –  Thomas Eding Nov 22 '13 at 16:45

I think this is a ridiculous change for the sake of change. What is wrong with the braces? What happens with the "find matching brace" shortcut? What is the benefit?

share|improve this answer
As others have said, braces and semicolons are merely clutter. Yes, tools can make them very manageable, but they're still clutter. But then again, Java has more serious problems (from a Python programmer's viewpoint) than syntactic clutter ;) –  delnan Dec 27 '10 at 23:18
what is clutter? –  MK01 Dec 27 '10 at 23:19
Clutter == unnecessary stuff. You don't need "find matching brace" shortcut when there are no braces; you can see the block/scope without such IDE features, when the block/scope is defined by indentation. The benefit is to get rid of unnecessary stuff. Granted, this is a relatively small benefit. –  Joonas Pulakka Dec 28 '10 at 7:27
Yeah, but what is unnecessary? Let's write f(i=0;i<10;i++) instead of "for" and w(n>0) instead of "while". "or" and "hile" are just clutter. Look at all the typing we've saved! It's like people saying untyped languages are better than typed ones because you don't need to type as much. –  MK01 Dec 28 '10 at 19:28
No, that's not clutter. Adding things that has a purpose, including increasing clarity, is not clutter. Braces does not add anything compared to indentation, hence they are clutter. Worse, indentation and braces can contradict each otehr, which is confusing. –  Lennart Regebro Dec 28 '10 at 23:02

You should have suggested this 20 years ago, while Java was still in the development stage. Now, it's far too late to change it.

Even if it was possible to change at this point, Java already has published code conventions. These conventions are the defaults that the two major Java IDEs, Eclipse and NetBeans, enforce. In both their code cleanup tools and any code they generate.

Java Programmers also tend to use whitespace for their own ends. Robert Harvey has already shown you method chaining that uses whitespace to line up code.

It is not uncommon to spread arguments to a constructor or method across multiple lines. You can see this in your first Pastebin (for the InetSocketAddress constructor) on lines 58 and 59.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.