Conditions | 24 |
Total Lines | 100 |
Code Lines | 77 |
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.FleetsOverviewDlg.FleetsOverviewDlg.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 | # |
||
93 | def show(self): |
||
94 | player = client.getPlayer() |
||
95 | items = [] |
||
96 | for fleetID in client.db.keys(): |
||
97 | fleet = client.get(fleetID, noUpdate = 1) |
||
98 | # skip non-fleets |
||
99 | if not hasattr(fleet, "type") or fleet.type != Const.T_FLEET: |
||
100 | continue |
||
101 | fgColor = self._analyzeRelations(player.oid, fleet) |
||
102 | if fgColor is None: |
||
103 | # nothing to show |
||
104 | continue |
||
105 | |||
106 | if hasattr(fleet, 'owner') and player.oid != fleet.owner: |
||
107 | owner = getattr(client.get(fleet.owner, noUpdate = 1), "name", res.getUnknownName()) |
||
108 | ownerName = " (%s)" % owner |
||
109 | ownerNameTip = owner |
||
110 | ownerTipTitle = _("Owner") |
||
111 | else: |
||
112 | ownerName = "" |
||
113 | ownerNameTip = "" |
||
114 | ownerTipTitle = "" |
||
115 | |||
116 | # check position of fleet |
||
117 | system = None |
||
118 | systemName = "-" |
||
119 | if hasattr(fleet, 'orbiting') and fleet.orbiting: |
||
120 | system = client.get(fleet.orbiting, noUpdate = 1) |
||
121 | systemName = getattr(system, "name", res.getUnknownName()) |
||
122 | elif hasattr(fleet, 'closeSystem'): |
||
123 | system = client.get(fleet.closeSystem, noUpdate = 1) |
||
124 | systemName = _("%s (dst)") % getattr(system, "name", res.getUnknownName()) |
||
125 | |||
126 | # get fleet current action and target of action |
||
127 | order = "-" |
||
128 | targetName = "-" |
||
129 | if hasattr(fleet, 'actions') and fleet.actionIndex < len(fleet.actions): |
||
130 | action, target, data = fleet.actions[fleet.actionIndex] |
||
131 | if action == Const.FLACTION_REDIRECT and not redirects: |
||
|
|||
132 | continue |
||
133 | order = gdata.fleetActions[action] |
||
134 | if target != Const.OID_NONE: |
||
135 | targetName = getattr(client.get(target, noUpdate = 1), 'name', res.getUnknownName()) |
||
136 | order = "%s %s" % (order, targetName) |
||
137 | # eta |
||
138 | if hasattr(fleet, "eta"): eta = res.formatTime(fleet.eta) |
||
139 | else: eta = "?" |
||
140 | |||
141 | # fuel |
||
142 | if hasattr(fleet, "storEn"): |
||
143 | if fleet.maxEn > 0: fuel = 100 * fleet.storEn / fleet.maxEn |
||
144 | else: fuel = 0 |
||
145 | else: |
||
146 | fuel = "?" |
||
147 | |||
148 | # operational time |
||
149 | if hasattr(fleet, 'storEn') and hasattr(fleet, 'operEn'): |
||
150 | turns = 100000 |
||
151 | if fleet.operEn > 0: turns = fleet.storEn / fleet.operEn |
||
152 | rawRange = turns * fleet.speed / Rules.turnsPerDay |
||
153 | range = "%.2f" % rawRange |
||
154 | opTime = res.formatTime(turns) |
||
155 | else: |
||
156 | rawRange = 0 |
||
157 | range = "?" |
||
158 | opTime = "?" |
||
159 | |||
160 | # last upgrade |
||
161 | if hasattr(fleet, "lastUpgrade"): |
||
162 | lastUpgrade = res.formatTime(fleet.lastUpgrade) |
||
163 | else: |
||
164 | lastUpgrade = "?" |
||
165 | |||
166 | if hasattr(fleet,'customname') and fleet.customname: |
||
167 | fleetname = fleet.customname |
||
168 | else: |
||
169 | fleetname = getattr(fleet, 'name', res.getUnknownName()) |
||
170 | |||
171 | # create ListBox Item for fleet |
||
172 | item = ui.Item( |
||
173 | "%s %s" % (fleetname, ownerName), |
||
174 | tooltipTitle = ownerTipTitle, |
||
175 | tooltip = ownerNameTip, |
||
176 | tLocation = systemName, |
||
177 | tOrder = order, |
||
178 | tMP = getattr(fleet, "combatPwr", "?"), |
||
179 | tETA = eta, |
||
180 | tETA_raw = getattr(fleet, "eta", 0), |
||
181 | tSignature = getattr(fleet, "signature", "?"), |
||
182 | tFuel = fuel, |
||
183 | tOpTime = opTime, |
||
184 | tRange = range, |
||
185 | tRange_raw = rawRange, |
||
186 | tLastUpgrade = lastUpgrade, |
||
187 | tFleetID = fleetID, |
||
188 | foreground = fgColor) |
||
189 | items.append(item) |
||
190 | |||
191 | self.win.vFleets.items = items |
||
192 | self.win.vFleets.itemsChanged() |
||
193 | |||
272 |