Skip to content

Commit c176b7f

Browse files
authored
Merge pull request #111 from GatorQue/master
add name optional arguments maintainability anti-pattern. Fixes issue #110
2 parents af685a2 + c616fd7 commit c176b7f

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
Not using variable name for optional arguments
2+
==============================================
3+
4+
Non-optional arguments must be added before optional arguments and will disrupt existing calls to the method if the programmer failed to name all optional arguments used. Also there is no guarentee that the order of the optional arguments might not change in the future. Always naming optional arguments used also improves the readability and maintainability of the code.
5+
6+
7+
Anti-pattern
8+
------------
9+
10+
The code below does not name the optional arguments used when calling the foo method. This results in unexpected behavior when the bar argument is added to the foo method by another developer but who fails to account for all callers of the method.
11+
12+
.. code:: python
13+
from __future__ import print_function
14+
15+
def foo(message, bar, count=0):
16+
print("=" * bar)
17+
for i in range(count):
18+
print(message)
19+
20+
foo("message", 10)
21+
22+
23+
Even if bar is added as an optional argument it might be added before count and results in unexpected behavior as shown in the code below:
24+
25+
.. code:: python
26+
from __future__ import print_function
27+
28+
def foo(message, bar=None, count=0):
29+
if bar:
30+
print("=" * bar)
31+
for i in range(count):
32+
print(message)
33+
34+
foo("message", 10)
35+
36+
37+
Best practice(s)
38+
----------------
39+
40+
Always name optional arguments used when calling a method
41+
.........................................................
42+
43+
The modified code below is the safest way to call the foo method. By always naming the optional arguments used you make it easier to detect callers that need to be fixed when adding non-optional arguments to the method in the future.
44+
45+
.. code:: python
46+
from __future__ import print_function
47+
48+
def foo(message, bar, count=0):
49+
print("=" * bar)
50+
for i in range(count):
51+
print(message)
52+
53+
foo("message", count=10)
54+
55+
56+
The above code will raise an error alerting the programmer to add the missing required bar argument. Whereas the addition of optional argument bar in the code below shows that no changes would be needed since the optional argument was named.
57+
58+
.. code:: python
59+
from __future__ import print_function
60+
61+
def foo(message, bar=None, count=0):
62+
if bar:
63+
print("=" * bar)
64+
for i in range(count):
65+
print(message)
66+
67+
foo("message", count=10)
68+
foo("message", bar=80, count=10)
69+
70+
71+
Always add new optional arguments to the end of a method
72+
........................................................
73+
74+
Adding new optional arguments to the end of a method also ensures that the callers behave as expected as shown in the code below:
75+
76+
.. code:: python
77+
from __future__ import print_function
78+
79+
def foo(message, count=0, bar=None):
80+
if bar:
81+
print("=" * bar)
82+
for i in range(count):
83+
print(message)
84+
85+
foo("message", 10)
86+
foo("message", 10, 80)
87+
88+
89+
The code above demonstrates how always adding optional arguments to the end prevents unexpected behavior but also demonstrates how unreadable the methods becomes as more optional arguments are added as compared to the code below:
90+
91+
.. code:: python
92+
from __future__ import print_function
93+
94+
def foo(message, count=0, bar=None):
95+
if bar:
96+
print("=" * bar)
97+
for i in range(count):
98+
print(message)
99+
100+
foo("message", count=10)
101+
foo("message", count=10, bar=80)
102+
103+

0 commit comments

Comments
 (0)