Skip to content

Wrap multi-line function declarations before the last paren #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

mike-at-home
Copy link

@mike-at-home mike-at-home commented May 5, 2025

Summary

This PR updates our Swift formatting rules for multiline function declarations to place the closing parenthesis and return type on the same line, immediately followed by the opening brace. This format aligns with Swift community conventions, improves readability, and results in cleaner diffs during edits and refactors.

(Note: this PR does not address call-site wrapping, which will be addressed in another proposal)


Reasoning

The current style:

public func executeRequest(
  _ request: URLRequest,
  onSuccess: @escaping (ModelType, Bool) -> Void,
  onFailure: @escaping (Error) -> Void)
  -> URLSessionCancellable
{
  ...
}

is replaced with:

public func executeRequest(
  _ request: URLRequest,
  onSuccess: @escaping (ModelType, Bool) -> Void,
  onFailure: @escaping (Error) -> Void
) -> URLSessionCancellable {
  ...
}

Benefits

  • Easier, clearer single-line to multiline conversion
    Converting this:
public func executeRequest(_ request: URLRequest, onSuccess: @escaping (ModelType, Bool) -> Void, onFailure: @escaping (Error) -> Void) -> URLSessionCancellable {
  ...
}

With the old format:

-public func executeRequest(_ request: URLRequest, onSuccess: @escaping (ModelType, Bool) -> Void, onFailure: @escaping (Error) -> Void) -> URLSessionCancellable {
+public func executeRequest(
+  _ request: URLRequest,
+  onSuccess: @escaping (ModelType, Bool) -> Void,
+  onFailure: @escaping (Error) -> Void)
+  -> URLSessionCancellable
+{

With the new format:

-public func executeRequest(_ request: URLRequest, onSuccess: @escaping (ModelType, Bool) -> Void, onFailure: @escaping (Error) -> Void) -> URLSessionCancellable {
+public func executeRequest(
+  _ request: URLRequest,
+  onSuccess: @escaping (ModelType, Bool) -> Void,
+  onFailure: @escaping (Error) -> Void
+) -> URLSessionCancellable {
  • Simplifies adding or removing parameters
    Using trailing commas avoids touching unrelated lines:
public func createUser(
  name: String,
  email: String,
) -> User

Adding a parameter:

 public func createUser(
   name: String,
   email: String,
+  age: Int,
 ) -> User

Removing a parameter:

 public func createUser(
   name: String,
-  email: String,
 ) -> User
  • Matches single-line function style
    This keeps multiline functions visually consistent with non-wrapped ones:
public func reset() -> Bool {
  ...
}
  • Community alignment
    This style is the default in tools like swift-format and increasingly the standard in open source Swift projects.

Conclusion

This change brings us in line with community norms and improves the day-to-day development experience — especially when editing, reviewing, and refactoring function declarations.

@mike-at-home mike-at-home changed the title Update README.md Use Swift community standard multi-line function decl wrapping May 5, 2025
@mike-at-home mike-at-home changed the title Use Swift community standard multi-line function decl wrapping Wrap multi-line function declarations more consistently with Swift community May 5, 2025
@mike-at-home mike-at-home changed the title Wrap multi-line function declarations more consistently with Swift community Wrap multi-line function declarations before the last paren May 5, 2025
@mike-at-home mike-at-home marked this pull request as ready for review May 8, 2025 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant