Conditions | 14 |
Total Lines | 61 |
Code Lines | 47 |
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 fused_iutest_files.IutestFused.Translate() 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 | #!/usr/bin/env python |
||
170 | def Translate(self, root, filename, output, output_dir, minimum): |
||
171 | output_file = codecs.open(os.path.join(output_dir, output), 'w', 'utf-8-sig') |
||
172 | processed_files = set() |
||
173 | # fused-min not support gtest switch |
||
174 | if minimum: |
||
175 | processed_files.add(os.path.normpath(os.path.join(root, "gtest/iutest_switch.hpp"))) |
||
176 | |||
177 | def ProcessFile(curr, filename, fileset, minimum): |
||
178 | path = os.path.join(root, filename) |
||
179 | if not os.path.exists(path): |
||
180 | path = os.path.join(curr, filename) |
||
181 | |||
182 | path = os.path.normpath(path) |
||
183 | if path in fileset: |
||
184 | return |
||
185 | |||
186 | find_ifdef = False |
||
187 | fileset.add(path) |
||
188 | # print(filename) |
||
189 | for line in codecs.open(path, 'r', 'utf-8-sig'): |
||
190 | line = re.sub('/\*.*?\*/', '', line) |
||
191 | m = self.INCLUDE_REGEX.match(line) |
||
192 | if m: |
||
193 | self.Flush(output_file) |
||
194 | include_file = m.group(1) |
||
195 | if find_ifdef: |
||
196 | s = set(fileset) |
||
197 | ProcessFile(os.path.dirname(path), include_file, s, minimum) |
||
198 | else: |
||
199 | ProcessFile(os.path.dirname(path), include_file, fileset, minimum) |
||
200 | else: |
||
201 | find_ifdef = bool(self.IFDEF_REGEX.match(line)) |
||
202 | if minimum: |
||
203 | line = self.Minimze(line) |
||
204 | if len(line): |
||
205 | if self.PREPRO_REGEX.match(self.store_line): |
||
206 | line = line.strip() |
||
207 | if line.endswith('\\'): |
||
208 | self.StoreMinimze(line.rstrip(r'\\')) |
||
209 | else: |
||
210 | self.StoreMinimze(line) |
||
211 | self.Flush(output_file) |
||
212 | elif self.PREPRO_REGEX.match(line): |
||
213 | self.Flush(output_file) |
||
214 | if line.strip().endswith('\\'): |
||
215 | self.StoreMinimze(line.strip().rstrip(r'\\')) |
||
216 | else: |
||
217 | self.Write(output_file, line) |
||
218 | else: |
||
219 | strip_line = line.strip() |
||
220 | self.StoreMinimze(strip_line) |
||
221 | #if not re.match('.*[{};\(\)<>]$', strip_line): |
||
222 | # self.Flush(output_file) |
||
223 | if re.match('.*(:|IUTEST_CXX_DEFAULT_FUNCTION)$', strip_line): |
||
224 | self.store_line += " " |
||
225 | else: |
||
226 | self.Write(output_file, line) |
||
227 | self.Flush(output_file) |
||
228 | |||
229 | ProcessFile(root, filename, processed_files, minimum) |
||
230 | output_file.close() |
||
231 | |||
266 |