Proposal: Make braces optional much like semicolons.


(Amir Michail) #1

// 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.


(Bart Whiteley) #2

-1. Please no. The fact that Swift requires curly braces even in
situations where C does not is a great feature. I still remember the
cheer that erupted from the '14 WWDC crowd when mandatory braces were
announced -- and rightfully so. Many bugs are prevented by mandatory
braces.

···

On Sun, Dec 20, 2015 at 8:17 AM, Amir Michail via swift-evolution <swift-evolution@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)


#3

There was a thread for this posted yesterday:

https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151214/003631.html

Stephen

···

On Dec 20, 2015, at 10:17 AM, Amir Michail via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Amir Michail) #4

// 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)

-1. Please no. The fact that Swift requires curly braces even in
situations where C does not is a great feature. I still remember the
cheer that erupted from the '14 WWDC crowd when mandatory braces were
announced -- and rightfully so. Many bugs are prevented by mandatory
braces.

The reason they cheered is because omitting braces for one line scopes is a source of bugs. Braces should ALWAYS be required for one line scopes, even if indentation allows you to omit braces in other contexts.

···

On Dec 20, 2015, at 11:44 AM, Bart Whiteley via swift-evolution <swift-evolution@swift.org> wrote:
On Sun, Dec 20, 2015 at 8:17 AM, Amir Michail via swift-evolution > <swift-evolution@swift.org> wrote:

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Patrick Gili) #5

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Craig Cruden) #6

Although I am not for braces being optional and using tab / indentation for scope, the forcing of using braces for one line scopes is overkill IMHO.

If there is scope that is multiline though I would probably include scope on all of them (there is one multi-line scope in that example).

So +1 / -1 at the same time (which I guess sums out to 0) :stuck_out_tongue:

···

On 2015-12-20, at 23:44:55, Bart Whiteley via swift-evolution <swift-evolution@swift.org> wrote:

On Sun, Dec 20, 2015 at 8:17 AM, Amir Michail via swift-evolution > <swift-evolution@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)

-1. Please no. The fact that Swift requires curly braces even in
situations where C does not is a great feature. I still remember the
cheer that erupted from the '14 WWDC crowd when mandatory braces were
announced -- and rightfully so. Many bugs are prevented by mandatory
braces.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Charles Constant) #7

I'm quite sure if Chris had announced "significant whitespace" instead of
"braces", the other half of the audience would have cheered :slight_smile:

···

On Sun, Dec 20, 2015 at 9:05 AM, Amir Michail via swift-evolution < swift-evolution@swift.org> wrote:

> On Dec 20, 2015, at 11:44 AM, Bart Whiteley via swift-evolution < > swift-evolution@swift.org> wrote:
>
> On Sun, Dec 20, 2015 at 8:17 AM, Amir Michail via swift-evolution > > <swift-evolution@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)
>>
>
> -1. Please no. The fact that Swift requires curly braces even in
> situations where C does not is a great feature. I still remember the
> cheer that erupted from the '14 WWDC crowd when mandatory braces were
> announced -- and rightfully so. Many bugs are prevented by mandatory
> braces.

The reason they cheered is because omitting braces for one line scopes is
a source of bugs. Braces should ALWAYS be required for one line scopes,
even if indentation allows you to omit braces in other contexts.

> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Amir Michail) #8

There was a thread for this posted yesterday:

https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151214/003631.html

Much of that discussion doesn’t consider the possibility of making braces OPTIONAL as shown in the examples.

···

On Dec 20, 2015, at 10:19 AM, Stephen Celis <stephen.celis@gmail.com> wrote:

Stephen

On Dec 20, 2015, at 10:17 AM, Amir Michail via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Amir Michail) #9

Although I am not for braces being optional and using tab / indentation for scope, the forcing of using braces for one line scopes is overkill IMHO.

I absolutely support the use of braces for one line scopes. This prevents bugs.

When making braces optional via indentation, I would still want them to be required for one line scopes.

···

On Dec 20, 2015, at 11:52 AM, Craig Cruden via swift-evolution <swift-evolution@swift.org> wrote:

If there is scope that is multiline though I would probably include scope on all of them (there is one multi-line scope in that example).

So +1 / -1 at the same time (which I guess sums out to 0) :stuck_out_tongue:

On 2015-12-20, at 23:44:55, Bart Whiteley via swift-evolution <swift-evolution@swift.org> wrote:

On Sun, Dec 20, 2015 at 8:17 AM, Amir Michail via swift-evolution >> <swift-evolution@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)

-1. Please no. The fact that Swift requires curly braces even in
situations where C does not is a great feature. I still remember the
cheer that erupted from the '14 WWDC crowd when mandatory braces were
announced -- and rightfully so. Many bugs are prevented by mandatory
braces.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Amir Michail) #10

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.

Swift already supports different programming paradigms (e.g., functional, OOP, etc.) which is a much bigger deal than the optional brace issue.

···

On Dec 20, 2015, at 12:55 PM, Patrick Gili <gili.patrick.r@gili-labs.com> wrote:

Cheers,
-Patrick

On Dec 20, 2015, at 10:17 AM, Amir Michail via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


#11

Sure. This is a point you could have made in that discussion. I think in general the preference is to reply to an existing, recent thread when the topic of discussion is so similar. Starting a new thread adds noise and tends to duplicate overlapping discussion.

Stephen

···

On Dec 20, 2015, at 10:54 AM, Amir Michail <a.michail@me.com> wrote:

Much of that discussion doesn’t consider the possibility of making braces OPTIONAL as shown in the examples.