Created
May 24, 2024 08:03
-
-
Save scott-lydon/15acd2e4221fe214736ee7b5f1f79360 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Omit the return keyword in one-line functions that return a value. | |
Non-triggering examples: | |
swift | |
Copy code | |
func blocks() -> [Block] { | |
.all(with: .retained, times: 2) | |
} | |
swift | |
Copy code | |
func fetchUser() -> User { | |
User(name: "John") | |
} | |
swift | |
Copy code | |
func calculateSum(a: Int, b: Int) -> Int { | |
a + b | |
} | |
Triggering examples: | |
swift | |
Copy code | |
func blocks() -> [Block] { return .all(with: .retained, times: 2) } | |
swift | |
Copy code | |
func fetchUser() -> User { return User(name: "John") } | |
swift | |
Copy code | |
func calculateSum(a: Int, b: Int) -> Int { return a + b } | |
2. Use trailing closure syntax. | |
Non-triggering examples: | |
swift | |
Copy code | |
someFunction { value in | |
print(value) | |
} | |
swift | |
Copy code | |
UIView.animate(withDuration: 0.3) { | |
self.view.alpha = 1.0 | |
} | |
swift | |
Copy code | |
fetchData { result in | |
handle(result) | |
} | |
Triggering examples: | |
swift | |
Copy code | |
someFunction(settingsAction: { value in | |
print(value) | |
}) | |
swift | |
Copy code | |
UIView.animate(withDuration: 0.3, animations: { | |
self.view.alpha = 1.0 | |
}) | |
swift | |
Copy code | |
fetchData(completion: { result in | |
handle(result) | |
}) | |
4. Use higher-order functions when multiple properties call the same function. | |
Non-triggering examples: | |
swift | |
Copy code | |
[am6Btn, am7Btn].forEach { $0.titleLabel?.setAkinFont() } | |
swift | |
Copy code | |
[btn1, btn2, btn3].forEach { $0.isEnabled = true } | |
swift | |
Copy code | |
[view1, view2, view3].forEach { $0.isHidden = false } | |
Triggering examples: | |
swift | |
Copy code | |
am6Btn.titleLabel?.setAkinFont() | |
am7Btn.titleLabel?.setAkinFont() | |
swift | |
Copy code | |
btn1.isEnabled = true | |
btn2.isEnabled = true | |
btn3.isEnabled = true | |
swift | |
Copy code | |
view1.isHidden = false | |
view2.isHidden = false | |
view3.isHidden = false | |
More examples: | |
swift | |
Copy code | |
addButton.addTarget( | |
self, | |
action: #selector(addProfilePicPress), | |
for: .touchUpInside | |
) | |
addProfilePicButton.addTarget( | |
self, | |
action: #selector(addProfilePicPress), | |
for: .touchUpInside | |
) | |
Should be: | |
swift | |
Copy code | |
[addButton, addProfilePicButton].forEach { | |
$0.addTarget(self, action: #selector(addProfilePicPress), for: .touchUpInside) | |
} | |
swift | |
Copy code | |
logoutLabel.text = "Logout" | |
logoutLabel.textColor = .romanceRed | |
contentView.addSubview(logoutLabel) | |
logoutLabel.constraint(from: .centeredHorizontallyWith(contentView)) | |
logoutLabel.constraint(from: .centeredVerticallyTo(contentView)) | |
logoutLabel.constraint(from: .distanceToBottom(contentView.bottomAnchor, 36)) | |
logoutLabel.constraint(from: .distanceToTop(contentView.topAnchor, 36)) | |
Should be: | |
swift | |
Copy code | |
logoutLabel.text = "Logout" | |
logoutLabel.textColor = .romanceRed | |
contentView.addSubview(logoutLabel) | |
[.centeredHorizontallyWith(contentView), .centeredVerticallyTo(contentView), | |
.distanceToBottom(contentView.bottomAnchor, 36), .distanceToTop(contentView.topAnchor, 36)].forEach { | |
logoutLabel.constraint(from: $0) | |
} | |
swift | |
Copy code | |
rightAddButton.titleLabel?.setAkinFont() | |
scrollButton.titleLabel?.setAkinFont() | |
closeButton.titleLabel?.setAkinFont() | |
closeButtonBordered.titleLabel?.setAkinFont() | |
Should be: | |
swift | |
Copy code | |
[rightAddButton, scrollButton, closeButton, closeButtonBordered].forEach { | |
$0.titleLabel?.setAkinFont() | |
} | |
5. Require every method to have a comment. | |
Non-triggering examples: | |
swift | |
Copy code | |
/// Fetches data from the server. | |
func fetchData() { | |
// implementation | |
} | |
/// Handles user login. | |
func loginUser() { | |
// implementation | |
} | |
Triggering examples: | |
swift | |
Copy code | |
func fetchData() { | |
// implementation | |
} | |
func loginUser() { | |
// implementation | |
} | |
6. Remove one-line functions that do not return anything and call the code directly. | |
Non-triggering examples: | |
swift | |
Copy code | |
print("Hello, World!") | |
Triggering examples: | |
swift | |
Copy code | |
func printHello() { | |
print("Hello, World!") | |
} | |
swift | |
Copy code | |
func reset() { | |
value = 0 | |
} | |
7. Avoid print statements; use a logging function instead. | |
Non-triggering examples: | |
swift | |
Copy code | |
logger.log("This is a log message") | |
swift | |
Copy code | |
Log.info("User logged in successfully") | |
swift | |
Copy code | |
debugPrint("Debugging information") | |
Triggering examples: | |
swift | |
Copy code | |
print("This is a log message") | |
swift | |
Copy code | |
print("User logged in successfully") | |
swift | |
Copy code | |
print("Debugging information") | |
8. Avoid function declarations within other function bodies. | |
Non-triggering examples: | |
swift | |
Copy code | |
func outerFunction() { | |
// code | |
} | |
func innerFunction() { | |
// code | |
} | |
swift | |
Copy code | |
func calculate() { | |
let result = compute() | |
// other code | |
} | |
swift | |
Copy code | |
func performAction() { | |
// action code | |
} | |
Triggering examples: | |
swift | |
Copy code | |
func outerFunction() { | |
func innerFunction() { | |
// code | |
} | |
innerFunction() | |
} | |
swift | |
Copy code | |
func calculate() { | |
func compute() -> Int { | |
return 5 | |
} | |
let result = compute() | |
} | |
swift | |
Copy code | |
func performAction() { | |
func action() { | |
// action code | |
} | |
action() | |
} | |
9. Use higher-order functions for processing arrays instead of loops. | |
Non-triggering examples: | |
swift | |
Copy code | |
let values = ["a", "b", "c"].map { $0.uppercased() } | |
swift | |
Copy code | |
let filtered = numbers.filter { $0 > 10 } | |
swift | |
Copy code | |
let sum = numbers.reduce(0, +) | |
Triggering examples: | |
swift | |
Copy code | |
var values = [String]() | |
continue | |
ChatGPT | |
swift | |
Copy code | |
for item in ["a", "b", "c"] { | |
values.append(item.uppercased()) | |
} | |
swift | |
Copy code | |
var filtered = [Int]() | |
for number in numbers { | |
if number > 10 { | |
filtered.append(number) | |
} | |
} | |
swift | |
Copy code | |
var sum = 0 | |
for number in numbers { | |
sum += number | |
} | |
10. Avoid using [String: Any]; use Codable instead. | |
Non-triggering examples: | |
swift | |
Copy code | |
struct MyData: Codable { | |
let name: String | |
let age: Int | |
} | |
swift | |
Copy code | |
let jsonData = try? JSONEncoder().encode(myData) | |
swift | |
Copy code | |
let myData = try? JSONDecoder().decode(MyData.self, from: jsonData) | |
Triggering examples: | |
swift | |
Copy code | |
let data: [String: Any] = ["name": "John", "age": 30] | |
swift | |
Copy code | |
let jsonData = try? JSONSerialization.data(withJSONObject: data) | |
swift | |
Copy code | |
let data = try? JSONSerialization.jsonObject(with: jsonData) | |
11. Combine nested if-else statements into a single initialization statement. | |
Non-triggering examples: | |
swift | |
Copy code | |
self.init( | |
presentation: enabled ? .enabled(importance ?? .irrelevant) : .disabled(importance), | |
autoChangedImportance: enabled && importance != nil | |
) | |
Triggering examples: | |
swift | |
Copy code | |
if enabled { | |
if let importance = importance { | |
self.init(presentation: .enabled(importance), autoChangedImportance: false) | |
} else { | |
self.init(presentation: .enabled(.irrelevant), autoChangedImportance: true) | |
} | |
} else { | |
self.init(presentation: .disabled(importance), autoChangedImportance: false) | |
} | |
12. Avoid empty files. | |
Non-triggering examples: | |
Any file with content. | |
Triggering examples: | |
Any file that is completely empty. | |
13. Inline constants or variables used only once. | |
Non-triggering examples: | |
swift | |
Copy code | |
alertViewController.styleCancelAlert( | |
regularContentsModel: RegularContentsView.Model( | |
title: "Are you sure?", | |
message: "If you close this greet, it can't be reopened." | |
), | |
models: UIButton.SimpleModel( | |
title: "Yes close it.", | |
action: { | |
self?.refuseGreetAndClose() | |
} | |
) | |
) | |
Triggering examples: | |
swift | |
Copy code | |
let contents = RegularContentsView.Model( | |
title: "Are you sure?", | |
message: "If you close this greet, it can't be reopened." | |
) | |
let buttonModel = UIButton.SimpleModel( | |
title: "Yes close it.", | |
action: { | |
self?.refuseGreetAndClose() | |
} | |
) | |
alertViewController.styleCancelAlert( | |
regularContentsModel: contents, | |
models: buttonModel | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment