Why is this a binding loop?
-
Hi,
There's one thing missing from your setter: the equality guard. Usually the first thing done in a setter is check if the value changes and stop there if not. This allows to avoid signal storms were each party connected re-emits the same values over and over.
-
Hi,
There's one thing missing from your setter: the equality guard. Usually the first thing done in a setter is check if the value changes and stop there if not. This allows to avoid signal storms were each party connected re-emits the same values over and over.
-
Either that or
void ChangeConsumables::SetPosterModalChecklist(QStringList qsl) { if (m_posterModalChecklist == qsl) { return; } m_posterModalChecklist = qsl; emit EventPosterModalChecklistChangedForQML(m_posterModalChecklist); }
Depending on your taste.
Note that I have seen this version more frequently. Also in Qt's own sources. -
Either that or
void ChangeConsumables::SetPosterModalChecklist(QStringList qsl) { if (m_posterModalChecklist == qsl) { return; } m_posterModalChecklist = qsl; emit EventPosterModalChecklistChangedForQML(m_posterModalChecklist); }
Depending on your taste.
Note that I have seen this version more frequently. Also in Qt's own sources. -
I understand your point.
IIRC, early returns like these may allow some optimization by the compiler.
It's technically also at least one less operation because != is usually implemented as ! ==.
-
Not to be argumentative, but I'd venture that a routine that is comparing QStringList objects probably leaves much to be desired in the optimization department anyway.
Your comment about !== is interesting. Is there a way for the programmer to explicitly effect a similar comparison?
-
@mzimmers said in Why is this a binding loop?:
Not to be argumentative, but I'd venture that a routine that is comparing QStringList objects probably leaves much to be desired in the optimization department anyway.
I was more in the generic case of simple type comparison :-)
@mzimmers said in Why is this a binding loop?:
Your comment about !== is interesting. Is there a way for the programmer to explicitly effect a similar comparison?
I am not sure I am following you on that one.
-
@mzimmers said in Why is this a binding loop?:
Not to be argumentative, but I'd venture that a routine that is comparing QStringList objects probably leaves much to be desired in the optimization department anyway.
I was more in the generic case of simple type comparison :-)
@mzimmers said in Why is this a binding loop?:
Your comment about !== is interesting. Is there a way for the programmer to explicitly effect a similar comparison?
I am not sure I am following you on that one.
@SGaist said in Why is this a binding loop?:
I am not sure I am following you on that one.
Well, of course C++ doesn't have a "!==" operator, so I assumed you meant that it first compares type, then content (a la Javascript). I was just curious how a C++ coder might perform that type check explicitly.
-
You missed the space between the ! and the == :-)
I meant it as: "!(a == b)"
-
Oh...I hate it when I do that (heh).
But now my statement on efficiency is even more true...
EDIT:
For those who really wish to exact that last bit of optimization, you could always do something like this:
void ChangeConsumables::SetPosterModalChecklist(QStringList qsl) { do { if (m_posterModalChecklist == qsl) { continue; } if (any other reasons to exclude further processing) { continue; } m_posterModalChecklist = qsl; emit EventPosterModalChecklistChangedForQML(m_posterModalChecklist); } while (false); }
This technique is also very handy for avoiding multiple if statements (with their annoying attendant indentation).