Avoid Hardcoding Values for Better Libraries
2020-08-19
Naming constants is good practice, but they are still hardcoded values that can make library code less reusable and less predictable.
Bad
constexpr int kThumbnailSizes[] = {480, 576, 720}; // Returns thumbnails of various sizes for the given image. std::vector<Image> GetThumbnails(const Image& image) { std::vector<Image> thumbnails; for (const int size : kThumbnailSizes) { thumbnails.push_back(ResizeImage(image, size)); } return thumbnails; }
Instead, let the caller decide.
Good
// Returns thumbnails of various sizes for the given image. std::vector<Image> GetThumbnails(const Image& image, absl::Span<const int> sizes) { std::vector<Image> thumbnails; for (const int size : sizes) { thumbnails.push_back(ResizeImage(image, size)); } return thumbnails; }
// Declared in the public header. inline constexpr int kDefaultThumbnailSizes[] = {480, 576, 720}; // Default argument allows the function to be used without specifying a size. std::vector<Image> GetThumbnails( const Image& image, absl::Span<const int> sizes = kDefaultThumbnailSizes);