Enums for hard-coded configuration

While writing my new Stretch Timer app, I needed to set lower and upper limits for three key numbers. I had them hardcoded as numeric literals but when I wanted to change one of them, it occurred to me there must be a better way.

After doing a bit of research on enums, I found a construct that not only factors the numbers out to a single place, but also makes their use a lot more readable.

enum pickerRange {
    static let Stretch = (min: 5, max: 60)
    static let Rest = (min: 5, max: 60)
    static let Repeat = (min: 3, max: 15)

It is fairly plain from the enum declaration what is being described. It is also fairly evident what’s going on when they are referenced, too.

return pickerRange.Stretch.max - pickerRange.Stretch.min + 1

Leave a Reply

Your email address will not be published. Required fields are marked *