[swift-evolution] Proposal: Make braces optional much like semicolons.

Patrick Gili gili.patrick.r at gili-labs.com
Sun Dec 20 11:55:09 CST 2015


I would not like to see braces made optional. First, I think this reduces the readability of the code. While readability might be somewhat subjective, compare these two blocks of code:

// With braces             // Without braces
for i in 0..<10 {          for i in 0..<10
    for j in 0..<5 {           for j in 0..<5
        if i %2 == 0 {             if i %2 == 0
            print(i+j)                  print(i+j)
        }                          print(i*j)
        print(i*j)
    }
}

IMHO, it is easier to see that "print(i*j) is part of the block belonging to the inner for loop, rather than the if statement, when braces are present.

Second, this is Python's style, which comes at a cost: the grammar is not context free. While this type of grammar is safer, especially for programmers with little to no experience, the imposition on veteran programmers is constraining.

I realize that you're proposing that this be optional. However, this would introduce a new problem. Now you have two distinctly different styles of code out there. IMHO, this further increases the readability issue.

Cheers,
-Patrick

> On Dec 20, 2015, at 10:17 AM, Amir Michail via swift-evolution <swift-evolution at swift.org> wrote:
> 
> // braces are optional here but you could still put them in if you want to
> for i in 0..<10
>    for j in 0..<5
>        if i % 2 == 0
>            print(i+j)
>        print(i*j)
> 
> // braces are necessary because “print" is on the same line as ”if"
> for i in 0..<10
>    for j in 0..<5
>        if i % 2 == 0 { print(i+j) }
>        print(i*j)
> 
> // braces are necessary with incorrect/unconventional indentation
>    for i in 0..<10 {
> for j in 0..<5 {
> if i % 2 == 0 {
> print(i+j)
> }
> print(i*j)
> }
> }
> 
> As for the space vs tab issue, my preference would be to allow only spaces to make braces optional. An alternative would be to use the python 3 indentation rules with respect to spaces and tabs.
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151220/2376de55/attachment.html>


More information about the swift-evolution mailing list