2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2023-06-14 10:47:35 +02:00
|
|
|
Nested conditionals are hard to read and can make the order of operations complex to understand.
|
2020-06-30 12:48:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,python]
|
2020-06-30 12:48:39 +02:00
|
|
|
----
|
2020-12-21 15:38:52 +01:00
|
|
|
class Job:
|
|
|
|
@property
|
|
|
|
def readable_status(self):
|
|
|
|
return "Running" if job.is_running else "Failed" if job.errors else "Succeeded" # Noncompliant
|
2020-06-30 12:48:39 +02:00
|
|
|
----
|
|
|
|
|
2023-06-14 10:47:35 +02:00
|
|
|
Instead, use another line to express the nested operation in a separate statement.
|
2020-06-30 12:48:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,python]
|
2020-06-30 12:48:39 +02:00
|
|
|
----
|
2020-12-21 15:38:52 +01:00
|
|
|
class Job:
|
|
|
|
@property
|
|
|
|
def readable_status(self):
|
|
|
|
if job.is_running:
|
|
|
|
return "Running"
|
|
|
|
return "Failed" if job.errors else "Succeeded"
|
2020-06-30 12:48:39 +02:00
|
|
|
----
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Exceptions
|
2020-06-30 12:48:39 +02:00
|
|
|
|
|
|
|
No issue is raised on conditional expressions in comprehensions.
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2023-06-14 10:47:35 +02:00
|
|
|
[source,python]
|
|
|
|
----
|
|
|
|
job_statuses = ["Running" if job.is_running else "Failed" if job.errors else "Succeeded" for job in jobs] # Compliant by exception
|
|
|
|
----
|
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
Extract this nested conditional expression into an independent statement.
|
|
|
|
|
|
|
|
|
|
|
|
=== Highlighting
|
|
|
|
|
|
|
|
* Primary: highlight the entire nested conditional expression
|
|
|
|
* Secondary: highlight the "if" and "else" of the parent conditional expression
|
|
|
|
message: 'Parent conditional expression.'
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../comments-and-links.adoc[]
|
2023-06-22 10:38:01 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|