by crazygringo · 1y ago · 0 comments
Turning boolean database values into timestamps is a weird hack that wastes space. Why do you want to record when an email was verified, but not when any other fields that happen to be strings or numbers or blobs were changed? Either implement proper event logging or not, but don't do some weird hack where only booleans get fake-logged but nothing else does.
Should booleans turn into enums when a third mutually-exclusive state gets added? Yes, of course, so go refactor, easy. But don't start with an enum before you need it. The same way we don't start with floats rather than ints "just in case" we need fractional values later on.
Booleans are a cornerstone of programming and logic. They're great. I don't know where this "booleans are bad" idea came from, but it's the opposite of communicating intention clearly in code. That boolean should probably stay a boolean unless there's an actual reason to change it.
> wastes space > premature optimisation
A timestamp is a witness of when the email was verified. Since if they’ve verified can be calculated from it, having both is not only redundant but allow invalid states to be represented.
Cases like email verified are often followed by the need to know when. Say an expiry system. Going from bools, you are faced with the hard choice of how to migrate existing state.
Databases also warrant more care as a source of persistent state - accessed by multiple versions of your software. If you don’t have this persistency, then it matters less.
> any other fields that happen to be strings or numbers or blobs were changed > implement proper event logging
Event logging is orthogonal to your database state. If your business logic needs dirty flags or timestamps they should be stored in the database, not queried.
And if you do need it for other fields, adding the bool is the perfect time to ask yourself if what you need is a timestamp.
> way we don't start with floats rather than ints "just in case" we need fractional values later on
Floats are a subset of int, and a natural migration. A bool can be calculated from a timestamp, but not the other way.
And often not. That's the point. Avoid premature optimization. (FWIW, I've never encountered a system in my life where a successful email verification then expired after a period of time.)
> having both is not only redundant but allow invalid states to be represented.
That's a different topic. That's about what to do when you know you need the timestamp. The article is about using a timestamp when you don't have a timestamp requirement.
> they should be stored in the database, not queried.
I don't know what that means. Everything in the database is queried. And you can store your events in the database, in one or more event log tables.
> Floats are a subset of int, and a natural migration.
I think you meant to say the opposite, but even that's not true because of precision. And so too are enums a natural migration from booleans. That's the point -- start simple and extend as needed.
I think a lot of people misunderstand KISS, believing everything should be primitives or surface-level simplicity. Instead, I interpret "simple" not something like golang's surface-level readability, but infosec's "principle of least privilege". Pick the option that minimizes possible state and capture the requirement logic, rather than primitives just because they're "simple" or "familiar".
Even then, sometimes it's fine to violate it. In this case, (nullable) date time might be more preferable than boolean for future-proofing purposes. It's trivial to optimize space by mapping date time to boolean, while it's a total pain to migrate from boolean to date time.
Also, doesn't "... a weird hack that wastes space" contradict "Avoid premature optimization"?
Which is what booleans do when the requirement is two states.
> Also, doesn't "... a weird hack that wastes space" contradict "Avoid premature optimization"?
No, because including the timestamp "just in case" is the premature optimization.
> I think a lot of people misunderstand KISS
I agree, people do misunderstand simplicity.
KISS, YAGNI, and then actually analyze your requirements to understand what the mature schema looks like. A boolean is the simplest thing that can possibly work, usually. Do that first and see how your requirements evolve, then build the database schema that reflects your actual requirements.
Although it always depends on what exactly you're really doing.
This is not premature optimization, sometimes booleans can be extremely hard to change so it's not as easy as "just refactor".