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 |
||
135 | View Code Duplication | def checkRoleForInt(name, server): |
|
136 | theList = name.split() |
||
137 | # We see if we have multiple parts split by a space |
||
138 | if len(theList)<2: |
||
139 | # Only one part - no int included (or not separated by space) |
||
140 | # Check if role exists - and if not throw an error, if so, throw a diff error |
||
141 | amember = roleForName(name, server) |
||
142 | if amember: |
||
143 | # We at least have a member |
||
144 | return { "Role" : amember, "Int" : None } |
||
145 | else: |
||
146 | # Now we check if we got an ID instead |
||
147 | # Get just the numbers |
||
148 | memID = ''.join(list(filter(str.isdigit, name))) |
||
149 | newMem = roleForID(memID, server) |
||
150 | if newMem: |
||
151 | # We FOUND it! |
||
152 | return { "Role" : newMem, "Int" : None } |
||
153 | else: |
||
154 | # Nothing was right about this... |
||
155 | return { "Role" : None, "Int" : None } |
||
156 | try: |
||
157 | # Let's cast the last item as an int and catch any exceptions |
||
158 | theInt = int(theList[len(theList)-1]) |
||
159 | newMemberName = " ".join(theList[:-1]) |
||
160 | amember = roleForName(newMemberName, server) |
||
161 | if amember: |
||
162 | return { "Role" : amember, "Int" : theInt } |
||
163 | else: |
||
164 | # Now we check if we got an ID instead |
||
165 | # Get just the numbers |
||
166 | memID = ''.join(list(filter(str.isdigit, newMemberName))) |
||
167 | newMem = roleForID(memID, server) |
||
168 | if newMem: |
||
169 | # We FOUND it! |
||
170 | return { "Role" : newMem, "Int" : theInt } |
||
171 | else: |
||
172 | # Nothing was right about this... |
||
173 | return { "Role" : None, "Int" : None } |
||
174 | except ValueError: |
||
175 | # Last section wasn't an int |
||
176 | amember = roleForName(name, server) |
||
177 | if amember: |
||
178 | # Name was just a role - return |
||
179 | return { "Role" : amember, "Int" : None } |
||
180 | else: |
||
181 | # Now we check if we got an ID instead |
||
182 | # Get just the numbers |
||
183 | memID = ''.join(list(filter(str.isdigit, name))) |
||
184 | newMem = roleForID(memID, server) |
||
185 | if newMem: |
||
186 | # We FOUND it! |
||
187 | return { "Role" : newMem, "Int" : None } |
||
188 | else: |
||
189 | # Nothing was right about this... |
||
190 | return { "Role" : None, "Int" : None } |
||
191 | # Should never get here |
||
192 | return None |
||
193 |