Conditions | 9 |
Total Lines | 58 |
Lines | 58 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # Copyright (c) 2017 CorpNewt |
||
76 | View Code Duplication | def checkNameForInt(name, server): |
|
|
|||
77 | theList = name.split() |
||
78 | # We see if we have multiple parts split by a space |
||
79 | if len(theList)<2: |
||
80 | # Only one part - no int included (or not separated by space) |
||
81 | # Check if member exists - and if not throw an error, if so, throw a diff error |
||
82 | amember = memberForName(name, server) |
||
83 | if amember: |
||
84 | # We at least have a member |
||
85 | return { "Member" : amember, "Int" : None } |
||
86 | else: |
||
87 | # Now we check if we got an ID instead |
||
88 | # Get just the numbers |
||
89 | memID = ''.join(list(filter(str.isdigit, name))) |
||
90 | newMem = memberForID(memID, server) |
||
91 | if newMem: |
||
92 | # We FOUND it! |
||
93 | return { "Member" : newMem, "Int" : None } |
||
94 | else: |
||
95 | # Nothing was right about this... |
||
96 | return { "Member" : None, "Int" : None } |
||
97 | try: |
||
98 | # Let's cast the last item as an int and catch any exceptions |
||
99 | theInt = int(theList[len(theList)-1]) |
||
100 | newMemberName = " ".join(theList[:-1]) |
||
101 | amember = memberForName(newMemberName, server) |
||
102 | if amember: |
||
103 | return { "Member" : amember, "Int" : theInt } |
||
104 | else: |
||
105 | # Now we check if we got an ID instead |
||
106 | # Get just the numbers |
||
107 | memID = ''.join(list(filter(str.isdigit, newMemberName))) |
||
108 | newMem = memberForID(memID, server) |
||
109 | if newMem: |
||
110 | # We FOUND it! |
||
111 | return { "Member" : newMem, "Int" : theInt } |
||
112 | else: |
||
113 | # Nothing was right about this... |
||
114 | return { "Member" : None, "Int" : None } |
||
115 | except ValueError: |
||
116 | # Last section wasn't an int |
||
117 | amember = memberForName(name, server) |
||
118 | if amember: |
||
119 | # Name was just a member - return |
||
120 | return { "Member" : amember, "Int" : None } |
||
121 | else: |
||
122 | # Now we check if we got an ID instead |
||
123 | # Get just the numbers |
||
124 | memID = ''.join(list(filter(str.isdigit, name))) |
||
125 | newMem = memberForID(memID, server) |
||
126 | if newMem: |
||
127 | # We FOUND it! |
||
128 | return { "Member" : newMem, "Int" : None } |
||
129 | else: |
||
130 | # Nothing was right about this... |
||
131 | return { "Member" : None, "Int" : None } |
||
132 | # Should never get here |
||
133 | return None |
||
134 | |||
193 |