Possible new rule: nested ternary operations

297 views
Skip to first unread message

thomas....@gmail.com

unread,
Sep 9, 2015, 3:50:57 AM9/9/15
to SonarQube
Hello everyone,

in my team I permit the usage of ternary operators. What I would like to prohibit is the usage of nested ternary operations.

e.g.
int x = y == null ? 0 : (z == null ? -1 : z);


Maybe the existing rule can even be extended in a way to define the depth that is allowed. Zero equals to prohibition at all.

Best regards,
Thomas

Nicolas Peru

unread,
Sep 9, 2015, 3:57:53 AM9/9/15
to thomas....@gmail.com, SonarQube
Hi, 

If I understand you correctly you would like to add a parameter to this rule to track nesting level of ternary ?
https://jira.sonarsource.com/browse/RSPEC-1774

Cheers,

Nicolas PERU | SonarSource
Senior Developer
http://sonarsource.com


--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/3714b30e-a3ff-4236-a484-1a22ad88b192%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Racodon

unread,
Sep 9, 2015, 4:05:23 AM9/9/15
to Nicolas Peru, thomas....@gmail.com, SonarQube
Hi Nicolas,

I'm also interested in such a rule.
But I would prefer to have two distinct rules instead of a parameter:
  • The ternary operator should not be used
  • Ternary operators should not be nested
I found it clearer this way.

Thank you

Regards,

David RACODON
Freelance QA Consultant

thomas....@gmail.com

unread,
Sep 9, 2015, 4:10:13 AM9/9/15
to SonarQube, thomas....@gmail.com
Hi Nicolas,

I don't mind how to solve that. The extension is only one possible solution. I would also agree with Davids suggestion.

Best regards,
Thomas

thomas....@gmail.com

unread,
Oct 2, 2015, 8:07:00 AM10/2/15
to SonarQube, thomas....@gmail.com
Hi Nicolas,

is there any update on this possible rule/extension?

Nicolas Peru

unread,
Oct 7, 2015, 6:06:11 AM10/7/15
to Thomas McWork, SonarQube
Hi Thomas, 

Ok, let's create a rule to avoid nesting of ternary operators. 

@Ann I stubbbed the RSPEC, can you please complete it : https://jira.sonarsource.com/browse/RSPEC-3358

Thanks.

Cheers,

Nicolas PERU | SonarSource
Senior Developer
http://sonarsource.com


G. Ann Campbell

unread,
Oct 7, 2015, 8:19:54 AM10/7/15
to SonarQube, thomas....@gmail.com
Done @Nicolas
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+unsubscribe@googlegroups.com.

Thomas McWork

unread,
Jan 24, 2017, 7:16:27 AM1/24/17
to G. Ann Campbell, SonarQube
Hi Ann and Nicolas,

I was just searching for this rule for Java and didn't find it. Your repository states there are only JavaScript and C implementations. Could you please also implements this for Java?

Thanks in advance.

Best regards,
Thomas

Nicolas Peru

unread,
Jan 24, 2017, 7:20:21 AM1/24/17
to Thomas McWork, G. Ann Campbell, SonarQube
Hi, 

That's odd : it is implemented in SonarJava : https://sonarqube.com/coding_rules#rule_key=squid%3AS1774 

Cheers, 

Done @Nicolas

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

G. Ann Campbell

unread,
Jan 24, 2017, 7:27:15 AM1/24/17
to Nicolas Peru, Thomas McWork, SonarQube
That's a different rule @Nico. S1774 is 'don't use ternary ops at all', S3358 is 'don't put a ternary inside a ternary'.


Ann



---
G. Ann CAMPBELL | SonarSource
Product Manager

Done @Nicolas
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+unsubscribe@googlegroups.com.

Nicolas Peru

unread,
Jan 24, 2017, 7:44:21 AM1/24/17
to Thomas McWork, SonarQube
Oh indeed, my bad. 

so it is not implemented yet. 
I created a ticket to track it : https://jira.sonarsource.com/browse/SONARJAVA-2068 

Cheers, 
--
Reply all
Reply to author
Forward
0 new messages