Can Ternary operators should not be nested (squid:S3358) be configured
up vote
0
down vote
favorite
When I have the following code with 2 levels of Ternary operations
double amount = isValid ? (isTypeA ? vo.getTypeA() : vo.getTypeB()) : 0;
Which Sonar warns about
Ternary operators should not be nested (squid:S3358)
Just because you can do something, doesn't mean you should, and that's the case with nested ternary operations. Nesting ternary operators results in the kind of code that may seem clear as day when you write it, but six months later will leave maintainers (or worse - future you) scratching their heads and cursing.
Instead, err on the side of clarity, and use another line to express the nested operation as a separate statement.
My colleague suggested that such level can be accepted and it's more clear than the alternative.
I wonder if this rule (or others) can be configured to allowed levels limit?
If not, why sonar is so strict when it deals with code conventions?
I don't want to ignore rule, just to customize to allow up to 2 levels instead of 1.
java sonarqube sonarlint sonarlint-eclipse
add a comment |
up vote
0
down vote
favorite
When I have the following code with 2 levels of Ternary operations
double amount = isValid ? (isTypeA ? vo.getTypeA() : vo.getTypeB()) : 0;
Which Sonar warns about
Ternary operators should not be nested (squid:S3358)
Just because you can do something, doesn't mean you should, and that's the case with nested ternary operations. Nesting ternary operators results in the kind of code that may seem clear as day when you write it, but six months later will leave maintainers (or worse - future you) scratching their heads and cursing.
Instead, err on the side of clarity, and use another line to express the nested operation as a separate statement.
My colleague suggested that such level can be accepted and it's more clear than the alternative.
I wonder if this rule (or others) can be configured to allowed levels limit?
If not, why sonar is so strict when it deals with code conventions?
I don't want to ignore rule, just to customize to allow up to 2 levels instead of 1.
java sonarqube sonarlint sonarlint-eclipse
I am with Sonar on this. This should be changed to nested-if.
– Kartik
Nov 19 at 5:59
@Kartik I'm too, but there are opinions that reject it, can sonar be more flexible with this rule?
– user7294900
Nov 19 at 6:00
@Kartik I don't want to ignore, I want to customize, allow up to 2 levels instead of 1
– user7294900
Nov 19 at 6:06
oh sorry then, no idea.. retracting the duplicate flag
– Kartik
Nov 19 at 6:08
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
When I have the following code with 2 levels of Ternary operations
double amount = isValid ? (isTypeA ? vo.getTypeA() : vo.getTypeB()) : 0;
Which Sonar warns about
Ternary operators should not be nested (squid:S3358)
Just because you can do something, doesn't mean you should, and that's the case with nested ternary operations. Nesting ternary operators results in the kind of code that may seem clear as day when you write it, but six months later will leave maintainers (or worse - future you) scratching their heads and cursing.
Instead, err on the side of clarity, and use another line to express the nested operation as a separate statement.
My colleague suggested that such level can be accepted and it's more clear than the alternative.
I wonder if this rule (or others) can be configured to allowed levels limit?
If not, why sonar is so strict when it deals with code conventions?
I don't want to ignore rule, just to customize to allow up to 2 levels instead of 1.
java sonarqube sonarlint sonarlint-eclipse
When I have the following code with 2 levels of Ternary operations
double amount = isValid ? (isTypeA ? vo.getTypeA() : vo.getTypeB()) : 0;
Which Sonar warns about
Ternary operators should not be nested (squid:S3358)
Just because you can do something, doesn't mean you should, and that's the case with nested ternary operations. Nesting ternary operators results in the kind of code that may seem clear as day when you write it, but six months later will leave maintainers (or worse - future you) scratching their heads and cursing.
Instead, err on the side of clarity, and use another line to express the nested operation as a separate statement.
My colleague suggested that such level can be accepted and it's more clear than the alternative.
I wonder if this rule (or others) can be configured to allowed levels limit?
If not, why sonar is so strict when it deals with code conventions?
I don't want to ignore rule, just to customize to allow up to 2 levels instead of 1.
java sonarqube sonarlint sonarlint-eclipse
java sonarqube sonarlint sonarlint-eclipse
edited Nov 19 at 6:07
asked Nov 19 at 5:56
user7294900
18.7k93056
18.7k93056
I am with Sonar on this. This should be changed to nested-if.
– Kartik
Nov 19 at 5:59
@Kartik I'm too, but there are opinions that reject it, can sonar be more flexible with this rule?
– user7294900
Nov 19 at 6:00
@Kartik I don't want to ignore, I want to customize, allow up to 2 levels instead of 1
– user7294900
Nov 19 at 6:06
oh sorry then, no idea.. retracting the duplicate flag
– Kartik
Nov 19 at 6:08
add a comment |
I am with Sonar on this. This should be changed to nested-if.
– Kartik
Nov 19 at 5:59
@Kartik I'm too, but there are opinions that reject it, can sonar be more flexible with this rule?
– user7294900
Nov 19 at 6:00
@Kartik I don't want to ignore, I want to customize, allow up to 2 levels instead of 1
– user7294900
Nov 19 at 6:06
oh sorry then, no idea.. retracting the duplicate flag
– Kartik
Nov 19 at 6:08
I am with Sonar on this. This should be changed to nested-if.
– Kartik
Nov 19 at 5:59
I am with Sonar on this. This should be changed to nested-if.
– Kartik
Nov 19 at 5:59
@Kartik I'm too, but there are opinions that reject it, can sonar be more flexible with this rule?
– user7294900
Nov 19 at 6:00
@Kartik I'm too, but there are opinions that reject it, can sonar be more flexible with this rule?
– user7294900
Nov 19 at 6:00
@Kartik I don't want to ignore, I want to customize, allow up to 2 levels instead of 1
– user7294900
Nov 19 at 6:06
@Kartik I don't want to ignore, I want to customize, allow up to 2 levels instead of 1
– user7294900
Nov 19 at 6:06
oh sorry then, no idea.. retracting the duplicate flag
– Kartik
Nov 19 at 6:08
oh sorry then, no idea.. retracting the duplicate flag
– Kartik
Nov 19 at 6:08
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
I wonder if this rule can be configured to allowed levels limit?
The Ternary operators should not be nested rule cannot be configured. You are only able to enable or disable it.
I wonder if other rules can be configured to allowed levels limit?
I don't know any existing rule which can do it. Luckily, you are able to create a custom analyzer. The original rule class is here NestedTernaryOperatorsCheck. You can simply copy it and adjust to your needs.
why sonar is so strict when it deals with code conventions?
SonarSource provides a lot of rules for different languages. Every customization makes code more difficult to maintain. They have a limited capacity, so they have to make decisions which are unaccepted by all users (but are accepted by most of them).
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I wonder if this rule can be configured to allowed levels limit?
The Ternary operators should not be nested rule cannot be configured. You are only able to enable or disable it.
I wonder if other rules can be configured to allowed levels limit?
I don't know any existing rule which can do it. Luckily, you are able to create a custom analyzer. The original rule class is here NestedTernaryOperatorsCheck. You can simply copy it and adjust to your needs.
why sonar is so strict when it deals with code conventions?
SonarSource provides a lot of rules for different languages. Every customization makes code more difficult to maintain. They have a limited capacity, so they have to make decisions which are unaccepted by all users (but are accepted by most of them).
add a comment |
up vote
1
down vote
accepted
I wonder if this rule can be configured to allowed levels limit?
The Ternary operators should not be nested rule cannot be configured. You are only able to enable or disable it.
I wonder if other rules can be configured to allowed levels limit?
I don't know any existing rule which can do it. Luckily, you are able to create a custom analyzer. The original rule class is here NestedTernaryOperatorsCheck. You can simply copy it and adjust to your needs.
why sonar is so strict when it deals with code conventions?
SonarSource provides a lot of rules for different languages. Every customization makes code more difficult to maintain. They have a limited capacity, so they have to make decisions which are unaccepted by all users (but are accepted by most of them).
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I wonder if this rule can be configured to allowed levels limit?
The Ternary operators should not be nested rule cannot be configured. You are only able to enable or disable it.
I wonder if other rules can be configured to allowed levels limit?
I don't know any existing rule which can do it. Luckily, you are able to create a custom analyzer. The original rule class is here NestedTernaryOperatorsCheck. You can simply copy it and adjust to your needs.
why sonar is so strict when it deals with code conventions?
SonarSource provides a lot of rules for different languages. Every customization makes code more difficult to maintain. They have a limited capacity, so they have to make decisions which are unaccepted by all users (but are accepted by most of them).
I wonder if this rule can be configured to allowed levels limit?
The Ternary operators should not be nested rule cannot be configured. You are only able to enable or disable it.
I wonder if other rules can be configured to allowed levels limit?
I don't know any existing rule which can do it. Luckily, you are able to create a custom analyzer. The original rule class is here NestedTernaryOperatorsCheck. You can simply copy it and adjust to your needs.
why sonar is so strict when it deals with code conventions?
SonarSource provides a lot of rules for different languages. Every customization makes code more difficult to maintain. They have a limited capacity, so they have to make decisions which are unaccepted by all users (but are accepted by most of them).
answered Nov 19 at 21:49
agabrys
3,98121340
3,98121340
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369046%2fcan-ternary-operators-should-not-be-nested-squids3358-be-configured%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I am with Sonar on this. This should be changed to nested-if.
– Kartik
Nov 19 at 5:59
@Kartik I'm too, but there are opinions that reject it, can sonar be more flexible with this rule?
– user7294900
Nov 19 at 6:00
@Kartik I don't want to ignore, I want to customize, allow up to 2 levels instead of 1
– user7294900
Nov 19 at 6:06
oh sorry then, no idea.. retracting the duplicate flag
– Kartik
Nov 19 at 6:08