Conditions | 11 |
Total Lines | 51 |
Code Lines | 35 |
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 glances.client_browser.GlancesClientBrowser.__update_stats() 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 | # |
||
83 | if not client.login(): |
||
84 | self.screen.display_popup( |
||
85 | "Sorry, cannot connect to '{}'\n" "See '{}' for more details".format(server['name'], LOG_FILENAME) |
||
86 | ) |
||
87 | |||
88 | # Set the ONLINE status for the selected server |
||
89 | self.set_in_selected('status', 'OFFLINE') |
||
90 | else: |
||
91 | # Start the client loop |
||
92 | # Return connection type: 'glances' or 'snmp' |
||
93 | connection_type = client.serve_forever() |
||
94 | |||
95 | try: |
||
96 | logger.debug("Disconnect Glances client from the {} server".format(server['key'])) |
||
97 | except IndexError: |
||
98 | # Server did not exist anymore |
||
99 | pass |
||
100 | else: |
||
101 | # Set the ONLINE status for the selected server |
||
102 | if connection_type == 'snmp': |
||
103 | self.set_in_selected('status', 'SNMP') |
||
104 | else: |
||
105 | self.set_in_selected('status', 'ONLINE') |
||
106 | |||
107 | # Return to the browser (no server selected) |
||
108 | self.screen.active_server = None |
||
109 | |||
110 | def __serve_forever(self): |
||
111 | """Main client loop.""" |
||
112 | # No need to update the server list |
||
113 | while not self.screen.is_end: |
||
114 | # Update the stats in the servers list |
||
115 | self.servers_list.update_servers_stats() |
||
116 | |||
117 | if self.screen.active_server is None: |
||
118 | # Display Glances browser (servers list) |
||
119 | self.screen.update(self.servers_list.get_servers_list()) |
||
120 | else: |
||
121 | # Display selected Glances server |
||
122 | self.__display_server(self.servers_list.get_servers_list()[self.screen.active_server]) |
||
123 | |||
124 | def serve_forever(self): |
||
125 | """Wrapper to the serve_forever function. |
||
126 | |||
127 | This function will restore the terminal to a sane state |
||
128 | before re-raising the exception and generating a traceback. |
||
129 | """ |
||
130 | try: |
||
131 | return self.__serve_forever() |
||
132 | finally: |
||
133 | self.end() |
||
134 | |||
142 |