Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Replace '??' operator with custom Optional extensions #2470

Closed
wants to merge 1 commit into from

Conversation

bahattinkoc
Copy link

Description:

This pull request refactors the codebase to use the newly introduced Optional extensions, replacing instances of the ?? operator with more readable and expressive methods. The added extensions provide convenient methods such as orEmpty, orTrue, orFalse, and others, to handle common use cases where the optional needs to be unwrapped or given a default value.

Changes:

Replaced occurrences of the ?? operator across the project with the corresponding custom Optional extensions.
Improved code readability and consistency by utilizing these extensions.
Added additional safeguards, like isNilOrBlank for strings and isNilOrEmpty for collections, to handle nil and empty checks more elegantly.

@calda
Copy link
Member

calda commented Aug 29, 2024

?? is idiomatic Swift code and I would recommend using it over creating one-off helpers. Creating project-specific helpers that replace this standard language pattern is not more readable (it adds a new separate topic that readers have to learn) and is not more expressive (?? is objectively more expressive since it handles all of these use cases, vs dedicated helpers only handle a single use case).

@calda calda closed this Aug 29, 2024
@bahattinkoc
Copy link
Author

?? is idiomatic Swift code and I would recommend using it over creating one-off helpers. Creating project-specific helpers that replace this standard language pattern is not more readable (it adds a new separate topic that readers have to learn) and is not more expressive (?? is objectively more expressive since it handles all of these use cases, vs dedicated helpers only handle a single use case).

Thank you for your feedback! I completely understand the value of using the idiomatic ?? operator, as it is indeed a well-known and concise way to handle optionals in Swift. The intention behind introducing these extensions was to address some specific use cases where readability and code consistency could be improved across the project, especially when dealing with common patterns like handling empty collections or strings.

That said, I recognize that introducing custom helpers can add an additional learning curve for those unfamiliar with the codebase. My goal was to enhance expressiveness and reduce repetitive boilerplate code, but I see your point about maintaining familiarity with standard language patterns.

I'm open to discussing further if you believe sticking to ?? would be more beneficial for the team and the project's long-term maintainability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants