Conditions | 26 |
Total Lines | 108 |
Code Lines | 98 |
Lines | 0 |
Ratio | 0 % |
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:
Complex classes like osci.dialog.SystemOverviewDlg.SystemOverviewDlg.show() 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 | # |
||
57 | def show(self): |
||
58 | items = [] |
||
59 | player = client.getPlayer() |
||
60 | # |
||
61 | for systemID in client.db.keys(): |
||
62 | if systemID == player.oid: |
||
63 | continue |
||
64 | system = client.get(systemID, noUpdate=1) |
||
65 | |||
66 | if not hasattr(system, 'planets'): |
||
67 | continue |
||
68 | planetsMine = 0 |
||
69 | planetsOwned = 0 |
||
70 | planetsUnowned = 0 |
||
71 | planetsGA = 0 |
||
72 | planetsNotMine = 0 |
||
73 | en = 0 |
||
74 | bio = 0 |
||
75 | enres = 0 |
||
76 | biores = 0 |
||
77 | stratRes = Const.SR_NONE |
||
78 | refuelMax = 0 |
||
79 | refuelInc = 0 |
||
80 | upgradeShip = 0 |
||
81 | repairShip = 0 |
||
82 | speedBoost = 0 |
||
83 | useOwner = Const.OID_NONE |
||
84 | for planetID in system.planets: |
||
85 | planet = client.get(planetID, noUpdate=1) |
||
86 | owner = getattr(planet, 'owner', Const.OID_NONE) |
||
87 | if owner != Const.OID_NONE: |
||
88 | useOwner = owner |
||
89 | if owner == player.oid: |
||
90 | planetsMine += 1 |
||
91 | else: |
||
92 | planetsOwned += 1 |
||
93 | if self.showOtherPlayers: |
||
94 | planetsNotMine += 1 |
||
95 | en += getattr(planet, 'changeEn', 0) |
||
96 | bio += getattr(planet, 'changeBio', 0) |
||
97 | enres += getattr(planet, 'storEn', 0) |
||
98 | biores += getattr(planet, 'storBio', 0) |
||
99 | stratRes = getattr(planet, 'plStratRes', Const.SR_NONE) if stratRes == Const.SR_NONE else stratRes |
||
100 | refuelMax = max(getattr(planet, 'refuelMax', 0), refuelMax) |
||
101 | refuelInc = max(getattr(planet, 'refuelInc', 0), refuelInc) |
||
102 | upgradeShip += getattr(planet, 'upgradeShip', 0) |
||
103 | repairShip = max(getattr(planet, 'repairShip', 0), repairShip) |
||
104 | speedBoost = max(getattr(planet, 'fleetSpeedBoost', 0), speedBoost) |
||
105 | else: |
||
106 | if hasattr(planet, "plType") and planet.plType in ("A", "G"): |
||
107 | planetsGA += 1 |
||
108 | if self.showUncolonizable: |
||
109 | planetsNotMine += 1 |
||
110 | else: |
||
111 | planetsUnowned += 1 |
||
112 | if self.showColonizable: |
||
113 | planetsNotMine += 1 |
||
114 | if planetsMine == 0: # fix no-data systems |
||
115 | en = '?' |
||
116 | bio = '?' |
||
117 | enres = '?' |
||
118 | biores = '?' |
||
119 | if ((planetsMine and self.showMine) |
||
120 | or (planetsOwned and self.showOtherPlayers) |
||
121 | or (planetsUnowned and self.showColonizable) |
||
122 | or (planetsGA and self.showUncolonizable)): |
||
123 | if stratRes == Const.SR_NONE: |
||
124 | stratResText = ' ' |
||
125 | else: |
||
126 | stratResText = gdata.stratRes[stratRes] |
||
127 | problem = (bio < 0 or en < 0) |
||
128 | if planetsMine > 0: # make sure you own it |
||
129 | useOwner = player.oid |
||
130 | if speedBoost > 1: |
||
131 | speedBoost = int((speedBoost - 1) * 100) |
||
132 | else: |
||
133 | speedBoost = '' |
||
134 | if self.showProblems: |
||
135 | color = res.getSystemOverviewProblemColor(useOwner, problem) |
||
136 | else: |
||
137 | color = res.getPlayerColor(useOwner) |
||
138 | item = ui.Item( |
||
139 | getattr(system, 'name', res.getUnknownName()), |
||
140 | tSyPnum=planetsMine + planetsOwned + planetsUnowned + planetsGA, |
||
141 | tSyPTnum=planetsNotMine, |
||
142 | tSyPYnum=planetsMine, |
||
143 | tSyBioRes=biores, |
||
144 | tSyEnRes=enres, |
||
145 | tSyBio=bio, |
||
146 | tSyEn=en, |
||
147 | tSyRefuel=refuelInc, |
||
148 | tSyRefuelMax=refuelMax, |
||
149 | tSyRepair=(repairShip * 100), |
||
150 | tSyUpgrade=int(upgradeShip), |
||
151 | tSyGate=speedBoost, |
||
152 | tStRes=_(stratResText), |
||
153 | tSysID=systemID, |
||
154 | foreground=color, |
||
155 | ) |
||
156 | items.append(item) |
||
157 | self.win.vPlanets.items = items |
||
158 | self.win.vPlanets.itemsChanged() |
||
159 | # buttons |
||
160 | self.win.vMine.pressed = self.showMine |
||
161 | self.win.vOtherPlayers = self.showOtherPlayers |
||
162 | self.win.vColonizable = self.showColonizable |
||
163 | self.win.vUncolonizable = self.showUncolonizable |
||
164 | self.win.vProblems = self.showProblems |
||
165 | |||
245 |