sofa-framework / sofa

Real-time multi-physics simulation with an emphasis on medical simulation.
https://www.sofa-framework.org
GNU Lesser General Public License v2.1
936 stars 312 forks source link

[Core] Clean public API of components #4943

Open alxbilger opened 3 months ago

alxbilger commented 3 months ago

The usual pattern of component classes is the following:

IMO, the template-specific method should not be public. In this PR, they are moved to protected. I think also, that the generic methods can be final, but this is breaking, so I am not sure that it is a good idea to keep the final keyword. The PR starts with the final keyword to study the consequence on the CI.

Those changes force the user to call only the generic methods. I took the opportunity to add a check on the component state in the generic methods. If the final keyword is used, it would ensure that the component state is called before each call of the public API.


By submitting this pull request, I acknowledge that
I have read, understand, and agree SOFA Developer Certificate of Origin (DCO).


Reviewers will merge this pull-request only if

bakpaul commented 3 months ago

I call that 'un doigt dans l'engrenage', nice !

I totally agree on the fact that those methods should be final otherwise the 'delegate' pattern used here makes no sense because we cannot trust it for child class...

But then I don't fully agree on the protected part : it might become cumbersome to test them in unit test with such protection

It might be good to understand why those base methods where overridden instead of the "delegate' one, maybe old artifact on un-refactored components ?

alxbilger commented 3 months ago

@bakpaul

I call that 'un doigt dans l'engrenage', nice !

Yes...

But then I don't fully agree on the protected part : it might become cumbersome to test them in unit test with such protection

It is exactly what happens now. However, I believe that the tests should not drive the design of a class. Today, I don't have a solution to test the protected methods (even if it's theoretically possible). So I am not against keeping them public.

damienmarchal commented 2 months ago

Hello,

Thanks for the PR. I fully agree on generalizing more the 'delegate' pattern and this is a proper usecase.

In the code base we are making the use of this pattern obvious by using a specific naming scheme (XXXX -> doXXX). The intention what to make visually clear what part was part of the public API and what part was using the "delegate" pattern. As in:

BaseData::beginEditVoidPtr() 
Data<T>::doBeginEditVoidPtr() 

I think it would be worth to stick to the scheme for the following reasons:

Of course, this means renaming the the addKToMatrix but in that matter, earlier is better and as the PR is breaking... this may be ok ;)