Conditions | 11 |
Total Lines | 67 |
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 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 | |||
96 | - The name of the section. |
||
97 | - Boolean which is true if all bears in the section |
||
98 | executed successfully. |
||
99 | - List of results where each result is a string |
||
100 | dictionary which contains: |
||
101 | id, origin, message, file, line_nr, severity |
||
102 | """ |
||
103 | retval = [] |
||
104 | if self.path == "" or self.config_file == "": |
||
105 | return retval |
||
106 | |||
107 | args = ["--config=" + self.config_file] |
||
108 | |||
109 | log_printer = ListLogPrinter() |
||
110 | exitcode = 0 |
||
111 | try: |
||
112 | yielded_results = False |
||
113 | (sections, |
||
114 | local_bears, |
||
115 | global_bears, |
||
116 | targets) = gather_configuration(fail_acquire_settings, |
||
117 | log_printer, |
||
118 | arg_list=args) |
||
119 | |||
120 | for section_name in sections: |
||
121 | section = sections[section_name] |
||
122 | |||
123 | if not section.is_enabled(targets): |
||
124 | continue |
||
125 | |||
126 | if any([fnmatch(self.path, file_pattern) |
||
127 | for file_pattern in glob_list(section["files"])]): |
||
128 | |||
129 | section["files"].value = self.path |
||
130 | # TODO: Integrate with caching |
||
131 | section_result = execute_section( |
||
132 | section=section, |
||
133 | global_bear_list=global_bears[section_name], |
||
134 | local_bear_list=local_bears[section_name], |
||
135 | print_results=lambda *args: True, |
||
136 | cache=None, |
||
137 | log_printer=log_printer) |
||
138 | yielded_results = yielded_results or section_result[0] |
||
139 | |||
140 | retval.append( |
||
141 | DbusDocument.results_to_dbus_struct(section_result, |
||
142 | section_name)) |
||
143 | |||
144 | if yielded_results: |
||
145 | exitcode = 1 |
||
146 | except BaseException as exception: # pylint: disable=broad-except |
||
147 | exitcode = exitcode or get_exitcode(exception, log_printer) |
||
148 | |||
149 | logs = [log.to_string_dict() for log in log_printer.logs] |
||
150 | return (exitcode, logs, retval) |
||
151 | |||
189 |