Conditions | 13 |
Total Lines | 78 |
Code Lines | 65 |
Lines | 21 |
Ratio | 26.92 % |
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 com.osomapps.pt.user.UserService.updateUser(String,UserRequestDTO) 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 | package com.osomapps.pt.user; |
||
195 | UserResponseDTO updateUser(String token, UserRequestDTO userRequest) { |
||
196 | final InUserLogin inUserLogin = checkUserToken(token); |
||
197 | final InUser inUser = inUserLogin.getInUser(); |
||
198 | if (userRequest.getGender() != null) { |
||
199 | inUser.setD_sex(userRequest.getGender()); |
||
200 | } |
||
201 | if (userRequest.getAge() != null) { |
||
202 | inUser.setAge(userRequest.getAge().floatValue()); |
||
203 | } |
||
204 | if (userRequest.getHeight() != null) { |
||
205 | inUser.setHeight(userRequest.getHeight().floatValue()); |
||
206 | } |
||
207 | if (userRequest.getWeight() != null) { |
||
208 | inUser.setWeight(userRequest.getWeight().floatValue()); |
||
209 | } |
||
210 | if (userRequest.getLevel() != null) { |
||
211 | inUser.setD_level(Integer.toString(userRequest.getLevel().getLevel())); |
||
212 | } |
||
213 | final MapBindingResult errors = |
||
214 | new MapBindingResult(new HashMap<>(), String.class.getName()); |
||
215 | dataurlValidator.validate(userRequest.getAvatar_dataurl(), errors); |
||
216 | if (errors.hasErrors()) { |
||
217 | throw new UnauthorizedException(errors.getAllErrors().get(0).getDefaultMessage()); |
||
218 | } |
||
219 | if (userRequest.getName() != null) { |
||
220 | final MapBindingResult errorsName = |
||
221 | new MapBindingResult(new HashMap<>(), String.class.getName()); |
||
222 | nameValidator.validate(userRequest.getName(), errorsName); |
||
223 | if (errorsName.hasErrors()) { |
||
224 | throw new UnauthorizedException( |
||
225 | errorsName.getAllErrors().get(0).getDefaultMessage()); |
||
226 | } |
||
227 | setUserName(inUser, userRequest.getName()); |
||
228 | } |
||
229 | View Code Duplication | if (userRequest.getGoals() != null) { |
|
230 | if (userRequest.getGoals().size() > 2) { |
||
231 | throw new UnauthorizedException("Amount of goals must be not more than 2"); |
||
232 | } |
||
233 | inUserGoalRepository.deleteAll(inUser.getInUserGoals()); |
||
234 | inUser.setInUserGoals(new ArrayList<>()); |
||
235 | userRequest |
||
236 | .getGoals() |
||
237 | .forEach( |
||
238 | (userGoalRequestDTO) -> { |
||
239 | Goal goal = |
||
240 | goalRepository |
||
241 | .findById(userGoalRequestDTO.getId()) |
||
242 | .orElse(null); |
||
243 | if (goal == null) { |
||
244 | throw new UnauthorizedException( |
||
245 | "Goal with id " |
||
246 | + userGoalRequestDTO.getId() |
||
247 | + " not found"); |
||
248 | } |
||
249 | String value = null; |
||
250 | try { |
||
251 | value = |
||
252 | new ObjectMapper() |
||
253 | .writeValueAsString( |
||
254 | userGoalRequestDTO.getValues()); |
||
255 | } catch (IOException ex) { |
||
256 | } |
||
257 | inUser.getInUserGoals() |
||
258 | .add( |
||
259 | inUserGoalRepository.save( |
||
260 | new InUserGoal() |
||
261 | .setGoalId( |
||
262 | userGoalRequestDTO.getId()) |
||
263 | .setD_goal_title( |
||
264 | goal.getDGoalTitle()) |
||
265 | .setD_goal_title_2( |
||
266 | goal.getDGoalTitle2()) |
||
267 | .setGoal_value(value))); |
||
268 | }); |
||
269 | } |
||
270 | inUser.setAvatar_dataurl(userRequest.getAvatar_dataurl()); |
||
271 | inUser.setUpdated(LocalDateTime.now()); |
||
272 | return userToDto(inUserRepository.save(adminProgramAssignService.assign(inUser))); |
||
273 | } |
||
275 |