You might as well timestamp it
source link: https://changelog.com/posts/you-might-as-well-timestamp-it
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
You might as well timestamp it
future-you will be glad you stored a timestamp instead of that boolean
In my 15+ years of web development, there are very few things I can say are unequivocally a good idea. It almost always does depend.
Storing timestamps instead of booleans, however, is one of those things I can go out on a limb and say it doesn’t really depend all that much. You might as well timestamp it. There are plenty of times in my career when I’ve stored a boolean and later wished I’d had a timestamp. There are zero times when I’ve stored a timestamp and regretted that decision.
Why is that? Because any time a piece of data is important enough to store its truth/falsehood in your database, it’s likely important enough that you’ll eventually want to know when that state was set. It’s like the exact opposite of YAGNI. You Ain’t Gonna Regret It? 🤔
Even in the rare case that you never need that timestamp… what have you lost? Its cost is negligible, both in data storage and coding overhead. Your code merely needs to consider a NULL
timestamp as false
and any non-NULL
timestamp as true
and there’s your boolean use-case. An example, in JS:
// using a boolean
let is_published = true
if (is_published) console.log("it's true!")
if (!is_published) console.log("it's false!")
// using a timestamp
let published_at = new Date()
if (published_at) console.log("it's true!")
if (!published_at) console.log("it's false!")
The ergonomics are darn-near identical. And you can pretty this up with a helper function called isPublished()
which checks the published_at
attribute. Basic stuff you already know, I’m sure!
- The next time you’re tempted to store an
is_deleted
boolean, reach for adeleted_at
timestamp instead. - If you think you need an
is_hidden
field, try usinghidden_at
in its place. Live with that for awhile and then ask yourself if you regret the decision. - Or if you’re about to create a
has_signed_in
boolean? You’re gonna want to know when that happened:signed_in_at
to the rescue.
Future-you will thank you. 🙏
Get the latest news and podcasts for developers in your inbox, every week. We make it super easy to keep up with developer news that matters.
Recommend
-
96
除非特别声明,此文章内容采用知识共享署名 3.0许可,代码示例采用Apache 2.0许可。更多细节请查看我们的服务条款。
-
60
README.md Kronos Kronos is a distributed service / library which can be used to provide synchronized time in a cluster. It provides an API to qu...
-
31
48
-
4
Use your MongoDB `_id` field as your created at timestamp and you can get away without creating an index for the created_at field From time to time I have to run queries against our MongoDB collections p...
-
4
How to get current timestamp yourbasic.org/golang Use
-
2
You Might as Well Be a Great Copy Editor An early draft of a paper, blog post, grant proposal, or other piece of technical writing typically...
-
4
Lane Wagner for Qvault...
-
2
I asked Bing to plan my vacation – and I might as well just stay home
-
4
Might as well add TCL’s 4-series 43-inch 4K Roku TV to your guest room at a new low of $148 ...
-
0
Everyone might as well keep a retractable utility knife on hand to open Amaz...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK