by bluGill · 3y ago · 0 comments
The point of this is DOWN WITH BOOL!
https://wiki.qt.io/API_Design_Principles#The_Boolean_Paramet...
> An obvious solution is to replace the bool parameters with enum types. This is what we've done in Qt 4 with case sensitivity in QString. Compare:
str.replace("USER", user, false); // Qt 3
str.replace("USER", user, Qt::CaseInsensitive); // Qt 4
It's undoubtedly easy to forget how great Qt was nowadays, after its changed hands many times and honestly lost it's lustre. If anyone remembers what GTK+2 used to look like, it was really funny comparing Qt APIs to GTK2 and Win32. (The GTK developers definitely improved GTK+2 throughout its long life though, so nearer the end GTK+2 was a fair bit less awful to use IMO.) Either way, in my opinion, they were quite ahead of the curve in terms of API design, and I found their API design guides to be immensely helpful. To this day, I also agree with their principles on e.g. naming getters.The self-evident way here is to use named parameters (now a D feature):
str.replace("USER", user, caseSensitive : true);However, I will say one thing: there is another good reason to consider using enumerations in place of booleans, and that's when you have a situation where you are not 100% absolutely sure that 1-bit of information is sufficient. For example...
A bit contrived but, let's say you have a method that can optionally perform some kind of validation, like let's say, in C syntax so I don't embarrass myself, you have something like this:
void process_request(request req, bool validate);
This works well, although it obviously fails to the boolean parameter trap. But in D, you can avoid the boolean parameter trap, so that's not too big of a deal. However... I think that in many cases, boolean values are used in cases where you just happen to have something that only has two options, and not necessarily something that is true by nature. For example, maybe you want a validation mode where you only log a message but do not halt processing upon validating. Something like this could work: void process_request(request req, bool validate, bool allow_invalid);
But there's a lot of ways you could go about trying to name allow_invalid, it's arguably a pretty bad name. Worse, it only really makes sense when validate = true. In my opinion, something like this would be better: typedef enum {
NO_VALIDATION = 0,
VALIDATE_AUDIT = 1,
VALIDATE_ENFORCE = 2,
} validation_mode;
void process_request(request req, validation_mode validate);
For case-sensitivity, maybe it's true that this is not necessarily important. However, for regex options in particular, I think what you MIGHT want is more like a set of flags rather than either a pure enumeration OR a pure boolean. Of course, at that point, it does beg the question if maybe regex options should go in their own options structure of some kind, at which point boolean would make more sense again, but at least for shorthand, having a simple flags option seems reasonable to me. (I haven't checked how Qt does it currently. I would not be surprised if they have a longer form "options" overload, too.) fobricate(type1(), type2(false), type3());
Here 'true' is the default. The only difference between these types would only be in the name, so they all can have a common base.I.e., you should never see this in code:
if (piece != Black)If there are only two states, and provably will never be more; it's either or.
A classic case of this:
if (!Windows) ... do something for Posix ...This is as silly as saying "down with the integers." (Boolean is a full-blown type that is richer than an enum in terms of the set of operations it supports.)
If you really want the full set of operations that a bool supports on your enum you can add them (every language I'm aware of makes this harder than it should be, but that is a different issue), but most of the time they don't make logical sense as operations for what I'm doing so I want my code to not compile if I do them - I just make a mistake.
[insert rant about different strings not being the same thing here]
We do need integers at the bottom of the stack, but we need to move up to strong non-fundamental types sooner. It makes things harder at first, but it saves a lot of bugs long term.
A square meter... Anyway, I am all for the strong-typing on top of built-ins, but enums is not the answer.*
See, with strong types when someone catches errors like that it is fixed everywhere, or at least fails to compile where the bugs are, while with weak types you won't find every other place I made the same mistake without an expensive audit.
... whistles