ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.
Apache License 2.0
27.51k stars 5.99k forks source link

Brackets formatting #3279

Open jjatie opened 6 years ago

jjatie commented 6 years ago

Our (undocumented) style guide for brackets is to write the opening bracket on a new line like so:

func myFunction()
{
    // Draw stuff
}

This is fine until we get into trailing closure syntax. How are we supposed to write it? We have options

// 1
myFunctionWithTrailingClosure { arg 1, arg2 in
    // Do stuff
}

// 2
myFunctionWithTrailingClosure
{ arg 1, arg2 in
    // Do stuff
}

// 3
myFunctionWithTrailingClosure
{
    arg 1, arg2 in
    // Do stuff
}

Option 1 is the only way I have ever seen this written and is the only clear way to write trailing closures, but it goes against our code style. I am strongly against the opening bracket on a new line because while it does make method and type declarations slight easier to read by forcing a new line, it makes chaining (if-elseif-elseif...) much more difficult to identify and read. There needs to be a final decision made on this.

Given most of the library was written using Swift, but without taking advantage of many Swift features I'd argue that the opening bracket on a new line comes from more crowded languages (e.g. C, Java), without consideration for Swift's cleaner syntax. As a result, we should move to the more standard (as seen throughout the Swift stdlib and foundation libraries, the Swift book, many popular iOS websites such as RayWenderlich, all of Apple's sample code, and even autocomplete) style of

func myFunction() {
    // Draw stuff
}
liuxuan30 commented 6 years ago

need to let @danielgindi @petester42 have a look and talk. code style is defined by @danielgindi

My opinion: option 3 seems good.

I also found "PaintCode Sketch plugin" supports Swift 4 code style to put { a single line.

pmairoldi commented 6 years ago

I really don’t care it’s just formatting. I’ve been using tools like https://prettier.io for a little bit and it makes the whole “how to format” debate a nonissue. Maybe we could include the swift version of prettier if it’s stable enough.

liuxuan30 commented 6 years ago

we just need to decide where to put { and come to an agreement. @danielgindi uses this code style from beginning, and we all followed that since then.

if we couldn't agree, then we have to have @danielgindi answer this for closures.

pmairoldi commented 6 years ago

we can just use tools like https://github.com/nicklockwood/SwiftFormat or https://github.com/prettier/plugin-swift to address this "problem"