Skip to content

Conversation

Pankraz76
Copy link
Contributor

@Pankraz76 Pankraz76 commented May 9, 2025

Fix redundant code in DefaultPluginXmlFactory:write - avoiding DRY increasing cohesion lowering coupling

fix unreachable code:

return xml.read(is, request.isStrict(), source);

imho, this branch can never happen:

image

because of early exit check before

This is too defensive over here. The last if-else is the actual final possible condition and therefore should just return:

Its double-trouble anyway and therefore DRY violation - its a smell in eyes. 🎃

image image image

@Pankraz76 Pankraz76 changed the title Fix dead code in DefaultPluginXmlFactory#write Fix unreachable/dead code in DefaultPluginXmlFactoryL:103 May 9, 2025
@Pankraz76 Pankraz76 changed the title Fix unreachable/dead code in DefaultPluginXmlFactoryL:103 Fix unreachable/dead code in DefaultPluginXmlFactory:103 May 9, 2025
@Pankraz76
Copy link
Contributor Author

So no, this is not a dead branch - it serves an important purpose in the API's flexibility.

Conclusion:
✅ The else block is reachable and necessary.
It serves as a fallback when only a URL is provided in the XmlReaderRequest.

Would you like help adding a unit test to validate this branch?

@Pankraz76
Copy link
Contributor Author

But it's redundant then, as the behavior is now the same, and the end result is just without being overly complex and duplicating it—making the default exception explicit.

When the default behavior is the same in any case, it’s kind of simpler and should turn out equal in the end, right? So, classic redundancy—time for TDD (aka refactoring).

@Pankraz76 Pankraz76 changed the title Fix unreachable/dead code in DefaultPluginXmlFactory:103 Fix redundant code in DefaultPluginXmlFactory:write May 9, 2025
@Pankraz76 Pankraz76 marked this pull request as ready for review May 9, 2025 12:22
}
}
try (InputStream is = Files.newInputStream(Objects.requireNonNull(path))) {
return xml.read(is, request.isStrict());
}
} catch (Exception e) {
Copy link
Contributor

Choose a reason for hiding this comment

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

we shouldn't be catching raw exceptions here; probably fix that first

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IOException | XMLStreamException e

need this:
https://checkstyle.sourceforge.io/checks/coding/illegalcatch.html

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course, it's better API design to be precise and not broad—especially in this case.

If possible we should ensure this standard as well. In checkstyle its the same (of course as all the checkers are on), so i know this quite well. Its a good thing to strengthen the design.

apache/maven-shared-resources#73

}

@Test
void readFromPathParsesPluginDescriptorCorrectly(@TempDir Path tempDir) throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

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

similar

@Pankraz76 Pankraz76 marked this pull request as draft May 9, 2025 12:40
@Pankraz76
Copy link
Contributor Author

#2312

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.

2 participants