[Swift-devel] Code style

Mihael Hategan hategan at mcs.anl.gov
Tue May 14 13:50:57 CDT 2013


On Tue, 2013-05-14 at 13:24 -0500, David Kelly wrote:
> Hello, 
> 
> 
> The topic of code style for internal Swift code came up this morning.
> On the swift-devel page, there is a document that describes how code
> should be formatted:
> https://sites.google.com/site/swiftdevel/internals/code-style. I think
> Justin wrote this document? I'm not sure we've ever discussed it much.
> Do people agree with these suggestions? Should we adopt this as our
> standard? Is it worth it go back to old code and reformat it? 

Mostly. I don't much agree with the 70 character lines. I think the age
of 80 column terminals has been gone for a while, and that limit tends
to make code unreadable since logical units span too many lines and are
split in the weirdest of places.

So I'd vote for something more reasonable, such as 110.

I'd like to add (Oracle also has most of these):
- always using brackets (even for single-instruction then/else). It
delimits the scope of conditions and allows for easily adding things to
branches. 
- use spaces before and after operators (e.g. 3 + 2 instead of 3+2).
- else, catch on next line. Most of the code is like that already. e.g.:
   if (condition) {
   }
   else {
   }
- sufficiently indent subsequent parts of a line-split thing to indicate
subordination to the beginning of the thing as well as distinguish from
other sub-ordinate things. Example
   private void method(int x)
           throws Exception {
       // code
   }
  In the above example it is very quickly clear that the throws clause
belongs to method and that it's not part of the code. 

   The not so good choices (IMO) are:

   private void method(int x)
   throws Exception {
       // code
   }
   (hard to tell without reading that the throws clause is not a method
since it's on the same level as method declarations)

  private void method(int x)
       throws Exception {
       // code
   }
   (hard to tell the throws clause is not part of the code unless you
scan to the end of the line and spot the "{")
- do indent cases in a switch statement.

Mihael

> 
> 
> Another option may be something like the Oracle Java coding standard: http://www.oracle.com/technetwork/java/codeconvtoc-136057.html. 
> 
> 
> David 
> 
> Hello,
> 
> 
> The topic of code style for internal Swift code came up this morning.
> On the swift-devel page, there is a document that describes how code
> should be formatted:
> https://sites.google.com/site/swiftdevel/internals/code-style. I think
> Justin wrote this document? I'm not sure we've ever discussed it much.
> Do people agree with these suggestions? Should we adopt this as our
> standard? Is it worth it go back to old code and reformat it?
> 
> 
> Another option may be something like the Oracle Java coding
> standard: http://www.oracle.com/technetwork/java/codeconvtoc-136057.html.
> 
> 
> David
> 
> 
> _______________________________________________
> Swift-devel mailing list
> Swift-devel at ci.uchicago.edu
> https://lists.ci.uchicago.edu/cgi-bin/mailman/listinfo/swift-devel





More information about the Swift-devel mailing list