| Conditions | 15 |
| Total Lines | 63 |
| Code Lines | 50 |
| 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 PokemonDBService.process2Pokemon 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 | import { Injectable } from "@angular/core"; |
||
| 104 | |||
| 105 | process2Pokemon() { |
||
| 106 | const self = this; |
||
| 107 | |||
| 108 | _.forOwn(this.pokemon, function(value: any) { |
||
| 109 | if(value.moves !== undefined) { |
||
| 110 | for(let i = 0; i < value.moves.length; i++) { |
||
| 111 | const move = value.moves[i]; |
||
| 112 | if(move._move !== undefined) |
||
| 113 | continue; |
||
| 114 | |||
| 115 | move._move = move.move; |
||
| 116 | move.move = _.find(self.moves, ['name', _.startCase(_.lowerCase(move.move))]); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | if(value.initial !== undefined && value._initial === undefined) { |
||
| 121 | value._initial = _.cloneDeep(value.initial); |
||
| 122 | for(let i = 0; i < value.initial.length; i++) { |
||
| 123 | const initial = value.initial[i]; |
||
| 124 | value.initial[i] = _.find(self.moves, ['name', _.startCase(_.lowerCase(initial))]); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | if(value.tmHm !== undefined && value._tmHm === undefined) { |
||
| 129 | value._tmHm = _.cloneDeep(value.tmHm); |
||
| 130 | for(let i = 0; i < value.tmHm.length; i++) { |
||
| 131 | const tmHmEntry = value.tmHm[i]; |
||
| 132 | value.tmHm[i] = _.find(self.moves, ['_tm', tmHmEntry]); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | if(value.type1 !== undefined && value._type1 === undefined) { |
||
| 137 | value._type1 = value.type1; |
||
| 138 | value.type1 = _.find(self.types, ['name', _.startCase(_.lowerCase(value.type1))]); |
||
| 139 | } |
||
| 140 | |||
| 141 | if(value.type2 !== undefined && value._type2 === undefined) { |
||
| 142 | value._type2 = value.type2; |
||
| 143 | value.type2 = _.find(self.types, ['name', _.startCase(_.lowerCase(value.type2))]); |
||
| 144 | } |
||
| 145 | |||
| 146 | if(value.evolution !== undefined) { |
||
| 147 | const process = (entry: any) => { |
||
| 148 | if(entry.item !== undefined && entry._item === undefined) { |
||
| 149 | entry._item = entry.item; |
||
| 150 | entry.item = _.find(self.items, ['name', _.startCase(_.lowerCase(entry.item))]); |
||
| 151 | } |
||
| 152 | |||
| 153 | if(entry._toName !== undefined) |
||
| 154 | return; |
||
| 155 | |||
| 156 | entry._toName = entry.toName; |
||
| 157 | entry.toName = _.find(self.pokemon, ['name', _.startCase(_.lowerCase(entry.toName))]); |
||
| 158 | } |
||
| 159 | |||
| 160 | if(Array.isArray(value.evolution)) { |
||
| 161 | for(let i = 0; i < value.evolution.length; i++) { |
||
| 162 | process(value.evolution[i]); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | else |
||
| 166 | process(value.evolution); |
||
| 167 | } |
||
| 192 |