View profile

 iOS Code Review | Curated code improvement tips - Issue #12

 iOS Code Review
 iOS Code Review
Hi there,
How is your week going?
I hope you enjoy today’s tips. Let me know what you found most interesting by replying to this email 🙌

Tired of translatesAutoresizingMasksIntoConstraints?
Two neat ways to deal with the good old (and annoying) translatesAutoresizingMasksIntoConstraints 👍
Rony Fadel
#iostipschallenge day 14

Using auto-layout and occasionally forget to call ``translatesAutoresizingMaskIntoConstraints = false`?

We've all been there.

Instead, use a `@UsesAutoLayout` property wrapper that automatically makes the call for you.
Or like that:
let label = UILabel().forAutoLayout() 
Type-safe identifiers
When we use string or int for identifiers, it can be easy to accidentally pass the wrong value to the function that takes a said identifier. @mecid wrote an article on using phantom types to make API that works with identifiers type-safe, so the compiler does the checking for us.
Majid Jabrayilov
A phantom type is a generic type that is declared but never used inside a type where it is declared. It is usually used as a generic constraint to build a more type-safe and robust API.
Understanding SwiftUI navigation
A nice short write-up on the current state of navigation in SwiftUI.
While the repo itself contains tools that you can use in your code, the readme goes over the issues that out-of-the-box navigation API has and potential directions for solving them.
It’s a valuable resource for understanding how to deal with navigation better, even if you don’t wish to use third-party code.
GitHub - pointfreeco/swiftui-navigation: Tools for making SwiftUI navigation simpler, more ergonomic and more precise.
Improving old code
This is a great approach to working on old code. Old code can be improved significantly even by small refactoring - it’s not always necessary to make big changes to make impact on code quality.
Matt Diephouse
How to improve old code:

1. Remove unused code
2. Use more accurate types

Accurate types let you prove that a function can’t take or can’t return certain values.

These form a virtuous cycle: removing unused code lets you remove unused values lets you remove unused code, etc.
On non-fatal errors
We often write checks like this one:
guard let requiredValue = value else { return }
In reality, when this case happens, the user will just see that nothing happens in response to their actions, and the developer will never know about such cases. Force unwrapping and crashing the app is not the answer either.
Antoine discusses ways to approach this problem in his recent article.
Antoine v.d. SwiftLee 
Learn about non-fatal errors

💥 Non-fatal vs crashes explained
☝🏼 Why 99% crash-free sessions isn't enough
🤔 Force unwrapping mindset

#swiftlang #iosdev
Alright, that’s it for today. 
Did you enjoy this issue? Let me know by pressing the buttons below, so I can improve the newsletter. 
Got feedback? Want to see more, or less of certain kinds of tips? I’d love to hear from you. Reply to this email or reach out on Twitter via @ios_code_review 🙌
Did you enjoy this issue? Yes No
 iOS Code Review
 iOS Code Review @ios_code_review

Bi-weekly newsletter amplifying code improvement tips from the Apple developer community in a bite-sized format. Swift, Objective-C, iOS, macOS, SwiftUI, UIKit and more. Curated by Marina Gornostaeva and published every other Thursday.

In order to unsubscribe, click here.
If you were forwarded this newsletter and you like it, you can subscribe here.
Created with Revue by Twitter.