Conversation
Default-constructors usually do not need to call `this->Modified()`. This particular Modified() call was accidentally moved into the function body of the default-constructor of BSplineDeformableTransform by pull request InsightSoftwareConsortium#6854 commit 9953d5f "STYLE: Remove SetFixedParameters... functions from BSplineBaseTransform"
It appears preferable for the `itk::Object` default-constructor _not_ to call `this->Modified()`, for two reasons: - `Object::Modified()` is virtual, but still the function call will not call any possible override, as explained by C++ Core Guidelines, "Don’t call virtual functions in constructors and destructors", Jun 14, 2026, https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rc-ctor-virtual - `Object::Modified()` calls InvokeEvent(ModifiedEvent()), which is useless when an Object is still "under construction", as it does not yet have any observer. So this `itk::Object` constructor should only update its timestamp, m_MTime.
dzenanz
left a comment
There was a problem hiding this comment.
Change is small, and reasoning makes sense. CI can totally sink it, though 😄
|
@dzenanz Indeed 😄 In fact I was just bothered by the unnecessary |
|
This change is probably fine but I don't agree with this comment:
The thing to consider here is not the event's but the expected consistency of the MTime for a the BSplineTransform object. This class contains other classes. Depending on how the object is expected to operate it may be expected that the MTime of the object >= than any object it owns. Sometimes for a class the GetMTime can return the max off all object it owns. I don't we are that particular or consistent with the MTime is many object, and this point may not be relevant, but I believe considering the MTime book keeping is more important than the Event side effect. |
|
Thanks for your reply @blowekamp The pull request text may be a bit informal, and indeed, I did not take into consideration that the MTime of an object might in some cases need to be greater than the MTime of its sub-objects. Are the commit messages still OK to you? Before my pull request #6854, the default-constructor of BSplineDeformableTransform indirectly called ITK/Modules/Core/Transform/include/itkBSplineDeformableTransform.hxx Lines 65 to 67 in dbab3e0
ITK/Modules/Core/Transform/include/itkBSplineBaseTransform.hxx Lines 110 to 112 in dbab3e0 So I think that the default-constructor of BSplineDeformableTransform did not intentionally do the extra |
There is the potential that is could confuse further people and AI looking at this code and understanding the MTime system. I am not going to give a blocking review due to this. |
|
Maybe update MTime at the end of the BSpline constructor? Would that be better Brad? |
|
Please remove from the commit message: |
@blowekamp Thanks but can you please explain why? During the default-construction of an Otherwise, can you please present a testable example where InvokeEvent is already useful for an You may consider this example: class MyObject : public itk::Object
{
public:
ITK_DISALLOW_COPY_AND_MOVE(MyObject);
using Self = MyObject;
using Superclass = Object;
using Pointer = itk::SmartPointer<Self>;
using ConstPointer = itk::SmartPointer<const Self>;
itkOverrideGetNameOfClassMacro(MyObject);
itkNewMacro(MyObject);
protected:
MyObject()
{
this->AddObserver(itk::ModifiedEvent(), [](const auto &) { std::cout << "Modified!\n"; });
}
~MyObject() override = default;
};
TEST(MyObject, Modified)
{
auto myObject = MyObject::New();
}When using the current main revision (and probably any released ITK version), So |
m_MTime.Modified()in the default-constructor ofitk::Object. For an Object that is still "under construction", it appears overdone to callthis->Modified(), because it does not yet have any observer to listen to itsModifiedEvent. Moreover, it appears preferable to avoid calling virtual functions within a constructor: C++ Core Guidelines, Don’t call virtual functions in constructors and destructors.For the record
Object::Modified()is defined here:ITK/Modules/Core/Common/src/itkObject.cxx
Lines 345 to 353 in fd3b0b4
It appears to have overrides at: