Conditions | 15 |
Total Lines | 57 |
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.MTL.__load() 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 import material |
||
9 | def __load(self, filename): |
||
10 | materialName = None |
||
11 | value = None |
||
12 | valueType = None |
||
13 | |||
14 | for line in open(filename, "r"): |
||
15 | # Avoid lines starting with "#" |
||
16 | if line.startswith('#'): |
||
17 | continue |
||
18 | else: |
||
19 | value = line.split() |
||
20 | # Make sure the line is no empty |
||
21 | if not value: |
||
22 | continue |
||
23 | else: |
||
24 | valueType = value[0] |
||
25 | |||
26 | # Check for material |
||
27 | if valueType == 'newmtl': |
||
28 | self.material = self.data[value[1]] = material.Material() |
||
29 | materialName = value[1] |
||
30 | continue |
||
31 | elif self.material is None: |
||
32 | print("Error, material member is None.") |
||
33 | break |
||
34 | |||
35 | # Generate a list of floats using the numbers |
||
36 | value = list(map(float, value[1:])) |
||
37 | |||
38 | if valueType == 'Ns': |
||
39 | # Material Specular Exponent which is multipled by texture value |
||
40 | self.data[materialName].ispecular = value[0] |
||
41 | elif valueType == 'Ka': |
||
42 | # Ambient color |
||
43 | self.data[materialName].ambient = [value[0], value[1], value[2]] |
||
44 | elif valueType == 'Kd': |
||
45 | # Diffuse color |
||
46 | self.data[materialName].diffuse = [value[0], value[1], value[2]] |
||
47 | elif valueType == 'Ks': |
||
48 | # Specular color |
||
49 | self.data[materialName].specular = [value[0], value[1], value[2]] |
||
50 | elif valueType == 'Ke': |
||
51 | # Emission color |
||
52 | self.data[materialName].emission = [value[0], value[1], value[2]] |
||
53 | elif valueType == 'Ni': |
||
54 | # Optical Denisty or Index of Refraction |
||
55 | self.data[materialName].IOR = value[0] |
||
56 | elif valueType == 'map_Kd': |
||
57 | # Diffuse texture is multiplied by the Kd |
||
58 | self.data[materialName].albedoLayers['test'] = value[0] |
||
59 | # Do texture stuff here |
||
60 | elif valueType == 'map_Ks': |
||
61 | # Specular texture is multiplied by the Ks |
||
62 | self.data[materialName].specularMapLayers['test'] = value[0] |
||
63 | elif valueType == 'map_Ns': |
||
64 | # Texture linked to the specular exponent and is multiplied by Ns |
||
65 | pass |
||
66 | |||
77 |