Conditions | 11 |
Total Lines | 64 |
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.output.dbus.DbusDocument.Analyze() 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 | import os |
||
84 | @dbus.service.method(interface, |
||
85 | in_signature="", |
||
86 | out_signature="(iaa{ss}a(sbaa{ss}))") |
||
87 | def Analyze(self): |
||
88 | """ |
||
89 | This method analyzes the document and sends back the result |
||
90 | |||
91 | :return: The output is structure which has 3 items: |
||
92 | - The exitcode from the analysis. |
||
93 | - List of logs from the analysis. |
||
94 | - List of information about each section that contains: |
||
95 | - The name of the section. |
||
96 | - Boolean which is true if all bears in the section |
||
97 | executed successfully. |
||
98 | - List of results where each result is a string |
||
99 | dictionary which contains: |
||
100 | id, origin, message, file, line_nr, severity |
||
101 | """ |
||
102 | retval = [] |
||
103 | if self.path == "" or self.config_file == "": |
||
104 | return retval |
||
105 | |||
106 | args = ["--config=" + self.config_file] |
||
107 | |||
108 | log_printer = ListLogPrinter() |
||
109 | exitcode = 0 |
||
110 | try: |
||
111 | yielded_results = False |
||
112 | (sections, |
||
113 | local_bears, |
||
114 | global_bears, |
||
115 | targets) = gather_configuration(fail_acquire_settings, |
||
116 | log_printer, |
||
117 | arg_list=args) |
||
118 | |||
119 | for section_name in sections: |
||
120 | section = sections[section_name] |
||
121 | |||
122 | if not section.is_enabled(targets): |
||
123 | continue |
||
124 | |||
125 | if any([fnmatch(self.path, file_pattern) |
||
126 | for file_pattern in path_list(section["files"])]): |
||
127 | |||
128 | section["files"].value = self.path |
||
129 | section_result = execute_section( |
||
130 | section=section, |
||
131 | global_bear_list=global_bears[section_name], |
||
132 | local_bear_list=local_bears[section_name], |
||
133 | print_results=lambda *args: True, |
||
134 | log_printer=log_printer) |
||
135 | yielded_results = yielded_results or section_result[0] |
||
136 | |||
137 | retval.append( |
||
138 | DbusDocument.results_to_dbus_struct(section_result, |
||
139 | section_name)) |
||
140 | |||
141 | if yielded_results: |
||
142 | exitcode = 1 |
||
143 | except BaseException as exception: # pylint: disable=broad-except |
||
144 | exitcode = exitcode or get_exitcode(exception, log_printer) |
||
145 | |||
146 | logs = [log.to_string_dict() for log in log_printer.logs] |
||
147 | return (exitcode, logs, retval) |
||
148 | |||
186 |