Conditions | 12 |
Total Lines | 56 |
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.OBJ.__process_in_house() 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 | |||
111 | def __process_in_house(self, filename): |
||
112 | matname = None |
||
113 | with open(filename, "r") as objfl: |
||
114 | |||
115 | for line in objfl: |
||
116 | |||
117 | value = line.split() |
||
118 | valueType = value[0] |
||
119 | |||
120 | # Don't bother unless the following key words exist in the line |
||
121 | if valueType not in ['f', 'v', 'vt', 'vn', 'usemtl']: |
||
122 | continue |
||
123 | |||
124 | # Start ignoring the first word of the line to grab the values |
||
125 | value = value[1:] |
||
126 | |||
127 | # Check first and continue on early because of string splitting |
||
128 | if valueType == "usemtl": |
||
129 | matname = value[0] |
||
130 | # Material Vertex UV Normal Indices Group (Vertex, UV, Normal) |
||
131 | self.matnumber += 1 |
||
132 | self.fnumber = 0 |
||
133 | continue |
||
134 | |||
135 | if valueType == "f": |
||
136 | temp = [item.split("/") for item in value] |
||
137 | |||
138 | for i in range(3): |
||
139 | # 0 - Vertex |
||
140 | # 1 - UV |
||
141 | # 2 - Normal |
||
142 | # Make sure UV index data exists |
||
143 | if temp[i][1] != '': |
||
144 | self.tmvnig[matname][1][self.fnumber] = int(temp[i][1]) |
||
145 | self.tmvnig[matname][0][self.fnumber] = int(temp[i][0]) |
||
146 | self.tmvnig[matname][2][self.fnumber] = int(temp[i][2]) |
||
147 | self.fnumber += 1 |
||
148 | continue |
||
149 | |||
150 | # Map the values after the keyword to floats |
||
151 | value = list(map(float, value)) |
||
152 | |||
153 | if valueType == "v": |
||
154 | v = [value[0], value[1], value[2]] |
||
155 | self.tempVertices[self.vnumber] = v |
||
156 | self.vnumber += 1 |
||
157 | |||
158 | elif valueType == "vt": |
||
159 | vt = [value[0], value[0]] |
||
160 | self.tempUVs[self.vtnumber] = vt |
||
161 | self.vtnumber += 1 |
||
162 | |||
163 | elif valueType == "vn": |
||
164 | n = [value[0], value[1], value[2]] |
||
165 | self.tempNormals[self.vnnumber] = n |
||
166 | self.vnnumber += 1 |
||
167 | |||
189 |