Reduce cognitive complexity in five scikit-learn modules through helper method extraction#642
Reduce cognitive complexity in five scikit-learn modules through helper method extraction#642sonarqube-agent[bot] wants to merge 1 commit into
Conversation
Fixed issues: - AZ45CyQmRXnEWm2Rf5b- for python:S3776 rule - AZ45CvRaRXnEWm2Rf4zO for python:S3776 rule - AZ45CvTuRXnEWm2Rf4zc for python:S3776 rule - AZ45C0KNRXnEWm2Rf6GB for python:S3776 rule - AZ45CuuSRXnEWm2Rf4p7 for python:S3776 rule Generated by SonarQube Agent (task: 972fb7d6-c81c-4574-a99e-7b77474d2ab8)
|
|
SonarQube reviewer guideSummary: Refactor multiple modules to improve code maintainability by extracting repeated logic into helper methods. Review Focus:
Start review at:
|




This change addresses five CRITICAL SonarQube violations of the Cognitive Complexity rule (python:S3776) by extracting complex nested logic into dedicated helper methods. By decomposing deeply nested conditionals in the discriminant analysis, SVM light format I/O, dictionary learning, histogram gradient boosting, and LDA modules, the refactoring improves code maintainability and readability while bringing these functions into compliance with complexity thresholds.
View Project in SonarCloud
Fixed Issues
python:S3776 - Refactor this function to reduce its Cognitive Complexity from 24 to the 15 allowed. • CRITICAL • View issue
Location:
sklearn/discriminant_analysis.py:1054Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
This hunk extracts the rank-checking and error-raising logic from the
fitmethod into a new helper method_check_rank. By moving this deeply nested code (which contained multiple conditionals includingif rank < n_features,if self.solver == 'svd' and n_samples_class <= n_features, and the ternaryif self.solver == 'eigen' else 'reg_param') out of thefitmethod, it reduces the cognitive complexity offit. The nested conditionals that were contributing significantly to the complexity score (flows involving +2 and +3 for nesting, plus +1 for mixed conditions) are now in a separate method where they start at a lower nesting level, thus reducing the overall cognitive complexity of thefitfunction from 24 toward the allowed threshold of 15.python:S3776 - Refactor this function to reduce its Cognitive Complexity from 31 to the 15 allowed. • CRITICAL • View issue
Location:
sklearn/datasets/_svmlight_format_io.py:463Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
Defines two helper functions, _validate_comment and _sort_sparse_indices, that extract complex logic out of the dump_svmlight_file function. The comment validation logic (isinstance check, bytes decoding, NUL byte check) and the sparse index sorting logic (with nested conditionals and hasattr checks) are moved into these smaller, focused functions. This reduces the cognitive complexity of dump_svmlight_file by removing multiple nested conditionals and branching paths from the main function body.
python:S3776 - Refactor this function to reduce its Cognitive Complexity from 25 to the 15 allowed. • CRITICAL • View issue
Location:
sklearn/decomposition/_dict_learning.py:548Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
This hunk extracts the initialization logic (for code and dictionary) from the
_dict_learningfunction into a new helper function_initialize_code_and_dictionary. This reduces the cognitive complexity of_dict_learningby moving conditional branches (theif code_init and dict_init is not None/elseandif n_components/elseblocks) out of the main function, helping bring the cognitive complexity score from 25 down toward the allowed threshold of 15.python:S3776 - Refactor this function to reduce its Cognitive Complexity from 30 to the 15 allowed. • CRITICAL • View issue
Location:
sklearn/ensemble/_hist_gradient_boosting/grower.py:527Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
This hunk replaces a deeply nested block of monotonic constraint logic (with multiple if/else branches) in the
split_nextmethod with a single method call to_apply_monotonic_constraints. By extracting this logic into a separate method, it reduces the cognitive complexity ofsplit_nextby removing several nested conditionals (the NO_CST check, the POS check, and the NEG else branch), which directly reduces the function's cognitive complexity score toward the allowed threshold of 15.python:S3776 - Refactor this function to reduce its Cognitive Complexity from 18 to the 15 allowed. • CRITICAL • View issue
Location:
sklearn/decomposition/_lda.py:637Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
This hunk extracts the perplexity evaluation and convergence checking logic from the
fitmethod into a new helper method_check_perplexity_and_convergence. This reduces the cognitive complexity of thefitmethod by moving deeply nested conditional logic (theif evaluate_every,if self.verbose,if last_bound and abs(...), andelif self.verboseblocks) out of the main loop body and into a separate, well-named function. This directly addresses the code smell warning about thefitmethod having a cognitive complexity of 18, which exceeds the allowed threshold of 15.SonarQube Remediation Agent uses AI. Check for mistakes.