3 ms·
Which is far more realistic than defining single-use enums all over the place. I don't know why more languages don't have named parameters...
by gary_0 2y ago
Which is far more realistic than defining single-use enums all over the place.
I don't know why more languages don't have named parameters...
- kerkeslager 2y agoWhy is defining an enum "unrealistic"? It's at most 4 lines of very simple code to define an enum that replaces a boolean.
- edflsafoiewq 2y agoThink about cases more complicated than passing a bare literal. // Booleans + Named Parameters foo(should_scrub=not options.scrubbing_disabled) // Single-Use Enum import ScrubbingOption, ShouldScrub; foo( if options.scrubbing == ScrubbingOption::Disabled { ShouldScrub::No } else { ShouldScrub::Yes } )
- oneeyedpigeon 2y agoIf you format the second example more sensibly: foo(options.scrubbing == ScrubbingOption::Disabled ? ShouldScrub::No : ShouldScrub::Yes) it doesn't look much worse at all
- kerkeslager 2y agoI'd actually go further and say that there's no reason I can think of that foo() shouldn't have access to the ScrubbingOption, so just pass that in. If you have two-option enums and for some reason you transform them into two-option enums that represent essentially the same thing, of course that's going to be unnecessarily complex. As a general rule, "it's more verbose" isn't much of a concern to me: verbosity is a very poor indication of complexity, and entering code with your keyboard simply isn't the bottleneck for writing code (if it is, you aren't thinking about what you're writing enough).
- edflsafoiewq 2y ago> If you have two-option enums and for some reason you transform them into two-option enums that represent essentially the same thing, of course that's going to be unnecessarily complex. "For some reason" being anything so prosaic as they come from different libraries.
- kerkeslager 2y agoIf you have two libraries that aren't explicitly made to work with each other and operate on the same problem domain, being explicit about the translation layer between those two libraries is very much a feature, not a bug. That's absolutely a spot where verbosity and named enums will shine.