Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In a similar fashion, storing user properties like whether or not they want email subscriptions, their notification settings, etc... Is something that I often use a bitmask field in my model to save space in the db.

These can be queried using bitwise operations and you can abstract all of it with synthetic instance properties.

User.email_enabled = true would translate to 0010 or something in, for example, a properties column.

Really handy stuff!!!



Are you running out of space in your database? It just seemed like a way to needlessly complicate your database, make it difficult to query (what was email_enabled again? the third bit?), difficult for other developers to use (now it needs a comment to understand) and difficult to constrain for data integrity.

I think that unless you're having space issues, you're better off using boolean columns.


This was somewhat of a contrived example.


A solution that seems good right up until a bug starts flipping the wrong bit or someone decides that a flag needs 3 states instead of two.

Bitmasking is a useful technique in some places, but storing user preferences isn't one of them. It's 'cleverness' for the sake of being clever - the code is harder to read, new developers can't immediately understand it, experienced developers have to look up what flags mean, you can't just dump the database row to see what a user's preference are, and if your documentation ever gets out of sync you're screwed.

For the sake of saving DB space it's a really bad idea.


Clever databases can do this themselves.


Gah, booleans use so little space do you really need that added complexity?




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: