Eli Perkins.

Conditional Rendering Over Nil Coalescing

Stop writing count ?? 0 in your UI code.

Stop writing items ?? [] in your API return values.

Please, for the love of god, stop writing item?.id ?? "", especially. Coalescing feels satisfying when you appease the compiler or typechecker, but what you're doing is introducing a ticking timebomb instead.

I've been guilty of using each of these patterns over my career. What I should have done instead was understand why those values could be nil and handle them with some loud error or better yet, some UI that indicated these values were missing or loading or just plain empty still.

I see so much UI code that defaults to these fallback values, rather than conditionally rendering a loading indicator, an error message or an empty state.

So this is my plea: stop using nil coalescing in your UI code. Use conditional rendering and show a dang loading indicator, error message, or empty state instead. It will make your product so much better. Your users will thank you. The various loading and empty states will be obvious and your app will feel that much more polished.

Future You™ will be so stoked when your API starts returning nil values for some edge case Current You™ hadn't considered. Future You™ will be so appreciative when they see an error rendered instead of an empty label because Current You™ thought ?? "" was a quick way to get the feature out.

A quick if let items { ListView(items: items) } else { LoadingIndicator() } will be so great for your users to understand what's up.

A simple switch result { case .success(let value): ... case .failure(let error): ... } will impress your teammates with your careful consideration of the error case.

A brief if !items.isEmpty { ListView(items: items) } else { ContentUnavailableView() } will look impressive for those brand new users to your app.

A thoughtful if let itemID = selectedItem?.id { ... } will save you hours of debugging why your database is filled with missing IDs or why your API is erroring out.

Just handle the dang nil values. Please.


Eli Perkins

Written by Eli Perkins, an engineering leader based in Denver. Say hello on Bluesky.

Loading Bluesky conversation...