Write code that explicitly states its intent rather than relying on implicit behavior or complex constructs. This improves readability and maintainability by making the code's purpose immediately clear to other developers.
Write code that explicitly states its intent rather than relying on implicit behavior or complex constructs. This improves readability and maintainability by making the code’s purpose immediately clear to other developers.
Key practices:
{}
Example of explicit default values:
// Prefer explicit default
disableKeyboardShortcuts(convertRawProp(
context,
rawProps,
"disableKeyboardShortcuts",
sourceProps.disableKeyboardShortcuts,
{false})) // explicit default value
// Instead of implicit empty default
disableKeyboardShortcuts(convertRawProp(
context,
rawProps,
"disableKeyboardShortcuts",
sourceProps.disableKeyboardShortcuts,
{})) // unclear what the default is
Example of clear conditional structure:
// Prefer clear if-else structure
if (ignoreYogaStyleProps_ || filterObjectKeys != nullptr) {
// We need to filter props
return jsi::dynamicFromValue(*runtime_, value_, [&](const std::string& key) {
if (ignoreYogaStyleProps_ && isYogaStyleProp(key)) {
return false;
}
if (filterObjectKeys) {
return filterObjectKeys(key);
}
return true;
});
} else {
// We don't need to filter, just include all props by default
return jsi::dynamicFromValue(*runtime_, value_, nullptr);
}
// Instead of complex ternary with intermediate variables
const std::function<bool(const std::string&)> filterFunctionOrNull =
ignoreYogaStyleProps_ || filterObjectKeys != nullptr
? propFilterFunction
: nullptr;
return jsi::dynamicFromValue(*runtime_, value_, filterFunctionOrNull);
Enter the URL of a public GitHub repository