this post was submitted on 15 Jan 2025
106 points (93.4% liked)
Programming
17823 readers
432 users here now
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Rules
- Follow the programming.dev instance rules
- Keep content related to programming in some way
- If you're posting long videos try to add in some form of tldr for those who don't want to watch videos
Wormhole
Follow the wormhole through a path of communities [email protected]
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
My boss insisted, before I arrived at the company, that everything in the database be coded so that 1 = Yes and 2 = No, because that's the way he likes to think of it. It causes us daily pain.
I'm reminded of an old job's database where every key was named "id_foo" instead of "foo_id"
You didn't have user_id. You had id_user. You didn't have project_id, you had id_project. Most of the time, anyway. It was weird and no one could remember why it was like that. (Also changes to the DB were kind of just yolo, there wasn't like a list of migrations or anything)
If that is something your boss is managing, get the fuck out of there.
Something like
if (stupid_bool & 0x01)
should work for those.Yeah of course we convert, but it effectively means you need this little custom conversion layer between every application and its database. It's a pain.
I imagine this would still lead to a never ending stream of subtle logic errors.
Can you spot the bug?
The conventional 'not' would not behave differently for the two non-zero values. Insidious.
Correct! I made a number of other mistakes (edited away now due to shame), but that's the one I made on purpose.
I mean, if you have a billysbool class anyway, you'd make its truthiness correct according to bossman's scheme, and then the not operator would work correctly.
Get a better boss
Does your boss frequently browse the database table records outside the API?
Oh you have no idea. There is no teaching this guy.
why not just take it a step further and make true = “Yes” and false = “No”
I have seen this, but with "Y", "N" instead. That was the way the database stored it and the way the UI displayed it, but everything inbetween converted to boolean instead, because there was logic depending on those choices. It wasn't that bad, all things considered, just a weird quirk in the system. I think there was another system that did just use those strings plain (like
WHERE foo = 'Y'
in stored procedures), but nothing I had to work with. We just mapped "Y" to true when reading the query results and were done with it.(And before anyone asks, yes, we considered any other value false. If anyone complained that their "Yes", "y" or empty was seen as false, we told them they used it wrong. They always accepted that, though they didn't necessarily learn from it.)
It would probably carry less risk, but in terms of bytes used this would be even worse. And we have other problems there that I'd tell you about but it would make me too sad.
Microsoft SQL Server has a bit type and you always use 0 and 1 and cast/convert them. No native bool type. It's a hassle.
Well that would be ok, because any standard tool for interfacing with the database would transparently treat bit in the DB as bool in the code. I think many DBs call it a bit rather than a bool.
that assumes you don't write any SQL
I'm used to ORM layers where you can write SQL queries but you're basically converting the results to objects before you use them. These kinds of things tend to handle bits OK, and bit parameters can usually be set as booleans directly. I haven't used SQL Server in a while though so maybe it isn't as convenient as that.