Batchable & Schedulable class has cyclomatic complexity of 48

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












Background



I have a class which implements Database.Batchable<Sobject> and Schedulable and when I run PMD I get this problem:




The class 'MyClassBatchJob' has a total cyclomatic complexity of 48 (highest 5).




Questions



  1. How is the Apex cyclomatic complexity calculated?

  2. How should I approach Batchable & Schedulable classes to reduce cyclomatic complexity?









share|improve this question





















  • Bear in mind that such metrics are great for drawing attention to parts of your code base that might benefit from some refactoring. The real metric though is more subjective and is whether others can easily and safely understand and modify the code in the future. That has multiple aspects including using good naming.
    – Keith C
    2 hours ago
















up vote
1
down vote

favorite












Background



I have a class which implements Database.Batchable<Sobject> and Schedulable and when I run PMD I get this problem:




The class 'MyClassBatchJob' has a total cyclomatic complexity of 48 (highest 5).




Questions



  1. How is the Apex cyclomatic complexity calculated?

  2. How should I approach Batchable & Schedulable classes to reduce cyclomatic complexity?









share|improve this question





















  • Bear in mind that such metrics are great for drawing attention to parts of your code base that might benefit from some refactoring. The real metric though is more subjective and is whether others can easily and safely understand and modify the code in the future. That has multiple aspects including using good naming.
    – Keith C
    2 hours ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Background



I have a class which implements Database.Batchable<Sobject> and Schedulable and when I run PMD I get this problem:




The class 'MyClassBatchJob' has a total cyclomatic complexity of 48 (highest 5).




Questions



  1. How is the Apex cyclomatic complexity calculated?

  2. How should I approach Batchable & Schedulable classes to reduce cyclomatic complexity?









share|improve this question













Background



I have a class which implements Database.Batchable<Sobject> and Schedulable and when I run PMD I get this problem:




The class 'MyClassBatchJob' has a total cyclomatic complexity of 48 (highest 5).




Questions



  1. How is the Apex cyclomatic complexity calculated?

  2. How should I approach Batchable & Schedulable classes to reduce cyclomatic complexity?






apex batch scheduled-apex pmd cyclomatic-complexity






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 hours ago









Robs

1,208323




1,208323











  • Bear in mind that such metrics are great for drawing attention to parts of your code base that might benefit from some refactoring. The real metric though is more subjective and is whether others can easily and safely understand and modify the code in the future. That has multiple aspects including using good naming.
    – Keith C
    2 hours ago
















  • Bear in mind that such metrics are great for drawing attention to parts of your code base that might benefit from some refactoring. The real metric though is more subjective and is whether others can easily and safely understand and modify the code in the future. That has multiple aspects including using good naming.
    – Keith C
    2 hours ago















Bear in mind that such metrics are great for drawing attention to parts of your code base that might benefit from some refactoring. The real metric though is more subjective and is whether others can easily and safely understand and modify the code in the future. That has multiple aspects including using good naming.
– Keith C
2 hours ago




Bear in mind that such metrics are great for drawing attention to parts of your code base that might benefit from some refactoring. The real metric though is more subjective and is whether others can easily and safely understand and modify the code in the future. That has multiple aspects including using good naming.
– Keith C
2 hours ago










1 Answer
1






active

oldest

votes

















up vote
3
down vote













Roughly speaking, too many branching constructs (if, for, etc.) in a single method. It's usually a sign that your methods could be broken down into more readable chunks.



Their complete definition:




The complexity of methods directly affects maintenance costs and
readability. Concentrating too much decisional logic in a single
method makes its behaviour hard to read and change.



Cyclomatic complexity assesses the complexity of a method by counting
the number of decision points in a method, plus one for the method
entry. Decision points are places where the control flow jumps to
another place in the program. As such, they include all control flow
statements, such as ‘if’, ‘while’, ‘for’, and ‘case’.



Generally, numbers ranging from 1-4 denote low complexity, 5-7 denote
moderate complexity, 8-10 denote high complexity, and 11+ is very high
complexity. By default, this rule reports methods with a complexity >=
10. Additionnally, classes with many methods of moderate complexity get reported as well once the total of their methods’ complexities
reaches 40, even if none of the methods was directly reported.



Reported methods should be broken down into several smaller methods.
Reported classes should probably be broken down into subcomponents.




https://pmd.github.io/pmd-6.7.0/pmd_rules_apex_design.html#cyclomaticcomplexity



Edit



Re-reading your message, it looks like it's probably that the class has too many methods and the total is too high, rather than any particular method being very bad. So, you should probably break the class into a few classes, each of which only does one thing. If you post your code (maybe as a separate question on how to decompose the class), you may be able to get specific help on doing that.






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "459"
    ;
    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: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f233367%2fbatchable-schedulable-class-has-cyclomatic-complexity-of-48%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    Roughly speaking, too many branching constructs (if, for, etc.) in a single method. It's usually a sign that your methods could be broken down into more readable chunks.



    Their complete definition:




    The complexity of methods directly affects maintenance costs and
    readability. Concentrating too much decisional logic in a single
    method makes its behaviour hard to read and change.



    Cyclomatic complexity assesses the complexity of a method by counting
    the number of decision points in a method, plus one for the method
    entry. Decision points are places where the control flow jumps to
    another place in the program. As such, they include all control flow
    statements, such as ‘if’, ‘while’, ‘for’, and ‘case’.



    Generally, numbers ranging from 1-4 denote low complexity, 5-7 denote
    moderate complexity, 8-10 denote high complexity, and 11+ is very high
    complexity. By default, this rule reports methods with a complexity >=
    10. Additionnally, classes with many methods of moderate complexity get reported as well once the total of their methods’ complexities
    reaches 40, even if none of the methods was directly reported.



    Reported methods should be broken down into several smaller methods.
    Reported classes should probably be broken down into subcomponents.




    https://pmd.github.io/pmd-6.7.0/pmd_rules_apex_design.html#cyclomaticcomplexity



    Edit



    Re-reading your message, it looks like it's probably that the class has too many methods and the total is too high, rather than any particular method being very bad. So, you should probably break the class into a few classes, each of which only does one thing. If you post your code (maybe as a separate question on how to decompose the class), you may be able to get specific help on doing that.






    share|improve this answer
























      up vote
      3
      down vote













      Roughly speaking, too many branching constructs (if, for, etc.) in a single method. It's usually a sign that your methods could be broken down into more readable chunks.



      Their complete definition:




      The complexity of methods directly affects maintenance costs and
      readability. Concentrating too much decisional logic in a single
      method makes its behaviour hard to read and change.



      Cyclomatic complexity assesses the complexity of a method by counting
      the number of decision points in a method, plus one for the method
      entry. Decision points are places where the control flow jumps to
      another place in the program. As such, they include all control flow
      statements, such as ‘if’, ‘while’, ‘for’, and ‘case’.



      Generally, numbers ranging from 1-4 denote low complexity, 5-7 denote
      moderate complexity, 8-10 denote high complexity, and 11+ is very high
      complexity. By default, this rule reports methods with a complexity >=
      10. Additionnally, classes with many methods of moderate complexity get reported as well once the total of their methods’ complexities
      reaches 40, even if none of the methods was directly reported.



      Reported methods should be broken down into several smaller methods.
      Reported classes should probably be broken down into subcomponents.




      https://pmd.github.io/pmd-6.7.0/pmd_rules_apex_design.html#cyclomaticcomplexity



      Edit



      Re-reading your message, it looks like it's probably that the class has too many methods and the total is too high, rather than any particular method being very bad. So, you should probably break the class into a few classes, each of which only does one thing. If you post your code (maybe as a separate question on how to decompose the class), you may be able to get specific help on doing that.






      share|improve this answer






















        up vote
        3
        down vote










        up vote
        3
        down vote









        Roughly speaking, too many branching constructs (if, for, etc.) in a single method. It's usually a sign that your methods could be broken down into more readable chunks.



        Their complete definition:




        The complexity of methods directly affects maintenance costs and
        readability. Concentrating too much decisional logic in a single
        method makes its behaviour hard to read and change.



        Cyclomatic complexity assesses the complexity of a method by counting
        the number of decision points in a method, plus one for the method
        entry. Decision points are places where the control flow jumps to
        another place in the program. As such, they include all control flow
        statements, such as ‘if’, ‘while’, ‘for’, and ‘case’.



        Generally, numbers ranging from 1-4 denote low complexity, 5-7 denote
        moderate complexity, 8-10 denote high complexity, and 11+ is very high
        complexity. By default, this rule reports methods with a complexity >=
        10. Additionnally, classes with many methods of moderate complexity get reported as well once the total of their methods’ complexities
        reaches 40, even if none of the methods was directly reported.



        Reported methods should be broken down into several smaller methods.
        Reported classes should probably be broken down into subcomponents.




        https://pmd.github.io/pmd-6.7.0/pmd_rules_apex_design.html#cyclomaticcomplexity



        Edit



        Re-reading your message, it looks like it's probably that the class has too many methods and the total is too high, rather than any particular method being very bad. So, you should probably break the class into a few classes, each of which only does one thing. If you post your code (maybe as a separate question on how to decompose the class), you may be able to get specific help on doing that.






        share|improve this answer












        Roughly speaking, too many branching constructs (if, for, etc.) in a single method. It's usually a sign that your methods could be broken down into more readable chunks.



        Their complete definition:




        The complexity of methods directly affects maintenance costs and
        readability. Concentrating too much decisional logic in a single
        method makes its behaviour hard to read and change.



        Cyclomatic complexity assesses the complexity of a method by counting
        the number of decision points in a method, plus one for the method
        entry. Decision points are places where the control flow jumps to
        another place in the program. As such, they include all control flow
        statements, such as ‘if’, ‘while’, ‘for’, and ‘case’.



        Generally, numbers ranging from 1-4 denote low complexity, 5-7 denote
        moderate complexity, 8-10 denote high complexity, and 11+ is very high
        complexity. By default, this rule reports methods with a complexity >=
        10. Additionnally, classes with many methods of moderate complexity get reported as well once the total of their methods’ complexities
        reaches 40, even if none of the methods was directly reported.



        Reported methods should be broken down into several smaller methods.
        Reported classes should probably be broken down into subcomponents.




        https://pmd.github.io/pmd-6.7.0/pmd_rules_apex_design.html#cyclomaticcomplexity



        Edit



        Re-reading your message, it looks like it's probably that the class has too many methods and the total is too high, rather than any particular method being very bad. So, you should probably break the class into a few classes, each of which only does one thing. If you post your code (maybe as a separate question on how to decompose the class), you may be able to get specific help on doing that.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        Aidan

        5,825937




        5,825937



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f233367%2fbatchable-schedulable-class-has-cyclomatic-complexity-of-48%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Confectionery