I was thinking of changing how CognitiveComplexity
(https://package.elm-lang.org/packages/jfmengels/elm-review-cognitive-complexity/latest/CognitiveComplexity) works.
At the moment, you set a threshold for the whole project, so you're bound to set it at highest value of all the functions, and then (maybe painfully?) lower it by making some functions simpler. But in the meantime, some of the functions in your files might become more complex unnoticeably up to that threshold.
I just added the rule to a codebase where the threshold has been set to 110, because there are some very large update
functions in old modules that will take some time to simplify. This is a very large number, and I get the feeling that the rule will stay where it's at for a long time because the first obstacle would be annoying to solve. In the meantime, complexity in new or currently okay files would rise unnoticeably without the rule helping preventing it.
So my thinking is that we could have a configuration for the rule like this
module ReviewConfig exposing (config)
import CognitiveComplexity
import CognitiveComplexitySuppressions
import Review.Rule exposing (Rule)
config : List Rule
config =
[ CognitiveComplexity.rule CognitiveComplexityConfiguration.config
]
and then have another file like this
module CognitiveComplexityConfiguration exposing (config)
{-| Generated by the CognitiveComplexity rule.
These suppressions are here to help you gradually lower the complexity of your modules.
-}
import CognitiveComplexity
config : CognitiveComplexity.Config
config =
{ threshold = 10
, suppressions = suppressions
}
suppressions : List ( String, Int )
suppressions =
[ { moduleName = "Some.Module", threshold = 64 }
, { moduleName = "Some.Other.Module", threshold = 14 }
]
My idea would be to have the rule generate this configuration when you configure it like CognitiveComplexity.generateComplexityCoverage { threshold = 10 }
, in a global error like:
CognitiveComplexity: Here is the generated file. Please change your configuration to <what I showed above> and create a file with the following content:
<contents of the CognitiveComplexityConfiguration file above>
This would generate a list with all modules where the threshold is the complexity of the most complex function in that file. Modules with complexities under the global threshold would not be listed.
Then once you have re-configured the rule, the threshold in effect for a module is the one listed in suppressions
(better name suggestions welcome) and the global threshold if not listed.
If no errors are being reported, but the maximum threshold for a module has changed to be lower than before, then you'd have a global error like the one above, asking you to copy and paste the contents inside CognitiveComplexityConfiguration.elm
, so that end threshold becomes increasingly low.
What do you think of the idea?