-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2364b72
commit d057c35
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
title: '#1 Swift code refactor in action - user profile name' | ||
date: 2024-11-20 | ||
tags: ['Swift', 'Refactoring'] | ||
series: "Swift code refactor in action" | ||
cover: | ||
image: 'images/cover.png' | ||
alt: 'Swift code refactor in action - user profile name' | ||
--- | ||
|
||
Swift code refactor in action 👨🏻💻 | ||
|
||
Common scenario: formatting user profile name - I bet any of you faced this kind of task. | ||
|
||
At first glance, it look straightforward, but when you take a closer look, you’ll notice two potential improvements: | ||
1️⃣ One single return - simplification of the the function flow. | ||
2️⃣ Centralised formatting logic - reduces the chance of bugs. | ||
|
||
Check out the animated gif and the code where I refactor to address these issues. | ||
|
||
Advice: The easiest and fastest way to validate your refactor is to have the code unit tested, so remember to test it before you start refactoring! | ||
|
||
Gif ⤵️ | ||
|
||
![reference_image_delete](images/refactoring.gif) | ||
|
||
Code ⤵️ | ||
|
||
#1 | ||
```swift | ||
/// Returns the user's full name, optionally including a nickname in the format: | ||
/// - With nickname: `[Johnny] John Doe` | ||
/// - Without nickname: `John Doe` | ||
func userProfileName(firstName: String, lastName: String, nickname: String?) -> String { | ||
if let nickname { | ||
"[\(nickname)] \(firstName) \(lastName)" | ||
} else { | ||
"\(firstName) \(lastName)" | ||
} | ||
} | ||
``` | ||
|
||
#2 | ||
```swift | ||
/// Returns the user's full name, optionally including a nickname in the format: | ||
/// - With nickname: `[Johnny] John Doe` | ||
/// - Without nickname: `John Doe` | ||
func userProfileName(firstName: String, lastName: String, nickname: String?) -> String { | ||
nickname.map { nickname in ["[\(nickname)]", firstName, lastName].joined(separator: " ") } | ||
?? [firstName, lastName].joined(separator: " ") | ||
} | ||
``` | ||
|
||
#3 | ||
```swift | ||
/// Returns the user's full name, optionally including a nickname in the format: | ||
/// - With nickname: `[Johnny] John Doe` | ||
/// - Without nickname: `John Doe` | ||
func userProfileName(firstName: String, lastName: String, nickname: String?) -> String { | ||
[nickname.map { nickname in "[\(nickname)]" }, firstName, lastName] | ||
.compactMap { $0 } | ||
.joined(separator: " ") | ||
} | ||
``` | ||
|
||
--- | ||
|
||
Thanks for reading. 📖 | ||
|
||
I hope you found it useful! | ||
|
||
If you enjoy the topic don't forget to follow me on one of my social media - [LinkedIn](https://www.linkedin.com/in/maciej-gomolka/), [X](https://twitter.com/gomolka_maciej) or via [RSS](https://www.mobiledevdiary.com/index.xml) feed to keep up to speed. 🚀 |