Use `#unavailable`/`#available` instead of `#available`/`#unavailable` with an empty bodySW-R1017
Using the #available
and #unavailable
compiler directives with an empty
body can be misleading and result in unintended behavior. It is recommended to
use the #available
directive with a non-empty body or the #unavailable
directive with an explanation of why the code is unavailable.
When using #available
with an empty body, the code will compile but will not
execute on platforms where the availability condition is not met. This can
cause issues with code maintenance, as it may be difficult to determine why
certain code is not executing. Additionally, it can lead to unexpected behavior
if the availability condition changes in the future.
Using #unavailable
with an empty body can also be problematic, as it does not
provide any explanation for why the code is unavailable. This can make it
difficult for other developers to understand why the code is not working as
expected.
To address this issue, it is recommended to use the #available
directive with
a non-empty body, which can include code that handles the unsupported platform
or shows an error message to the user. Alternatively, use the #unavailable
directive with an explanation of why the code is unavailable, which would help
other developers understand the reasoning behind it.
Bad Practice
if #available(iOS 14.0) {
} else {
oldIos13TrackingLogic()
}
Recommended
if #unavailable(iOS 13) {
loadMainWindow()
}
if #available(iOS 9.0, *) {
doSomething()
} else {
legacyDoSomething()
}