5 ms·
I have to disagree with the enum advice. "enum { No, Yes };" is bad (regardless of order) but the solution isn't use bools. The solution is to use a better nam
by bluGill 3y ago
I have to disagree with the enum advice. "enum { No, Yes };" is bad (regardless of order) but the solution isn't use bools. The solution is to use a better name "enum { DoNotFoobar, WithFoobar };" is what you should be using. (C++ has scoped enums so you might see Foobar::with or Foobar::without - possibly better if you have that type of option in your language). If you do it this way your code is even more readable, and you can someday realize there is another option as well.
The point of this is DOWN WITH BOOL!
- queuebert 3y agoGeorge Boole weeps.
- frou_dh 3y agoAlso, for example if you are implementing chess then you should have an enum of Black and White rather than using `bool white`.
- WalterBright 3y agoBlack and White are two states where you should never use negation, and hence bool is not the right choice for self-evident code. I.e., you should never see this in code: if (piece != Black)
- codr7 3y agoWhy? If there are only two states, and provably will never be more; it's either or.
- WalterBright 3y agoBecause negation is a cognitive problem people have. The other problem happens when a third state is added - it becomes very hard to find all the dependencies that need updating. A classic case of this: if (!Windows) ... do something for Posix ...
- codr7 3y agoYes, and I agree in most cases. But the previous example is not one of those.
- planede 3y agoI mostly agree with this, nobody likes `fobricate(true, false, true)`. I do have to wonder if this is a cure for the lack of named function arguments in C++.
- bluGill 3y agoEven in languages with named function arguments you still end up with the same problem very often. The person who wrote fobricate(True, False, True) in python knows what order things are and probably didn't bother to name each one. You could force naming all arguments, but that gets ridiculous as well in a different way.
- Koshkin 3y agoOne approach would be to have custom types (i.e. classes) so that you could write things in a type-safe manner: 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.
- jchw 3y agoI agree, this is absolutely true. The first time I was introduced to this was actually an excellent old Qt Quarterly, wherein they discussed good API design. Thankfully, it seems most of that information is still online. https://wiki.qt.io/API_Design_Principles#The_Boolean_Parameter_Trap 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.
- WalterBright 3y agoCaseInsensitive is a negation, and so less clear. The self-evident way here is to use named parameters (now a D feature): str.replace("USER", user, caseSensitive : true);
- jchw 3y agoGood point: named arguments/keyword parameters actually do negate some of the need for this. Qt is obviously designed for the limitations of C++, so this definitely makes sense. (And yes, CaseInsensitive is not necessarily ideal either. That's a fair point. But, given the limitations, I definitely see why they picked that route.) 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.)
- Koshkin 3y ago> DOWN WITH BOOL! 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.)
- bluGill 3y agoDown with integers and floats as well! Seriously, replace them all with strong types. Just because you have two integers doesn't mean can mix them, and even if you can that doesn't mean all operations make sense. If you have a matrix you don't want to mix up row and columns accidentally (there are times to do it intentionally, but it should be clear that is intentional). A meter * a meter should result in a liter, while a meter * page count should fail to compile. 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.
- Koshkin 3y ago> A meter * a meter should result in a liter A square meter... Anyway, I am all for the strong-typing on top of built-ins, but enums is not the answer.*
- bluGill 3y agoErr... 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