| Conditions | 11 |
| Total Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like coalib.tests.test_bears.internal_folder.SpaceConsistencyTestBear.run() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | from coalib.bearlib.spacing.SpacingHelper import SpacingHelper |
||
| 9 | def run(self, |
||
| 10 | filename, |
||
| 11 | file, |
||
| 12 | use_spaces: bool, |
||
| 13 | allow_trailing_whitespace: bool=False, |
||
| 14 | tab_width: int=SpacingHelper.DEFAULT_TAB_WIDTH, |
||
| 15 | enforce_newline_at_EOF: bool=True): |
||
| 16 | ''' |
||
| 17 | Checks the space consistency for each line. |
||
| 18 | |||
| 19 | :param use_spaces: True if spaces are to be used instead |
||
| 20 | of tabs. |
||
| 21 | :param allow_trailing_whitespace: Whether to allow trailing whitespace |
||
| 22 | or not. |
||
| 23 | :param tab_width: Number of spaces representing one |
||
| 24 | tab. |
||
| 25 | :param enforce_newline_at_EOF: Whether to enforce a newline at the |
||
| 26 | End Of File. |
||
| 27 | ''' |
||
| 28 | spacing_helper = SpacingHelper(tab_width) |
||
| 29 | result_texts = [] |
||
| 30 | |||
| 31 | for line_number, line in enumerate(file, start=1): |
||
| 32 | replacement = line |
||
| 33 | |||
| 34 | if enforce_newline_at_EOF: |
||
| 35 | # Since every line contains at the end at least one \n, only |
||
| 36 | # the last line could potentially not have one. So we don't |
||
| 37 | # need to check whether the current line_number is the last |
||
| 38 | # one. |
||
| 39 | if replacement[-1] != "\n": |
||
| 40 | replacement += "\n" |
||
| 41 | result_texts.append("No newline at EOF.") |
||
| 42 | |||
| 43 | if not allow_trailing_whitespace: |
||
| 44 | replacement = replacement.rstrip(" \t\n") + "\n" |
||
| 45 | if replacement != line.rstrip("\n") + "\n": |
||
| 46 | result_texts.append("Trailing whitespaces.") |
||
| 47 | |||
| 48 | if use_spaces: |
||
| 49 | pre_replacement = replacement |
||
| 50 | replacement = spacing_helper.replace_tabs_with_spaces( |
||
| 51 | replacement) |
||
| 52 | if replacement != pre_replacement: |
||
| 53 | result_texts.append("Tabs used instead of spaces.") |
||
| 54 | else: |
||
| 55 | pre_replacement = replacement |
||
| 56 | replacement = spacing_helper.replace_spaces_with_tabs( |
||
| 57 | replacement) |
||
| 58 | if replacement != pre_replacement: |
||
| 59 | result_texts.append("Spaces used instead of tabs.") |
||
| 60 | |||
| 61 | if len(result_texts) > 0: |
||
| 62 | diff = Diff(file) |
||
| 63 | diff.change_line(line_number, line, replacement) |
||
| 64 | inconsistencies = "".join("\n- " + string |
||
| 65 | for string in result_texts) |
||
| 66 | yield Result.from_values( |
||
| 67 | self, |
||
| 68 | "Line contains following spacing inconsistencies:" |
||
| 69 | + inconsistencies, |
||
| 70 | diffs={filename: diff}, |
||
| 71 | file=filename, |
||
| 72 | line=line_number) |
||
| 73 | result_texts = [] |
||
| 74 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.