View profile

 Code Review | Curated code improvement tips - Issue #3

 iOS Code Review
 iOS Code Review
Hi there!
I hope your Thursday is going well ☀️ Today’s issue covers topics such as architecture, Combine, Swift, SwiftUI, UIKit, and development process. Let’s dive in!

Architectures are for humans
Last week a particular topic resonated through the community: the mental toll the different architectures bring on developers. The issue is especially prominent for complex architectures such as VIPER, which is known for having many similarly-named small files per each screen.
@collinodonnel shared his experience, which triggered many others to come forward. Dozens of developers shared similar experiences last week.
Any task required routing through six classes, and I found myself physically exhausted when I had to interact with it.
I have struggled with VIPER myself in exactly the same way.
Let this serve as a reminder to all of us. Not that VIPER is a bad architecture pattern, but that architectures are not only about clean code or testability. They’re also about people. It’s us humans who have to be able to understand the codebase, reason about it, and work on it - productively.
Retain cycles with Combine
Combine’s map operator retains the transform closure during the publisher’s lifetime. When we pass a function declared on self, self is retained as well. If the subscription is kept alive by the same object, then we have an unbreakable retain cycle that prevents self from ever deallocating.
Marking self as unowned would leave the possibility of the app crashing. @eneko shares a safe way to resolve the retain cycle:
Eneko Alonso
Working with Combine (or any other escaping closures) would be much nicer if we didn't have to worry about retain cycles (screenshot illustrates is the fix for the retain cycle above).
#combine #weakself #retain #SwiftLang https://t.co/rbZOGrsG0B
Configuring SwiftUI views with Environment
Majid shares a real-life example of how to leverage custom environment variables to configure custom SwiftUI views. Check out his article if you want to understand SwiftUI’s Environment better. The way we do things in SwiftUI is definitely different from how we used to do them in UIKit 🙈
Majid Jabrayilov
Every release of SwiftUI brings new environment values which you can use in many places like styles, custom views, etc. You can even populate the environment with your own custom values. Learn more about the power of the environment in my post.
https://t.co/CqjXLactKC https://t.co/zUUjRB2KLt
Reverse arrays for free
An interesting fact shared by @swiftandtips: reversing an array with reversed() doesn’t use any new memory, and has constant time complexity (read: is very, very fast). Read documentation with examples here.
Pitt (Swift && Tips)
Fun fact:
reversed() method in #SwiftLang's arrays has a complexity of O(1) 👌🏼

https://t.co/qZygKMSHka

It is not allocating new memory, instead, it's just changing the order of the access (indices) to work in reverse order! 😄
Detecting orientation changes in UIKit
Why do we want to know about changes in device orientation? Most often it is to know about screen size changes. In today’s variety of devices, device orientation is not the only possible cause for the app’s window to change size - think moving the app to split screen on iPad. Individual view controllers can also change size - f.ex. when changing width of columns in UISplitViewController.
Because of all this, it’s much safer to rely on viewWillTransitionToSize for concrete size changes in view controllers, and on traitCollectionDidChange for general changes in environment, such as size class, dark mode, preferred text size and so on. Thanks @mluisbrown for bringing this up!
Michael Brown
For observing orientation changes always prefer `traitCollectionDidChange` instead of `UIDevice.orientation` + notifications.
It’s much easier and less prone to subtle bugs. #iOSDev
Getting unstuck in the morning
This tip is not about tweaking the code, but about tweaking your own development process. Are you sometimes feeling stuck when picking up a new task in the morning? There are some smart ideas in @nicklockwood‘s thread.
My favourite trick is to create the pull request in the morning, even if I finished the implementation and pushed the commits the day before. Besides the productivity boost, it gives a chance to look at the solution with fresh eyes and think of missed edge cases.
Nick Lockwood
Last night I was in the middle of coding something, but was too tired to finish it so I went to bed.

If I *had* finished, then today I'd have been between tasks and inclined to procrastinate. Instead I was thinking about it since I woke up and couldn't wait to get started again.
✌️
Did you enjoy this issue? Let me know by pressing the buttons below, so I can improve the newsletter.
Got feedback? I’d love to hear your thoughts! 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. 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.