Skip to content

[java] Refactored god class rule#578

Merged
adangel merged 11 commits intopmd:masterfrom
oowekyala:god-class-rule
Aug 26, 2017
Merged

[java] Refactored god class rule#578
adangel merged 11 commits intopmd:masterfrom
oowekyala:god-class-rule

Conversation

@oowekyala
Copy link
Copy Markdown
Member

This is the new god class rule

The metrics can be improved individually later, especially ATFD, which has some known limitations (I'll have no time to do that in a few days)

@adangel adangel added the in:metrics Affects the metrics framework label Aug 24, 2017
@adangel adangel added this to the 6.0.0 milestone Aug 24, 2017
Copy link
Copy Markdown
Member

@adangel adangel left a comment

Choose a reason for hiding this comment

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

Awesome!

if (val < 0 || val > 1) {
throw new IllegalArgumentException("Expected a number between 0 and 1");
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think, you could use String.format(...) here:

return String.format("%." + numDecimals + "f%%", 100 * val);

I think, this is without the truncate-to-Int functionality.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's nice indeed! We'll have to override the locale though, the decimal separator is locale-specific (for me it's a comma). Tests that pass on my machine could fail on other machines.

Truncate to int is only really needed for the tests, I think it actually looks neater/ more consistent with decimals


if (methodCount > 1) {
for (int i = 0; i < methodCount; i++) {
for (int j = i + 1; j < methodCount; j++) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That would compare in the last round the same method - i == j.
I think the condition for the out loop over i should be "i < methodCount - 1"?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hum no, in the last round, i = methodCount - 1, so j = i + 1 = methodCount, thus j < methodCount doesn't hold and the inner for loop is skipped. You wrote this ;)

Changing the outer condition might be clearer though, will do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, right ;)



private String getNameImage(ASTPrimaryExpression node) {
ASTPrimaryPrefix prefix = node.getFirstDescendantOfType(ASTPrimaryPrefix.class);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could there be a case, where there is no PrimaryPrefix? Do we need to check for null?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've never seen that case, not sure if that's possible... Maybe @jsotuyod would know?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No, under the current grammar there is always a prefix, although there may be no suffix: https://github.com/pmd/pmd/blob/master/pmd-java/etc/grammar/Java.jjt#L1830-L1834



private String getMethodOrAttributeName(ASTPrimaryExpression node) {
ASTPrimaryPrefix prefix = node.getFirstDescendantOfType(ASTPrimaryPrefix.class);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could there be a case, where there is no PrimaryPrefix? Do we need to check for null?

double totalMethodPairs = n * (n + 1) / 2.0;
return totalMethodPairs;
}
private static final int FEW_ATFD_THRESHOLD = 6;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

FEW was before 5. Any reason why you changed it to 6?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Err no valid reason. I guess it's not uncommon to use getters or setters of the objects you manipulate, and a total of 5 in a class seemed a bit low (to me). I don't know if it makes a difference really, I can put back 5 so as not to change the behaviour too much

public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
int wmc = (int) JavaMetrics.get(JavaClassMetricKey.WMC, node);
double tcc = JavaMetrics.get(JavaClassMetricKey.TCC, node);
int atfd = (int) JavaMetrics.get(JavaClassMetricKey.ATFD, node);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So simple! I like it!

language="java"
since="5.0"
message="Possible God class"
message="Possible God Class (WMC={0}, ATFD={2}, TCC={1})"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice idea to ouput the metric values

<code-ref id="full-example"/>
</test-code>

<test-code>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

you can add the attribute regressionTest="true", then this test case will be ignored.... You can then expect the correct message.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nice, thanks :)

@adangel adangel changed the title [java/design] Refactored god class rule [java] Refactored god class rule Aug 26, 2017
adangel added a commit to adangel/pmd that referenced this pull request Aug 26, 2017
@adangel adangel merged commit 5a2fc3e into pmd:master Aug 26, 2017
@oowekyala oowekyala deleted the god-class-rule branch August 26, 2017 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in:metrics Affects the metrics framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants