How to count the number of "if-else" with semantic API?

I’m totally newbie in sonarqube.
I’m trying to write a custom rule to find where there are more than 3 “if-else” in the code.
Then mark it as a place where the programmer can apply the strategy design pattern.

Can someone give me a way?
I really want to learn how to use the SonarQube semantic API.
Thanks you!!



Welcome to the community!

What language are you working on?


Thanks a lot!
I use JAVA language.

Hey @Liver
The documentation on custom rules development is usually the right place to start.

To build the rule you are talking about, I would look in the examples at the NoIfStatementRule that tests if there are if statements in Junit4 code.

If you want to dive deeper in the code, the CognitiveComplexityVisitor counts different elements in the code and takes the level of nesting into account.


Tanks a lot!
I will try it!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.