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





Subscribe to our newsletter

By subscribing, you agree with Revue’s Terms of Service and Privacy Policy and understand that  iOS Code Review | Curated code improvement tips will receive your email address.

 iOS Code Review
 iOS Code Review
Hi there. Did you know that it’s already August?
In today’s issue, among other things, @v_pradeilles shares two video tips, memory leaks in Combine make a come-back, and we fix Copilot’s code. Let’s dive in 👇

When not to use [weak self]
@swiftandtips found a well-structured writeup on when to use and not to use [weak self] in closures. Here’s a concise chart to help you decide.
If you also want to learn the why’s, read the full article: You don’t (always) need [weak self] by @BesherMaleh
Pitt (Swift && Tips)
When do I need to use [weak self] in a closure? 🤔
This picture illustrates very well!

(Source: https://t.co/ujhMFqRLgh) https://t.co/MqhEOVIRp2
Lazy collections for speed
Love the new tip series by @v_pradeilles. In this short video he demonstrates speeding up his code 400.000 times just by adding .lazy. Yes, only five characters 🔥 Watch full video on Youtube (6 min)
Vincent Pradeilles
.map { $0 * $0 }
.filter { $0.isMultiple(of: 5) }
.first(where: { $0 > 100 })

This code is pretty wasteful, because it computes everything eagerly 🤷🏻‍♂️

But did you know it could be optimized with a single line?

Watch the full video 👉 https://t.co/ManYdiTXI0 https://t.co/vxqDAk2YVE
There's no secrets in UserDefaults
And another tip from @v_pradeilles. You’ve likely heard that saving sensitive data to UserDefaults is a bad idea, but perhaps weren’t sure why?
Vincent recorded a short tutorial demonstrating how the data saved in UserDefaults can be easily accessed. Watch on Youtube (4 min)
Vincent Pradeilles
Have you ever used the User Defaults API to store sensitive data?

If that’s the case, you might want to reconsider that choice 🙅🏻‍♂️

Because User Defaults are actually really far from providing a secure storage…

https://t.co/1MAv5IvHGp https://t.co/3HvX0UrCiS
Memory leaks in Combine
We’ve touched on this topic in the Issue #3. Well, there are always more places to find retain cycles in.
If you’re binding a publisher’s values to a property of a class, watch out for which operator you use. Using .assign(to: .property, on: self) will retain self and may cause a retain cycle. On the other hand using .assign(to: $property) retains the property’s publisher, not self.
This old forums thread has an example of leaking code. Know your enemy!
Does 'assign(to:)' produce memory leaks? - Using Swift - Swift Forums
Remember Copilot?
In Issue #2 I asked for your suggestions on what can be improved in Copilot’s code, and you sent some great suggestions! There are many things that could be improved there. Did you notice that it doesn’t even compile?
I made a playground with four improvement iterations. Check it out to see if you’ve spotted everything, or if there’s anything we missed 👀
Marcin Krzyzanowski
copilot can't Swift, and this makes the AI closer to us, humans. We can't Swift either. https://t.co/IukkILg4Sv
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 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.