Search code examples
cframeworksglfwsoftware-designsoftware-quality

Reason for using custom true false values in frameworks


I was reading the GLFW library and I noticed they use GLFW_TRUE(1) and GLFW_FALSE(0). Now I've already seen that in other frameworks as well,making their own custom true/false identifiers. Is there any reason in creating your own custom true/false enums or classes in a framework? Does it make the code more readable for example? Is it a compatibility issue?

Another programmer speculated that they probably wouldn't want to #include <stdbool.h> but that header is in fact very small and wouldn't overcomplicate dependencies


Solution

  • Is there any reason in creating your own custom true/false enums or classes in a framework?

    • support of old or broken compilers (C99 incompatible, do not have stdbool.h and _Bool type)
    • static type checks with external tools (like cppcheck)
    • strong type checking by compiler (thinking of C++ classes with custom conversion)
    • subjective code readability

    Does it make the code more readable for example?

    Is subjective. For someone, not for someone else.

    Is it a compatibility issue?

    Most probably yes.


    In case of GLFW it was written for windows in ~2002 (according to this site https://www.glfw.org/changelog ). My guess the windows compiler the author used didn't have stdbool.h. Either way, ask the author of the library.