| Conditions | 17 |
| Total Lines | 82 |
| Lines | 0 |
| Ratio | 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:
Complex classes like ed2d.assets.OBJObject.__init__() 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 | from ed2d.assets.mtlloader import MTL |
||
| 29 | |||
| 30 | for line in self.lines: |
||
| 31 | lastType = valueType |
||
| 32 | valueType, value = line.strip().split(' ', 1) |
||
| 33 | |||
| 34 | # Don't bother unless the following key words exist in the line |
||
| 35 | if valueType not in ['o', 'g', 'f', 'v', 'vt', 'vn', 'usemtl', 'mtllib']: |
||
| 36 | continue |
||
| 37 | |||
| 38 | value = value.split(' ') |
||
| 39 | |||
| 40 | # Check first and continue on early because of string splitting |
||
| 41 | if valueType == "usemtl": |
||
| 42 | matname = value[0] |
||
| 43 | continue |
||
| 44 | |||
| 45 | if valueType in ['g', 'o']: |
||
| 46 | # These objects reset state basically |
||
| 47 | matname = None |
||
| 48 | continue |
||
| 49 | |||
| 50 | if valueType == 'mtllib': |
||
| 51 | mtlpath = files.resolve_path('data', 'models', value[0]) |
||
| 52 | |||
| 53 | # Load the mtl file |
||
| 54 | self.mtlfile = MTL(mtlpath) |
||
| 55 | for material in self.mtlfile.data.keys(): |
||
| 56 | self.fmvnig[material] = [ [], [], [] ] |
||
| 57 | |||
| 58 | continue |
||
| 59 | |||
| 60 | if valueType == "f": |
||
| 61 | face = [item.split("/") for item in value] |
||
| 62 | for typeGroup in face: |
||
| 63 | for typeIndex in range(len(typeGroup)): |
||
| 64 | if typeIndex == 0: # Vertex |
||
| 65 | typeSource = self.tempVertices |
||
| 66 | elif typeIndex == 1: # UV |
||
| 67 | typeSource = self.tempUVs |
||
| 68 | elif typeIndex == 2: # Normal |
||
| 69 | typeSource = self.tempNormals |
||
| 70 | |||
| 71 | index = typeGroup[typeIndex] |
||
| 72 | |||
| 73 | # Make sure data exists |
||
| 74 | if index != '': |
||
| 75 | index = int(index) |
||
| 76 | typeData = typeSource[index - 1] |
||
| 77 | |||
| 78 | self.fmvnig[matname][typeIndex].append(typeData) |
||
| 79 | continue |
||
| 80 | |||
| 81 | # Map the values after the keyword to floats |
||
| 82 | value = list(map(float, value)) |
||
| 83 | |||
| 84 | if valueType == "v": |
||
| 85 | self.tempVertices.append(value) |
||
| 86 | |||
| 87 | elif valueType == "vt": |
||
| 88 | self.tempUVs.append(value * 2) |
||
| 89 | |||
| 90 | elif valueType == "vn": |
||
| 91 | self.tempNormals.append(value) |
||
| 92 |