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.
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:
getAsBoolean()
with explicit default values instead of Boolean.parseBoolean()
Booleans.parseBoolean()
with default valueFor 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.
Enter the URL of a public GitHub repository