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.










share|improve this question
























  • 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















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.










share|improve this question
























  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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).






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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).






    share|improve this answer

























      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).






      share|improve this answer























        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).






        share|improve this answer













        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).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 21:49









        agabrys

        3,98121340




        3,98121340






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga