#golang tip
You can use documented consts for simple values (like bools) to make code much easier to read and understand AT A GLANCE.
I wouldn't do this _all_ the time, but when I notice code is tricky to read, I do it.
Example follows... 1/4
Consider this:
Greet("Mat", true)
I'd have to look up the method signature to figure out what that 'true' was for.
2/4
If we had these consts:
const (
// AnimationOn will greet with a flourish.
AnimationOn = true
// AnimationOff will greet without animations.
AnimationOff = false
)
Glanceability goes up:
Greet("Mat", AnimationOn)
3/4
Prefixing the consts is useful for when IDEs show you the options in alphabetical order because it groups related concepts.
Yes, it's more verbose. But also yes, it's more clear.
Do you like it? Dislike it? Do something similar?
4/4
This is most useful for when you are using someone else’s code.
If you control the API, see comments to this thread for other design options :)
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.
