UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to split up SwiftUI code into manageable chunks

Recorded – watch the full episode on YouTube.

You want code that is beautiful, but also maintainable and scalable and reusable and flexible. What advice do you give to folks to help them split up their large SwiftUI views into smaller, easier to understand more reusable views?

Meng To: Well, perhaps I'm not the best person to answer this question, but I'm going to try to answer from a designer perspective who codes. First of all, in the design tool, more and more designers are using components, are using design systems.

So the way that I look at it is to borrow the same strategy to make sure that every single piece of code is reusable and is three steps customizable. Again, from that principle of the number three, which is something that I've learned a lot from Steve Jobs back in terms of like the way he thinks. The three steps is to make sure that you're not necessarily making a component that can transform itself to be an app or can transform in seven to 10 types of buttons.

“Keep it short, make it reusable, connect it to the design system and try to be consistent between the design and the SwiftUI with UI.”

But if a single button can transform into two types of button or three types of button, that's amazing. You don't want to have like maybe 20 props. When I say props, variables. You might want to have three or five or something like that. Keep it short, make it reusable, connect it to the design system and try to be consistent between the design and the SwiftUI with UI.

So for example, a lot of the naming conventions that I try to do in my team, because a lot of our developers are not native English speakers. So, they have trouble with naming their component. They have trouble naming their views and they make typos. And so, what I try to do in the design is to sort of say, okay, this is a list view, or this is a row.

So, I put that in the design tool when I name the layers. And then when I ship it to developers, they look at it and they're like, “I’m going to use exactly this name.” So, we try to be consistent and during meetings then when we talk to each other, we can say, “okay, remember the row view.” Then it's consistent across the two and it helps with communication as well. So, that's kind of how I look at it.

Paul Hudson: Yeah. Consistency is key in so many things in our job.

This transcript was recorded as part of Swiftly Speaking. You can watch the full original episode on YouTube, or subscribe to the audio version on Apple Podcasts.

Listen on Apple Podcasts

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.