Why do libraries define their own true and false?
from crimsonpoodle@pawb.social to programming@programming.dev on 15 Jan 23:23
https://pawb.social/post/18723915

I’m working through the vulkan tutorial and came across GLFW_TRUE and GLFW_FALSE. I presume there’s a good reason but in looking at the docs it’s just defining 1 and 0, so I’m sorta at a loss as to why some libraries do this (especially in cpp?).

Tangentially related is having things like vk_result which is a struct that stores an enum full of integer codes.

Wouldn’t it be easier to replace these variables with raw int codes or in the case of GLFW just 1 and 0?

Coming mostly from C, and having my caps lock bound to escape for vim, the amount of all caps variables is arduous for my admittedly short fingers.

Anyway hopefully one of you knows why libraries do this thanks!

#programming

threaded - newest

darklamer@lemmy.dbzer0.com on 15 Jan 23:35 next collapse

This is often done for backward compatibility, as stdbool.h which provides true and false wasn’t standard before C99 and even though that’s more than 25 years ago now a lot of old habits die hard.

crimsonpoodle@pawb.social on 15 Jan 23:40 next collapse

Ahh this makes some sense

SpaceNoodle@lemmy.world on 15 Jan 23:46 next collapse

Also, plenty of embedded systems don’t use the C standard library.

nrab@sh.itjust.works on 17 Jan 22:28 collapse

stdbool.h (along with float.h, limits.h, stdarg.h, stddef.h, stdint.h, and some other library facilities) is required to be provided even in freestanding environment so, at least as long as you use an ISO C conformant compiler, you can always include those even if you don’t have a libc implementation

rimu@piefed.social on 15 Jan 23:59 collapse

Yeah in the late 90's I was coding in C++ and I'm pretty sure I had to define true and false manually.

SpaceNoodle@lemmy.world on 16 Jan 02:20 collapse

I seem to recall using the true and false literals C++ in the late '90s … looks like they were in the C++98 standard, but it’s not clear which pre-standard compilers might have supported them.

furrowsofar@beehaw.org on 15 Jan 23:39 next collapse

Probably readability. Correct typing maybe too. Also better error checking.

crimsonpoodle@pawb.social on 15 Jan 23:42 collapse

I’m not sure I understand readability? I guess is disambiguates numeric variables if you used 1 and 0. But with true and false available that would seemingly do the same thing. You still have to know what the arguments your passing are for regardless.

furrowsofar@beehaw.org on 15 Jan 23:47 next collapse

Does C have a logical type these days? Never use to.

crimsonpoodle@pawb.social on 15 Jan 23:52 collapse

I guess in reading not until c99(see other comment); they just used integers in place of Booleans, in which case your readability statement makes more sense given the historical context

furrowsofar@beehaw.org on 15 Jan 23:57 next collapse

For what it is worth. I learned C in 1990. Switched largely to Python in 1998.

bluGill@fedia.io on 16 Jan 00:36 next collapse

True is not one in C. You can always compare a value to 0 if you need false but comparing to any single value for true is wrong. Often functions will return some calculated value which would be zero for false and who cares - it isn't zero so return it for true. Thus all defines of true are suspect.

__nobodynowhere@startrek.website on 16 Jan 01:20 collapse

stdbool.h’s true and false are macros that expand to integers 1 and 0

C23 adds a proper bool type

Flipper@feddit.org on 16 Jan 06:26 next collapse

Only 50 years after it’s creation.

brisk@aussie.zone on 16 Jan 09:12 collapse

C99 has a proper boolean (_Bool), C23 makes true and false booleans (and properly gives _Bool the name bool without the macro)

JakenVeina@lemm.ee on 16 Jan 02:39 collapse

A function call of “MyFunction(parameter: GLFW_TRUE)” is more readable than “MyFunction(parameter: 1)”. Not by much, mind you, but if given the choice between these two, one is clearly better. It requires no assumptions about what the reader may or may not already know about the system.It communicates intent without any ambiguity.

over_clox@lemmy.world on 15 Jan 23:52 next collapse

Some languages define True as -1, which is NOT False…

bitcrafter@programming.dev on 16 Jan 00:52 next collapse

which is NOT False…

You really didn’t need this; I would have just assumed that you were speaking the truth.

over_clox@lemmy.world on 16 Jan 01:29 collapse

CONST False = 0, True = NOT False

NOT as in the binary operator. What’s NOT of 0 in a 32 bit space? 0xFFFFFFFF, which is -1, which is ≠ 1

Different languages, and even different programmers might interpret the concept and definition of True and False differently, so to save any ambiguity and uncertainty, defining your own critical constants in your own library helps make sure your code is robust.

bitcrafter@programming.dev on 16 Jan 02:49 collapse

So… all that is NOT False either, I presume?

SwordInStone@lemmy.world on 16 Jan 15:14 collapse

they mean every bit is different

bitcrafter@programming.dev on 16 Jan 16:36 collapse

I don’t know; their comment seemed pretty much the same throughout…

pHr34kY@lemmy.world on 16 Jan 07:23 collapse

I once wrote a library to replace an older one. Someone did this, and users were multiplying variables by booleans and negating them in formulas.

I just made the new library less stupid and left the users to clean up their mess when migrating.

floofloof@lemmy.ca on 15 Jan 23:54 next collapse

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.

furrowsofar@beehaw.org on 16 Jan 00:02 next collapse

Now I have heard everything. What is zero? Missing value?

floofloof@lemmy.ca on 16 Jan 00:23 collapse

Zero is something you always have to watch out for and handle, because he likes to use NULL for “don’t know”. I should really have deleted the database while it was still young, before they had backups.

[deleted] on 16 Jan 00:05 next collapse
.
forrcaho@lemmy.world on 16 Jan 00:43 next collapse

Something like if (stupid_bool & 0x01) should work for those.

floofloof@lemmy.ca on 16 Jan 02:27 next collapse

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.

0x0@lemmy.dbzer0.com on 16 Jan 04:14 collapse

I imagine this would still lead to a never ending stream of subtle logic errors.

from bossland import billysbool, billysand
from geography import latlong
import telephony

def send_missile_alert(missiles_incoming: billysbool, is_drill: billysbool, target: latlong):
  if billysand(missiles_incoming, not is_drill):
    for phone in telephony.get_all_residents(target):
      phone.send_alert("Missiles are inbound to your location")

Can you spot the bug?

mathmaniac43@lemm.ee on 16 Jan 04:21 collapse

The conventional ‘not’ would not behave differently for the two non-zero values. Insidious.

0x0@lemmy.dbzer0.com on 16 Jan 04:30 collapse

Correct! I made a number of other mistakes (edited away now due to shame), but that’s the one I made on purpose.

kabi@lemm.ee on 16 Jan 16:53 collapse

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.

joyjoy@lemm.ee on 16 Jan 03:19 next collapse

Does your boss frequently browse the database table records outside the API?

floofloof@lemmy.ca on 16 Jan 08:03 collapse

Oh you have no idea. There is no teaching this guy.

Hawk@lemmy.dbzer0.com on 16 Jan 06:13 next collapse

If that is something your boss is managing, get the fuck out of there.

Kissaki@programming.dev on 16 Jan 08:04 next collapse

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.

floofloof@lemmy.ca on 16 Jan 08:08 collapse

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.

Kissaki@programming.dev on 16 Jan 08:16 collapse

that assumes you don’t write any SQL

floofloof@lemmy.ca on 16 Jan 08:25 collapse

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.

affiliate@lemmy.world on 16 Jan 08:13 next collapse

why not just take it a step further and make true = “Yes” and false = “No”

floofloof@lemmy.ca on 16 Jan 08:18 next collapse

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.

Hoimo@ani.social on 16 Jan 15:17 collapse

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.)

lurklurk@lemmy.world on 16 Jan 13:29 next collapse

Get a better boss

jjjalljs@ttrpg.network on 16 Jan 13:39 collapse

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)

Corbin@programming.dev on 15 Jan 23:55 next collapse

It’s because the Booleans sometimes are flipped in display-server technology from the 1980s, particularly anything with X11 lineage, and C didn’t have Boolean values back then. More generally, sometimes it’s useful to have truthhood be encoded low or 0, as in common Forths or many lower-level electrical-engineering protocols. The practice died off as popular languages started to have native Boolean values; today, about three quarters of new developers learn Python or ECMAScript as their first language, and FFI bindings are designed to paper over such low-level details. You’ll also sometimes see newer C/C++ libraries depending on newer standards which add native Booleans.

As a fellow vim user with small hands, here are some tricks. The verb gU will uppercase letters but not underscores or hyphens, so sentences like gUiw can be used to uppercase an entire constant. The immediate action ~ which switches cases can be turned into a verb by :set tildeop, after which it can be used in a similar way to gU. If constants are all namespaced with a prefix followed by something unique like an underscore, then the prefix can be left out of new sections of code and added back in with a macro or a :%s replacement.

atzanteol@sh.itjust.works on 16 Jan 05:55 next collapse

In VisualBasic “true” would be represented as -1 when converted to an int because it’s all 1’s in twos complement.

PoolloverNathan@programming.dev on 16 Jan 17:21 next collapse

By the way, you can use g~ to get the effects of tildeop without needing to set it.

crimsonpoodle@pawb.social on 17 Jan 04:22 collapse

Seriously helpful thanks! One of my friends working on a G15 restoration project pointed out this notation to be after you did— yet while they use 0 for truth they used 20 for false so not sure were they got the second idea. And your vim tip saved me a bunch of hand ache!

qx128@lemmy.world on 16 Jan 00:24 next collapse

It’s for the extra helpful documentation. You see, in this fantastic example, after the author set GLFW_TRUE to 1, he explained the deep and profound meaning of the value. This exemplifies that the number 1 can also be written as a word: “One”! Some people might be able to figure this out, but the author clearly went above and beyond to make the code accessible to the open source community, encouraging contributions from anyone who’s considering improving the code. Furthermore, this follows the long held tradition of man pages - explaining the nuance of the code, in preparation for telling others to RTFM when they arrogantly ask a question.

Azzu@lemm.ee on 16 Jan 05:37 next collapse

My brain is so used to seeing political content that I read “why do liberals define their own true and false” and was already like “what kind of shit take am I going to have fun reading today”

spacecadet@lemm.ee on 16 Jan 07:28 next collapse

I can give you a shit take if you want one, but I don’t have shiitake mushrooms.

Azzu@lemm.ee on 16 Jan 12:43 collapse

I like my shit takes wrapped in beef rolls, in case you come across some more :)

topherclay@lemmy.world on 17 Jan 02:05 collapse

That should probably be red alert wakeup call for you right?

Azzu@lemm.ee on 17 Jan 09:57 collapse

Why?

Kissaki@programming.dev on 16 Jan 08:08 next collapse

I found the comments/answers about backwards compatibility of not defined booleans and negative true interesting and plausible.

What I first thought of was that TRUE and FALSE can be redefined, so it serves as ensurance that within the library consistent values are being used no matter what other libs and callers do with their typing and definitions.

Kissaki@programming.dev on 16 Jan 08:09 next collapse

I love the description as well. “One.” “Zero.”

BigDanishGuy@sh.itjust.works on 16 Jan 10:40 next collapse

I work with young people starting out in IT, so I’m used to getting screenshots, and I’m so used to screenshots made with a phone instead of just capturing the screen, that I’ve stopped complaining… But come on! At least evaluate the result of the first picture and maybe do another if it’s illegible.

crimsonpoodle@pawb.social on 16 Jan 16:44 collapse

Yeah that’s fair— this is my focus workstation so don’t have any messaging apps or email to send the screenshot but def could have taken a second picture.

KindaABigDyl@programming.dev on 16 Jan 17:02 next collapse

GLFW is a C library, not a C++ one, and an old one at that, and so the reason is that a long time ago, there was no bool in C. Every library would make their own true and false bc it’s handy to have.

Nowadays, the type _Bool has been added to C, and C++ has built-in bool, but you can still see the legacy of no boolean in C as to use the type name “bool” as well as the key words “true” and “false” for 1 and 0, you have to include “stdbool.h,” as well as in custom types in these old GL-adjacent libraries.

slazer2au@lemmy.world on 16 Jan 17:45 next collapse

Because JavaScript exists

cupcakezealot@lemmy.blahaj.zone on 17 Jan 02:21 collapse

i just like namespacing my variables anyway so there’s no chance of any conflicts and so you can easily change something one place instead of everywhere