Function stem
has a Cognitive Complexity of 199 (exceeds 5 allowed). Consider refactoring. Open
def stem(self, word: str) -> str:
"""Return the Porter2 (Snowball English) stem.
Parameters
----------
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Cyclomatic complexity is too high in method stem. (127) Open
def stem(self, word: str) -> str:
"""Return the Porter2 (Snowball English) stem.
Parameters
----------
- Read upRead up
- Exclude checks
Cyclomatic Complexity
Cyclomatic Complexity corresponds to the number of decisions a block of code contains plus 1. This number (also called McCabe number) is equal to the number of linearly independent paths through the code. This number can be used as a guide when testing conditional logic in blocks.
Radon analyzes the AST tree of a Python program to compute Cyclomatic Complexity. Statements have the following effects on Cyclomatic Complexity:
Construct | Effect on CC | Reasoning |
---|---|---|
if | +1 | An if statement is a single decision. |
elif | +1 | The elif statement adds another decision. |
else | +0 | The else statement does not cause a new decision. The decision is at the if. |
for | +1 | There is a decision at the start of the loop. |
while | +1 | There is a decision at the while statement. |
except | +1 | Each except branch adds a new conditional path of execution. |
finally | +0 | The finally block is unconditionally executed. |
with | +1 | The with statement roughly corresponds to a try/except block (see PEP 343 for details). |
assert | +1 | The assert statement internally roughly equals a conditional statement. |
Comprehension | +1 | A list/set/dict comprehension of generator expression is equivalent to a for loop. |
Boolean Operator | +1 | Every boolean operator (and, or) adds a decision point. |
Cyclomatic complexity is too high in class Porter2. (64) Open
class Porter2(_Snowball):
"""Porter2 (Snowball English) stemmer.
The Porter2 (Snowball English) stemmer is defined in :cite:`Porter:2002`.
- Read upRead up
- Exclude checks
Cyclomatic Complexity
Cyclomatic Complexity corresponds to the number of decisions a block of code contains plus 1. This number (also called McCabe number) is equal to the number of linearly independent paths through the code. This number can be used as a guide when testing conditional logic in blocks.
Radon analyzes the AST tree of a Python program to compute Cyclomatic Complexity. Statements have the following effects on Cyclomatic Complexity:
Construct | Effect on CC | Reasoning |
---|---|---|
if | +1 | An if statement is a single decision. |
elif | +1 | The elif statement adds another decision. |
else | +0 | The else statement does not cause a new decision. The decision is at the if. |
for | +1 | There is a decision at the start of the loop. |
while | +1 | There is a decision at the while statement. |
except | +1 | Each except branch adds a new conditional path of execution. |
finally | +0 | The finally block is unconditionally executed. |
with | +1 | The with statement roughly corresponds to a try/except block (see PEP 343 for details). |
assert | +1 | The assert statement internally roughly equals a conditional statement. |
Comprehension | +1 | A list/set/dict comprehension of generator expression is equivalent to a for loop. |
Boolean Operator | +1 | Every boolean operator (and, or) adds a decision point. |
File _porter2.py
has 310 lines of code (exceeds 250 allowed). Consider refactoring. Open
# Copyright 2014-2020 by Christopher C. Little.
# This file is part of Abydos.
#
# Abydos is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Avoid too many return
statements within this function. Open
return word
Avoid too many return
statements within this function. Open
return word
Avoid too many return
statements within this function. Open
return word
Refactor this function to reduce its Cognitive Complexity from 201 to the 15 allowed. Open
def stem(self, word: str) -> str:
- Read upRead up
- Exclude checks
Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.
See
Too many return statements (7/6) Open
def stem(self, word: str) -> str:
- Read upRead up
- Exclude checks
Used when a function or method has too many return statement, making it hard to follow.
Too many statements (177/50) Open
def stem(self, word: str) -> str:
- Read upRead up
- Exclude checks
Used when a function or method has too many statements. You should then split it in smaller functions / methods.
Too few public methods (1/2) Open
class Porter2(_Snowball):
- Read upRead up
- Exclude checks
Used when class has too few public methods, so be sure it's really worth it.
Unnecessary elif
after return
Open
if word in self._exception1dict:
- Read upRead up
- Exclude checks
Used in order to highlight an unnecessary block of code following an if containing a return statement. As such, it will warn when it encounters an else following a chain of ifs, all of them containing a return statement.
Too many branches (114/12) Open
def stem(self, word: str) -> str:
- Read upRead up
- Exclude checks
Used when a function or method has too many branches, making it hard to follow.
Merge this if statement with the enclosing one. Open
if (
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if condition1: if condition2: # ...
Compliant Solution
if condition1 and condition2: # ...
Merge this if statement with the enclosing one. Open
if len(word[r1_start:]) >= 4:
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if condition1: if condition2: # ...
Compliant Solution
if condition1 and condition2: # ...
Merge this if statement with the enclosing one. Open
if len(word[r1_start:]) >= 4:
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if condition1: if condition2: # ...
Compliant Solution
if condition1 and condition2: # ...
Merge this if statement with the enclosing one. Open
if len(word[r1_start:]) >= 4:
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if condition1: if condition2: # ...
Compliant Solution
if condition1 and condition2: # ...
Merge this if statement with the enclosing one. Open
if (
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if condition1: if condition2: # ...
Compliant Solution
if condition1 and condition2: # ...
Merge this if statement with the enclosing one. Open
if self._sb_has_vowel(word[:-2]):
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if condition1: if condition2: # ...
Compliant Solution
if condition1 and condition2: # ...
Merge this if statement with the enclosing one. Open
if self._sb_has_vowel(word[:-3]):
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if condition1: if condition2: # ...
Compliant Solution
if condition1 and condition2: # ...
Merge this if statement with the enclosing one. Open
if len(word[r1_start:]) >= 6:
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if condition1: if condition2: # ...
Compliant Solution
if condition1 and condition2: # ...
Merge this if statement with the enclosing one. Open
if len(word[r1_start:]) >= 3:
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if condition1: if condition2: # ...
Compliant Solution
if condition1 and condition2: # ...
Merge this if statement with the enclosing one. Open
if len(word[r1_start:]) >= 5:
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if condition1: if condition2: # ...
Compliant Solution
if condition1 and condition2: # ...
Merge this if statement with the enclosing one. Open
if len(word[r1_start:]) >= 5:
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if condition1: if condition2: # ...
Compliant Solution
if condition1 and condition2: # ...
Merge this if statement with the enclosing one. Open
if (
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if condition1: if condition2: # ...
Compliant Solution
if condition1 and condition2: # ...
Merge this if statement with the enclosing one. Open
if len(word[r2_start:]) >= 1 and word[-2] == 'l':
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if condition1: if condition2: # ...
Compliant Solution
if condition1 and condition2: # ...
Either merge this branch with the identical one on line "214" or change one of the implementations. Open
if self._sb_has_vowel(word[:-3]):
word = word[:-3]
step1b_flag = True
- Read upRead up
- Exclude checks
Having two branches in the same if
structure with the same implementation is at best duplicate code, and at worst a coding error. If
the same logic is truly needed for both instances, then they should be combined.
Noncompliant Code Example
if 0 <= a < 10: do_the_thing() elif 10 <= a < 20: do_the_other_thing() elif 20 <= a < 50: do_the_thing() # Noncompliant; duplicates first condition else: do_the_rest() b = 4 if a > 12 else 4
Compliant Solution
if (0 <= a < 10) or (20 <= a < 50): do_the_thing() elif 10 <= a < 20: do_the_other_thing() else: do_the_rest() b = 4
or
if 0 <= a < 10: do_the_thing() elif 10 <= a < 20: do_the_other_thing() elif 20 <= a < 50: do_the_third_thing() else: do_the_rest() b = 8 if a > 12 else 4
Either remove or fill this block of code. Open
pass
- Read upRead up
- Exclude checks
Most of the time a block of code is empty when a piece of code is really missing. So such empty block must be either filled or removed.
Noncompliant Code Example
for i in range(3): pass
Exceptions
When a block contains a comment, this block is not considered to be empty.
Either merge this branch with the identical one on line "224" or change one of the implementations. Open
word += 'e'
- Read upRead up
- Exclude checks
Having two branches in the same if
structure with the same implementation is at best duplicate code, and at worst a coding error. If
the same logic is truly needed for both instances, then they should be combined.
Noncompliant Code Example
if 0 <= a < 10: do_the_thing() elif 10 <= a < 20: do_the_other_thing() elif 20 <= a < 50: do_the_thing() # Noncompliant; duplicates first condition else: do_the_rest() b = 4 if a > 12 else 4
Compliant Solution
if (0 <= a < 10) or (20 <= a < 50): do_the_thing() elif 10 <= a < 20: do_the_other_thing() else: do_the_rest() b = 4
or
if 0 <= a < 10: do_the_thing() elif 10 <= a < 20: do_the_other_thing() elif 20 <= a < 50: do_the_third_thing() else: do_the_rest() b = 8 if a > 12 else 4
Wrong hanging indentation before block (add 4 spaces). Open
and word[-3] in self._li
- Read upRead up
- Exclude checks
TODO and word[-3] in self._li ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'ent',
- Read upRead up
- Exclude checks
TODO 'ent', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'ous',
- Read upRead up
- Exclude checks
TODO 'ous', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'ive',
- Read upRead up
- Exclude checks
TODO 'ive', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
and word[-4] == 'l'
- Read upRead up
- Exclude checks
TODO and word[-4] == 'l' ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'ence',
- Read upRead up
- Exclude checks
TODO 'ence', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'ment',
- Read upRead up
- Exclude checks
TODO 'ment', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
and not self._sb_ends_in_short_syllable(word[:-1])
- Read upRead up
- Exclude checks
TODO and not self.sbendsinshort_syllable(word[:-1]) ^ |
Wrong hanging indentation before block (add 4 spaces). Open
and word[-1] in {'Y', 'y'}
- Read upRead up
- Exclude checks
TODO and word[-1] in {'Y', 'y'} ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'ic',
- Read upRead up
- Exclude checks
TODO 'ic', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
r1_start >= 1
- Read upRead up
- Exclude checks
TODO r1_start >= 1 ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'ance',
- Read upRead up
- Exclude checks
TODO 'ance', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'ate',
- Read upRead up
- Exclude checks
TODO 'ate', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'ism',
- Read upRead up
- Exclude checks
TODO 'ism', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
len(word[r2_start:]) >= 3
- Read upRead up
- Exclude checks
TODO len(word[r2_start:]) >= 3 ^ |
Wrong hanging indentation before block (add 4 spaces). Open
len(word[r1_start:]) >= 1
- Read upRead up
- Exclude checks
TODO len(word[r1_start:]) >= 1 ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'al',
- Read upRead up
- Exclude checks
TODO 'al', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'able',
- Read upRead up
- Exclude checks
TODO 'able', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
and len(word[r1_start:]) >= 2
- Read upRead up
- Exclude checks
TODO and len(word[r1_start:]) >= 2 ^ |
Wrong hanging indentation before block (add 4 spaces). Open
len(word) > 2
- Read upRead up
- Exclude checks
TODO len(word) > 2 ^ |
Wrong hanging indentation before block (add 4 spaces). Open
and len(word) >= 4
- Read upRead up
- Exclude checks
TODO and len(word) >= 4 ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'ant',
- Read upRead up
- Exclude checks
TODO 'ant', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'er',
- Read upRead up
- Exclude checks
TODO 'er', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
and word[-4] in tuple('st')
- Read upRead up
- Exclude checks
TODO and word[-4] in tuple('st') ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'iti',
- Read upRead up
- Exclude checks
TODO 'iti', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
r1_start >= 1
- Read upRead up
- Exclude checks
TODO r1_start >= 1 ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'ible',
- Read upRead up
- Exclude checks
TODO 'ible', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'ize',
- Read upRead up
- Exclude checks
TODO 'ize', ^ |
Wrong hanging indentation before block (add 4 spaces). Open
and word[-2] not in self._vowels
- Read upRead up
- Exclude checks
TODO and word[-2] not in self._vowels ^ |
Wrong hanging indentation before block (add 4 spaces). Open
and len(word[r1_start:]) >= 3
- Read upRead up
- Exclude checks
TODO and len(word[r1_start:]) >= 3 ^ |
Wrong hanging indentation before block (add 4 spaces). Open
'ement',
- Read upRead up
- Exclude checks
TODO 'ement', ^ |
Consider using enumerate instead of iterating with range and len Open
for i in range(0, len(word)):
- Read upRead up
- Exclude checks
Emitted when code that iterates with range and len is encountered. Such code can be simplified by using the enumerate builtin.