Back to all reviewers

Use configuration access methods

elastic/elasticsearch
Based on 6 comments
Java

When accessing configuration settings, always use the appropriate type-safe accessor methods provided by the configuration framework rather than manual string parsing. This improves code readability, maintainability, and reduces potential bugs from string parsing errors.

Configurations Java

Reviewer Prompt

When accessing configuration settings, always use the appropriate type-safe accessor methods provided by the configuration framework rather than manual string parsing. This improves code readability, maintainability, and reduces potential bugs from string parsing errors.

For boolean settings:

  • Use getAsBoolean() with explicit default values instead of Boolean.parseBoolean()
  • For system properties, use Booleans.parseBoolean() with default value

For example, instead of:

boolean sourceOnly = Boolean.parseBoolean(indexSettings.getSettings().get("index.source_only"));
boolean isFrozen = Boolean.parseBoolean(writeIndex.getSettings().get("index.frozen"));
boolean enabledDebugLogs = Boolean.parseBoolean(System.getProperty(ENABLE_KERBEROS_DEBUG_LOGS_KEY));

Prefer:

boolean sourceOnly = indexSettings.getAsBoolean("index.source_only", false);
boolean isFrozen = writeIndex.getSettings().getAsBoolean("index.frozen", false);
boolean enabledDebugLogs = Booleans.parseBoolean(System.getProperty(ENABLE_KERBEROS_DEBUG_LOGS_KEY), false);

Similarly, when exposing configuration parameters in APIs, avoid implicit defaults and require explicit parameters. When checking compatibility or validating configurations, use clear and readable expressions that make the intent obvious.

This practice promotes consistency, makes default values explicit, and leverages the built-in validation provided by the configuration framework.

6
Comments Analyzed
Java
Primary Language
Configurations
Category

Source Discussions