Recurring C++ and Qt anti-patterns
-
@kshegunov
I don't want to get into a debate (I know what you're like :) ), and I do know about floating point numbers being approximate representations (though zero/0.0
does have an exact representation), but (IMHO!) it is only in your physics/quantum mechanics area that "dividing by a double(0) is a valid operation" (the area where you can magic-away infinities and so on!). In a program it is not. (What have I let myself in for...!) -
@JonB said in Recurring C++ and Qt anti-patterns:
I know what you're like
Hey! Words can hurt, you know! ;)
though zero/0.0 does have an exact representation
Yes, actually two representations, as with the actual zero. You have +0.0 and -0.0.
it is only in your physics/quantum mechanics area that "dividing by a double(0) is a valid operation"
Eh, I didn't write the IEEE standard. Take your beef with prof. Kahan.
In a program it is not
Actually if you look through the
math.h
implementations you're going to see a lot of handling for such cases. For example the people who wrote them had the decency to actually handle these special cases likelog(0)
returning-inf
. While I agree it's not often useful to divide by zero it sometimes can be, so that's the reason to handle it like that, I assume. -
@kshegunov said in Recurring C++ and Qt anti-patterns:
You have +0.0 and -0.0
The next time someone asks how much money I have in my pocket I will remember to give this answer.
I have $123.45 to give away. I want to hand each person $0.00. How many people do I need to meet to get rid of all my cash? :)
-
Here: https://en.wikipedia.org/wiki/Riemann_series_theorem
Knock yourself out ... ;P -
Be careful with not doing things the pythonic way in python. A lot of the time doing it the pythonic way leverages the internals of the language. In other words it pushes the execution from the interpreter to the built in methods that are written in C. So it can have an effect on performance. I don't think the exception example does this though. There may be other reasons I am not aware of.
-
wait a cotton pickin minute! there is no explicit cast to double in python so the x/double(0) argument is invalid on that basis alone...and x/float(0) behaves as expected.
-
@fcarney said in Recurring C++ and Qt anti-patterns:
Uncomfortable admission:
I wrote windows specfic code today...We feel for you :D
-
Here is a nice QML anti-pattern:
Column { Rectangle { height: parent.height } }
This one was "fun". Yeah, it doesn't necessarily detect the loop and it locks up the desktop (Gnome). So you have to kill the process manually from a terminal outside of the desktop (ctrl-alt-f4).
-
From https://forum.qt.io/topic/113223/check-whether-a-script-exists-by-script-name/14
QProcess process; process.setStandardOutputFile(QProcess::nullDevice()); if (!process.startDetached(progName, args)) ...
Would anyone care to comment on why C++ allows calling a static method off an instance without (seemingly) offering the option of a warning message for it? :) (C# doesn't let me write this.)
-
AFAIK, there's nothing wrong with that. It's just that in the case you are showing, the static method has a specific behaviour that makes it unsuitable to be called like that.
-
@JonB said in Recurring C++ and Qt anti-patterns:
Would anyone care to comment on why C++ allows calling a static method off an instance without (seemingly) offering the option of a warning message for it? :) (C# doesn't let me write this.)
Because the class is known and that's all that matters. Whether you call it through an object or with its qualified name makes no difference. Actually, there's one widespread use of that in the Qt documentation:
int main(int argc, char *argv[]) { QApplication app(argc, argv); return app.exec(); // QCoreApplication::exec is static }
-
@kshegunov
But that is not my point/question. Which is: this piece of code is not the first (or the last) where someone has mistakenly written this. If C++ wants it this way, would it not be a good idea by now for compilers to offer a warning option? There is reason that e.g. C# does not allow it. -
@JonB said in Recurring C++ and Qt anti-patterns:
If C++ wants it this way, would it not be a good idea by now for compilers to offer a warning option?
If this were a potential error, probably. Since this is almost always safe there's no reason to offer a warning.
There is reason that e.g. C# does not allow it.
Which is what exactly?
-
@kshegunov I think the point from @JonB is that people do call static methods on an object by mistake and then wander why the object is not changed (I sometimes see this here in the forums). The compiler could generate a warning, but I doubt people would care enough about those if they do not even notice what they do wrongly :-)
-
@jsulm said in Recurring C++ and Qt anti-patterns:
but I doubt people would care enough about those
They don't I have taken over projects that hat on first compile 20k + warnings...
"Every time you compile with warnings, a fairy dies! So don't forget to clap your hands during compile time. Once for each fairy!"
-
@jsulm said in Recurring C++ and Qt anti-patterns:
@kshegunov I think the point from @JonB is that people do call static methods on an object by mistake and then wander why the object is not changed (I sometimes see this here in the forums).
Yes, I acknowledged that, but it's not an error, nor does it warrant a warning in my mind. One just have to know what they're doing/expecting of said method, which is good approach in every case. ;)
The compiler could generate a warning, but I doubt people would care enough about those if they do not even notice what they do wrongly :-)
That's why I compile with warnings-are-errors before I even consider deploying. The other option is just abysmal ... and can be dangerous depending on which field you're working in. So to everyone out there that ignores warnings: fix your freaking code!
-
@kshegunov
I'll try to keep my remarks brief, as I don't want to dominate this thread.As @jsulm said, my point is that being allowed to call a static method on an instance is not wrong or an error, but it may indicate a programmer mistake. I observe this empirically from the number of cases I have seen, such as the one I quoted from this forum.
If I write a statement like
word;
, thengcc
gives me a-Wunused-value
warning. If I writeif (word = value)
I get a-Wparentheses
warning. Neither of these is "wrong", the second one in particular is perfectly useful, yet someone recognised they may indicate commonly made faux-pas. Which the user may ignore, or suppress, at their peril. Personally, I would have liked to have seen a-Wstatic-call-on-instance
:)