3 ms·
I like where his head is at, he's trying to make code easier to write in a type safe way i guess. But this is a bit over zealous. An enum IS the right solutio
by swalsh 13y ago
I like where his head is at, he's trying to make code easier to write in a type safe way i guess. But this is a bit over zealous.
An enum IS the right solution here. Its more maintainable because everyone who touches the code base will implicitly know how the code works. With the solution he used, as i'm reading the code... i'm going to go "oh hey, huh i wonder how that works", then i'll spend 10 minutes trying to understand the code, added to the extra 10 minutes probably spent writing this code, I think its a bad idea.
if you're worried about passing a bad value in still, i think its fine to add a range check in the constructor, and to throw an exception. Exceptions are not something to be afraid of.
Also, i don't know what library he's using... but i feel somewhat strongly like i've done something like this in the past where i've serialized JSON, and enums have serialized as integers (as long as entry assigned a numeric value?). So it wouldn't be an issue to pass it in as a enum anyways.