| Conditions | 11 |
| Total Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 SpacingHelper.replace_spaces_with_tabs() 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.abstractions.SectionCreatable import SectionCreatable |
||
| 77 | @enforce_signature |
||
| 78 | def replace_spaces_with_tabs(self, line: str): |
||
| 79 | """ |
||
| 80 | Replaces spaces with tabs where possible. However in no case only one |
||
| 81 | space will be replaced by a tab. |
||
| 82 | |||
| 83 | Example: " \t a_text another" will be converted to |
||
| 84 | "\t a_text\tanother", assuming the tab_width is set to 4. |
||
| 85 | |||
| 86 | :param line: The string with spaces to replace. |
||
| 87 | :return: The converted string. |
||
| 88 | """ |
||
| 89 | def previous_whitespace(): |
||
| 90 | # Find the previous real character, and remaining chars to fill tab |
||
| 91 | non_whitespace_position = tabless_position - currspaces |
||
| 92 | tab_fill = non_whitespace_position % self.tab_width |
||
| 93 | |||
| 94 | if tab_fill and tab_fill + currspaces >= self.tab_width: |
||
| 95 | whitespace = "\t" |
||
| 96 | remaining_spaces = currspaces - (self.tab_width - tab_fill) |
||
| 97 | else: |
||
| 98 | whitespace = "" |
||
| 99 | remaining_spaces = currspaces |
||
| 100 | |||
| 101 | whitespace += "\t" * (remaining_spaces // self.tab_width) |
||
| 102 | whitespace += " " * (remaining_spaces % self.tab_width) |
||
| 103 | return whitespace |
||
| 104 | |||
| 105 | currspaces = 0 |
||
| 106 | result = "" |
||
| 107 | # Tracking the index of the string isnt enough because tabs are |
||
| 108 | # spanning over multiple columns |
||
| 109 | tabless_position = 0 |
||
| 110 | previous_char = None |
||
| 111 | for char in line: |
||
| 112 | if char == " ": |
||
| 113 | currspaces += 1 |
||
| 114 | tabless_position += 1 |
||
| 115 | elif char == "\t": |
||
| 116 | space_count = (self.tab_width - tabless_position |
||
| 117 | % self.tab_width) |
||
| 118 | currspaces += space_count |
||
| 119 | tabless_position += space_count |
||
| 120 | else: |
||
| 121 | if currspaces: |
||
| 122 | if currspaces == 1: |
||
| 123 | result += previous_char |
||
| 124 | else: |
||
| 125 | result += previous_whitespace() |
||
| 126 | |||
| 127 | result += char |
||
| 128 | currspaces = 0 |
||
| 129 | tabless_position += 1 |
||
| 130 | |||
| 131 | previous_char = char |
||
| 132 | |||
| 133 | if currspaces: |
||
| 134 | if currspaces == 1: |
||
| 135 | result += previous_char |
||
| 136 | else: |
||
| 137 | char = None |
||
| 138 | result += previous_whitespace() |
||
| 139 | |||
| 140 | return result |
||
| 141 |