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

WMC for conditional statements #31

Open
aaghamohammadi opened this issue Dec 6, 2019 · 9 comments
Open

WMC for conditional statements #31

aaghamohammadi opened this issue Dec 6, 2019 · 9 comments

Comments

@aaghamohammadi
Copy link
Contributor

aaghamohammadi commented Dec 6, 2019

Hello,

I think there is a problem in calculating the WMC metric based on cyclomatic complexity.
For example, consider the following methods:

public class CC6 {
    public boolean m1(int a) {
        if (0 <= a && a <= 10) {
            return true;
        }
        return false;
    }

    public boolean m2(int a) {
        boolean cond = (0 <= a && a <= 10);
        if (cond) {
            return true;
        }
        return false;
    }

    public boolean m3(int a) {
        if (0 <= a <= 10) {
            return true;
        }
        return false;
    }
}

All these three methods do the same thing. However, the WMC complexities for m1,m2, and m3 are 3,2, and 2 respectively. In fact, the following inline conditional statement

boolean cond = (0 <= a && a <= 10);

does not increase CC complexity at all. Furthermore, when a condition becomes compact such as

if (0 <= a <= 10)

you increase CC complexity just by one. I know the original paper does not take into account situations like that. However, don't you think it would be better to consider that?

@mauricioaniche
Copy link
Owner

Hi, @aaghamohammadi! Sorry for the slow reply. For some reason, I never got an email about your issue.

You have definitely a good point. For now, the WMC calculation in the tool is basically "a count" of specific instructions, e.g., && and || in ifs, etc. Apparently, it's not working well for inline instructions. I'll work on that!

Keep you posted!

@aaghamohammadi
Copy link
Contributor Author

Dear @mauricioaniche,

Thank you for your reply. I have another issue with Cyclomatic Complexity. It ignores nesting complexity. For example, When developers apply a popular refactoring method named "Replace Nested Conditional with Gaurd Clauses" (link), the structure of the source code really improves. However, the CC metric (by definition) does not change. Some researchers have proposed a few solutions. Surprisingly, the best solution (in my opinion) has the minimum citation (link).

@mauricioaniche
Copy link
Owner

@aaghamohammadi Can you open another issue for this second suggestion (a concrete example like the one you provided here is very useful!)? So that we don't mix the discussions.

@aaghamohammadi
Copy link
Contributor Author

@mauricioaniche Done.

@mauricioaniche
Copy link
Owner

Still working on it. To differentiate between m1() and m2(), we need some fancier semantic analysis, so that it understands that they are similar in terms of complexity. Maybe a simple heuristic can be that boolean variables that are declared in the method body are ignored by the visitor, when we visit the If and we measure its complexity. I have to estimate the effort here, and whether it is actually worth the effort...

I, however, improved a lot the WMC, by adding a lot of tests, and fixing some bugs in very specific corner cases.

Note that if (0 <= a <= 10) is not a valid Java construct.

@mauricioaniche
Copy link
Owner

Changing the label here, as this is not a bug anymore, but an enhancement.

@aaghamohammadi
Copy link
Contributor Author

@mauricioaniche
Maybe you won't need a semantic analysis. If you have access to the String representation of a statement s, then all you need is regex analysis. I think the most elegant implementation would be the Observer pattern. When a statement s, contains a logical operand, the subscriber should be notified to capture its clauses.

@aaghamohammadi
Copy link
Contributor Author

aaghamohammadi commented Feb 13, 2020

Oh, I see what you mean. Sorry for my mistake, you definitely need some semantic analysis.

@mauricioaniche
Copy link
Owner

I'll keep this one open... Maybe we'll find a simple way to do this semantic check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants