domains / / maplibre/maplibre-native
Descriptive named constants
Replace magic numbers and unclear variables with descriptive named constants using appropriate naming conventions. This improves code readability, maintainability, and prevents errors.
Replace magic numbers and unclear variables with descriptive named constants using appropriate naming conventions. This improves code readability, maintainability, and prevents errors.
Guidelines:
- Use
constexprvariables with descriptive names instead of raw numbers - Follow camelCase naming for constants (reserve ALL_CAPS for macros only)
- Include comments explaining the origin of special values when relevant
- Use meaningful names for array indices and limits
Example:
// Poor readability:
resource.dataRange = std::make_pair<uint64_t, uint64_t>(0, 16383);
// Improved with named constants:
// https://github.com/protomaps/PMTiles/blob/main/spec/v3/spec.md#3-header
constexpr uint64_t pmtilesHeaderOffset = 0;
constexpr uint64_t pmtilesHeaderMaxSize = 16383;
resource.dataRange = std::make_pair(pmtilesHeaderOffset, pmtilesHeaderMaxSize);
// Array indices should have names:
bool propUpdateFlags[4] = {propertiesUpdated, propertiesUpdated, propertiesUpdated, propertiesUpdated};
// Better:
constexpr size_t linePropFlagIndex = 0;
bool propUpdateFlags[4] = {/*...*/};
if (!linePropertiesBuffer || propUpdateFlags[linePropFlagIndex]) {
// ...
}
// Context for numeric parameters:
Context::Context(RendererBackend& backend_)
: gfx::Context(16), // TODO
// Better:
constexpr uint32_t maximumVertexBindingCount = 16;
Context::Context(RendererBackend& backend_)
: gfx::Context(maximumVertexBindingCount),
This approach makes code self-documenting, easier to maintain, and less prone to errors when numbers need to be updated.