[swift-evolution] [Idea] Wrap switch cases in curly braces

Dennis De Mars demars at fractaldomains.com
Sun Jul 10 15:37:34 CDT 2016


I don’t like this idea at all. The current switch syntax is really clean, one of the nicest parts of Swift, and this would really turn it into something messy.

I’ll make a possibly controversial statement here: one of the worst aspects of C syntax, which is unfortunately perpetuated by many modern languages, Swift included, is the use of curly braces everywhere to demarcate every kind of block: every control structure, every data structure and every function body.

This leads to a proliferation of nested braces which all have to be placed correctly in order for the code to be correct. Of course, we all use indentation to help manage this, but I think we all know that once the closing brace is sufficiently far from the opening brace, it becomes difficult to tell which brace matches which even with indentation. I think I spend a significant amount of my development time just eyeballing those closing braces. Of course, we also have editor features to help match them up but relying on such editor features might be an indication of a flaw in the language. At any rate, it impedes readability of the code, editor or no editor.

Not having the braces for each case is, to me, analogous to the way Swift removed the outermost parenthesis in the if statement conditional part. When you have a complex conditional expression with nested parentheses, removing that unnecessary outermost pair really improves readability (and reduces possibility of error). This can be done because the outermost parentheses aren’t really necessary to demarcate the boundaries of the expression.

Similarly, the case keywords in the switch statement sufficiently demarcate the extent of the statement block; it is unnecessary to toss in an extra pair of these brace characters that may already be heavily used in the statement block itself.

I think the extra burden on readability (and writability) of having the extra pair of nested braces is not justified by the desire for consistency. If consistency is so important, then rather than detracting from the quality of the switch statement by adding the braces, why don’t we improve the quality of the rest of the language by getting rid of some of those braces in the other constructs that use them! (Note: I don’t really expect that to happen…)

- Dennis D.


More information about the swift-evolution mailing list