Unnecessary "else" after "continue", remove the "else" and de-indent the code inside it
154 resolved_metrics = []
155 for rule in self.agg_rules_manager.rules:
156 aggregate_metric = rule.get_aggregate_metric(key)
157 if aggregate_metric is None:158 continue
159 else:
160 resolved_metrics.append(aggregate_metric)
Unnecessary "else" after "continue", remove the "else" and de-indent the code inside it
105 if self.diverse_replicas:
106 used_servers = set()
107 for (server, instance) in self.ring.get_nodes(key):
108 if server in used_servers:109 continue
110 else:
111 used_servers.add(server)
Description
The use of else
or elif
becomes redundant and can be dropped if the last statement under the leading if
/ elif
block is a continue
statement.
In the case of an elif
after continue
, it can be written as a separate if
block.
For else
blocks after continue
, the statements can be shifted out of else
. Please refer to the examples below for reference.
Refactoring the code this way can improve code-readability and make it easier to maintain.
Bad practice
def classify_number(x):
for num in range(x):
if x % 2 == 0:
continue
else:
print(f"{num} is Odd}")
Preferred:
def classify_number(x):
for num in range(x):
if x % 2 == 0:
continue
print(f"{num} is Odd}")