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

Fix Issue #184 #2295

Merged
merged 3 commits into from
Jan 5, 2023
Merged

Fix Issue #184 #2295

merged 3 commits into from
Jan 5, 2023

Conversation

Alex-Vol-Amz
Copy link
Contributor

@Alex-Vol-Amz Alex-Vol-Amz commented Jan 2, 2023

The logic conditionals were entangled in an invalid way that produced nonsense debug log messages. Simplified the logic and reduced the logic branches to make the operation direct and thus fix the problem.


Make sure these boxes are checked before submitting your PR -- thank you!

  • Added an entry into CHANGELOG.md if you have changed SpotBugs code

The logic conditionals were entangled in an invalid way that produced
nonsense debug log messages. Simplified the logic and reduced the logic
branches to make the operation direct and thus fix the problem.
@@ -190,15 +190,13 @@ public final void reportBug(@Nonnull BugInstance bugInstance) {
}
int priority = bugInstance.getPriority();
int bugRank = bugInstance.getBugRank();
if (priority <= priorityThreshold && bugRank <= rankThreshold) {
doReportBug(bugInstance);
if (priority > priorityThreshold) {
Copy link
Contributor

@ThrawnCA ThrawnCA Jan 2, 2023

Choose a reason for hiding this comment

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

This logic seems wrong. If the priority is higher than the threshold, it should be reported, not filtered, right?

Seems to me that the correct fix is simply to swap the first condition so that it reports the bug if either priority or bugRank is greater than the threshold, and leave the rest basically the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Threshold for both priority and rank operates with a "lower value is more important".

Look at the original code

        if (priority <= priorityThreshold && bugRank <= rankThreshold) {
            doReportBug(bugInstance);
        }

I think just this comment alone illustrates why the original logic was overly complex and unnecessary. Even experienced reviewers get it wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could have effected the same fix just by reversing the logic in the else clause from <= to > but after reading this code the current form is easier to follow and reduced the number of comparisons to exactly 2 with 3 logical outcomes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You may need to look at the issue reported. The problem is not with the reporting but with the invalid debug logging. What was happening before is that when the reporting was skipped due to priority the debug log would instead report skipping due to rankThreshold with nonsense conditions. The opposite would also do the same, a skip due to rank would debug log that skip due to priority with invalid conditions.

Obviously if the original logic was flawed this would have been fixed a long time ago, it was only the debug logging that was confusing and this was never addressed.

In my opinion this makes the intended logic clear and unambiguous.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps a unit test would help restore confidence in this logic and eliminate any questions. There were no unit tests I could find that validated logged messages but it may be useful to at least make a unit test that proves doReportBug is called based on the rules of this logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This logic seems wrong. If the priority is higher than the threshold, it should be reported, not filtered, right?

Seems to me that the correct fix is simply to swap the first condition so that it reports the bug if either priority or bugRank is greater than the threshold, and leave the rest basically the same.

I am not going to make the changes due to the reasons stated above. The code is correct as it stands and I expect this would be approved now. I added the unit test to prove that the intended filtering was working the same way before and after my changes.

Copy link
Member

Choose a reason for hiding this comment

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

@Alex-Vol-Amz Think I'm following the logic error. The problem was that both were considered for the original first check.

priority <= priorityThreshold && bugRank <= rankThreshold

Then it was checking this again

priority <= priorityThreshold

If it didn't hit that then it assumed rank threshold to log. Problem as I see original code then just to clarify my understanding is that priority or bug ran could be > and such the only path was for rank threshold to write the debug line.

I agree it was overly complex as I had to think too hard to even respond here. Its far easier in new state as follows.

Write debug...previously this was a bug, if it was > then it was never logged, it fell through to rankThreshold incorrectly.

priority > priorityThreshold

Write debug...

bugRank > rankThreshold

Otherwise....reportBug

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly, which is now also covered by unit test so we know for sure it will stay fixed in this new form.

I wish the code was amenable to actually mock the logger so one could even verify that the debug is actually logged and correct, but given the direct approach followed here it becomes simple binary logic.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @Alex-Vol-Amz I'm good with the changes. Going to merge it in now.

This unit test serves as proof that the reportBug function is operating
as expected with respect to reporting based on priority and rank
threshold values.

As a bonus this test also verifies that the relaxed mode will override
any rank or priority and always report bugs found.
@Alex-Vol-Amz
Copy link
Contributor Author

Performed the execution of the new unit test with and without the main code change. The result was the same passing test as expected since the intention of the code change was to fix the debug log, not change the reporting behavior.

@hazendaz hazendaz self-assigned this Jan 5, 2023
@hazendaz hazendaz dismissed ThrawnCA’s stale review January 5, 2023 05:25

Satisfactory information from contributor on the issue, full suite of test cases, along with second review here. All seems ok and far easier to read. It was a problem with logging.

@hazendaz hazendaz merged commit d1343b5 into spotbugs:master Jan 5, 2023
@hazendaz
Copy link
Member

hazendaz commented Jan 5, 2023

@Alex-Vol-Amz All in all, satisfied with the change as far more readable and avoids having to think to hard on what the processing was doing. Thanks for going that extra mile generating tests for the changes as that helps considerably with the effort.

@hazendaz hazendaz added this to the SpotBugs 4.8.0 milestone Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants