-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnautilus_exercise_ly_nguyen.py
118 lines (94 loc) · 1.93 KB
/
nautilus_exercise_ly_nguyen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Improve the following code for:
# ● Style
# ● Simplicity
# ● Maintainability
# ==== Refactored codes - Option 1
'''
Cerate smaller helper functions and eliminate unnecessary return False statements
This is the closet to the original code with clear if/else statements for each condition A, B, C, D
'''
def f():
""" Main function """
if A:
z()
b_helper()
else:
s()
return False
def d_helper():
""" Helper function if condition D is met """
if D:
w()
return True
else:
v()
def c_helper():
""" Helper function if condition C is met """
if C:
x()
d_helper()
else:
u()
def b_helper():
""" Helper function if condition B is met """
if B:
y()
c_helper()
else:
t()
# ==== Refactored codes - Option 2
'''
Create smaller helper functions and eliminate else statements.
In each function, we quickly return False if a given condition is not met.
'''
def f():
""" Main function """
if not A:
s()
return False
z()
b_helper()
def d_helper():
""" Helper function for condition D """
if not D:
v()
return False
w()
return True
def c_helper():
""" Helper function for condition C """
if not C:
u()
return False
x()
d_helper()
def b_helper():
""" Helper function for condition B """
if not B:
t()
return False
y()
c_helper()
# ===== Original codes
def f():
if A:
z()
if B:
y()
if C:
x()
if D:
w()
return True
else:
v()
return False
else:
u()
return False
else:
t()
return False
else:
s()
return False