Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(PD): ThreadSize enum simplification #18682

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

alfrix
Copy link
Contributor

@alfrix alfrix commented Dec 22, 2024

refactor to get the designations from the ThreadDescription instead of having a bunch of arrays duplicating the same thing

also added a test for threadtype change to check that is working properly

@github-actions github-actions bot added the Mod: Part Design Related to the Part Design Workbench label Dec 22, 2024
@chennes
Copy link
Member

chennes commented Dec 23, 2024

@AjinkyaDahale is going to look at this.

@@ -194,6 +194,44 @@ def testRefineHole(self):
self.Doc.recompute()
self.assertEqual(len(self.Hole.Shape.Faces), 7)

def testThreadTypeChange(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to split this test in two, each with one specific purpose:

As you refactored the way the enum values are determined and set, and there hasn't been a test for this previously, it would be good to address this first. You can use the getEnumerationsOfProperty method to get the possible values. E.g.

self.Hole.ThreadType = 'ISOMetricProfile'
allowed_sizes = self.Hole.getEnumerationsOfProperty("ThreadSize")
# then asserts on allowed_sizes; I'd say some spot checking is enough

I think we also should only set enums using their string representation (i.e. self.Hole.ThreadSize = 'M4' instead of self.Hole.ThreadSize = 11). I acknowledge that this source file currently uses a wild mix of both, but in my opinion new code should not copy the integer way.

Generally, long test methods of the form: act, assert, act, assert, act, assert, ... are considered an anti-pattern. Unfortunately, Python's unittest module does not support data driven tests for cases like the one here. But we can still improve the readability and reporting (in case of failures or errors) using sub-tests. See here for an example.

@jbaehr
Copy link
Contributor

jbaehr commented Jan 1, 2025

I think this is a very welcomed refactoring! I'm also happy to see that you extended the test suite as well.

I compiled this branch and did a few rudimentary tests using the GUI and it all seems to work as expected.

I'm no expert on modern C++, though, so I cannot comment on that part.

Then I have a question regarding the order of the enum values. @alfrix do you know whether the new code produces the same order as the old one? (at a first glance it does) @chennes do you know whether the order of the enums matters for backward compatibility with existing files?

Copy link
Contributor

@AjinkyaDahale AjinkyaDahale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. There are a couple comments but there's no need to insist on them. Apart from the comment below, could you not remove the magic number 171 in src/Mod/PartDesign/App/FeatureHole.cpp:86?

Comment on lines 619 to 623
for (const auto& thread : Hole::threadDescription[threadType]) {
if (thread.designation != nullptr) {
designations.emplace_back(thread.designation);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be replaced with std::transform and then std::erase(std::remove(...),...). Also, is the null check necessary?

@alfrix
Copy link
Contributor Author

alfrix commented Jan 3, 2025

jbaehr
@alfrix do you know whether the new code produces the same order as the old one?

I think we also should only set enums using their string representation (i.e. self.Hole.ThreadSize = 'M4' instead of self.Hole.ThreadSize = 11

Thank you for the feedback, i made the test to check that the enum order wans't changed, and both ThreadSize and ThreadType map correctly to the expected values. Setting enums using their string representation would defeat this purpose.
I agree that using subTest can be useful for providing more detailed failure messages, however, it still results in a series of act, assert, act, assert blocks, just with added context managers around each.

I’m not seeing a significantly better way to structure this.

AjinkyaDahale
could you not remove the magic number 171 in src/Mod/PartDesign/App/FeatureHole.cpp:86?

changing it to a vector would be more appropriate?

-const Hole::ThreadDescription Hole::threadDescription[][171] =
+const std::vector<Hole::ThreadDescription> Hole::threadDescription[] =

Can be replaced with std::transform and then std::erase(std::remove(...),...). Also, is the null check necessary?

i would prefer to avoid std::transform, is too obscure for coders that aren't focused on c++.
the for loop is easier to read and less complex.

changing to a vector guarantees non nulls, so yes it can be avoided if the type is changed:

-        if (thread.designation != nullptr) {
-            designations.emplace_back(thread.designation);
-        }
+        designations.push_back(thread.designation);

@AjinkyaDahale
Copy link
Contributor

AjinkyaDahale
could you not remove the magic number 171 in src/Mod/PartDesign/App/FeatureHole.cpp:86?

changing it to a vector would be more appropriate?

-const Hole::ThreadDescription Hole::threadDescription[][171] =
+const std::vector<Hole::ThreadDescription> Hole::threadDescription[] =

Now that I think about it, this is much better. I'm not sure if the empty values earlier would have been initiated to nullptr or left as garbage values to cause issues.

Can be replaced with std::transform and then std::erase(std::remove(...),...). Also, is the null check necessary?

i would prefer to avoid std::transform, is too obscure for coders that aren't focused on c++. the for loop is easier to read and less complex.

After removing the check, probably good enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Mod: Part Design Related to the Part Design Workbench
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants