[swift-evolution] Brace syntax

Michael Buckley michael at buckleyisms.com
Sun Dec 20 12:59:09 CST 2015


On Sun, Dec 20, 2015 at 8:12 AM, Alexander Regueiro via swift-evolution <
swift-evolution at swift.org> wrote:

>
> There have also been a few votes against removing braces, but these seem
> to be reactionary. Could any of you perhaps clarify why you want to keep
> them, or is it just the “change is bad” stance?
>

Having written a lot of Python code over the last seven years, I definitely
prefer braces to significant whitespace, not because I enjoy looking at
them or I want to indent my code incorrectly, but because they help my
indent my code correctly and keep it correctly indented. The problems I'll
describe below are all problems I have had in the real world when working
with Python code.

Python suffers from the famous tab-spaces problem with significant
indentation, which Guido listed is one of this "Python Regrets" in 2002. In
short, Python considers tab characters to be worth 8 space characters, but
very few if any people have their editor set to tab = 8 spaces. It actually
doesn't matter what value you assign to tab. Sone people will have tab = 2
spaces, others 4, and I've even worked with programmers who have it set to
5. The only defense against this is for everyone on a project to stick with
either tabs or spaces, and this is why most Python programmers use spaces.
But all it takes is one misconfigured text editor to introduce tabs into
the project. They likely won't be caught by code review, and they may not
immediately cause an error. But later on, they'll end up causing
frustrating, difficult-to-diagnose errors. You also need to be paranoid
about integrating code from other projects, because they might use tabs.

You could try to prevent this is Swift by banning the use of either tabs or
spaces in indentation. But this would put you in the middle of a holy war
that has been going on for decades, and would be a huge distraction. Not
only would you anger those who like either tabs or spaces, but you would
anger those who use tabs for semantic indentation, spaces for alignment.

The other classic problem, though not as common as it once was, is that
HTML doesn't preserve multiple spaces in a row by default. Not a problem
when you put the code in preformatted blocks, but there are HTML contexts
that will still mangle your code unexpectedly. For example, some Web mail
systems out there will display plan-text emails using HTML.

My personal pet peeve with significant whitespace is that it makes
refactoring code more tedious. Let's say I want to move an if statement out
of a loop. I select the statement, cut it, move my text cursor out of the
loop to where I want to insert it, make sure I'm indented at the level I
want to be, and paste. The first line gets pasted with the proper
indentation. All the subsequent lines get pasted with their original
indentation, which I now have to fix.

Which brings me to the reason why I like braces: They allow text editors to
handle the indentation for me. I don't have to worry about it. In the above
example, I can just paste the code, and all the lines are indented
properly. If I get sent some code in an HTML context that mangles
indentation, or code that contains tabs, I can paste it into my text
editor, and the editor will indent it properly and remove all the tabs.

Critics of tabs tend to focus on the fact that they don't express anything
not already expressed by indentation. But as I've explained here, that's
not quite true. They provide enough context to text editors to
automatically format your code for you. Without them, text editors cannot
perform certain formatting operations.

Additionally, I have heard tell that one of the original inspirations for
Python's significant whitespace code were C bugs caused by programmers who
omitted braces and relied on indentation for if statements. In other words,
code like the goto fail bug. Swift already takes care of this by making the
curly braces mandatory in if statements, so Swift code will not fall victim
to the class of bugs that inspired significant whitespace.

Python on the other hand, is vulnerable to this kind of bug due to
unintentional outdenting. Maybe a programmer, when moving a block, missed
intending the last line of the block. Perhaps an errant finger hit the
delete key, outdenting the last line of an if statement. Python will now
execute this line unconditionally. The equivalent error in braces-based
languages, accidentally deleting the closing brace, will just cause the
code to not compile.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151220/315b2a96/attachment.html>


More information about the swift-evolution mailing list