Back to all reviewers

prefer explicit code

facebook/react-native
Based on 2 comments
C++

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.

Code Style C++

Reviewer Prompt

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:

  • Set default values explicitly instead of using empty initializers like {}
  • Structure conditional logic with clear if-else blocks rather than complex ternary operations or intermediate variables
  • Add intent-clarifying comments when the logic serves multiple purposes

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);
2
Comments Analyzed
C++
Primary Language
Code Style
Category

Source Discussions