boolean_algebra.boolean()   D
last analyzed

Complexity

Conditions 13

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 21
nop 3
dl 0
loc 26
rs 4.2
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like boolean_algebra.boolean() 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
OPERATION_NAMES = (
2
    "conjunction",
3
    "disjunction",
4
    "implication",
5
    "exclusive",
6
    "equivalence",
7
)
8
9
10
def boolean(x, y, operation):
11
    if operation == 'conjunction':
12
        if x and y:
13
            return 1
14
        else:
15
            return 0
16
    elif operation == 'disjunction':
17
        if x or y:
18
            return 1
19
        else:
20
            return 0
21
    elif operation == 'implication':
22
        if x:
23
            return y
24
        else:
25
            return 1
26
    elif operation == 'exclusive':
27
        if x == y:
28
            return 0
29
        else:
30
            return 1
31
    elif operation == 'equivalence':
32
        if x == y:
33
            return 1
34
        else:
35
            return 0
36
37
38
if __name__ == '__main__':  # pragma: no cover
39
    # These "asserts" using only for self-checking and not necessary for
40
    # auto-testing
41
    assert boolean(1, 0, "conjunction") == 0, "and"
42
    assert boolean(1, 0, "disjunction") == 1, "or"
43
    assert boolean(1, 1, "implication") == 1, "material"
44
    assert boolean(0, 1, "exclusive") == 1, "xor"
45
    assert boolean(0, 1, "equivalence") == 0, "same?"
46