| Conditions | 14 |
| Total Lines | 127 |
| Code Lines | 117 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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.xlsx.XlsxProgramParser.extractWorkoutItem(Sheet,int,int,ExcelGoal,String,List) 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.xlsx; |
||
| 225 | private Optional<WorkoutItem> extractWorkoutItem( |
||
| 226 | final Sheet sheet, |
||
| 227 | int workoutItemIndex, |
||
| 228 | int workoutIndex, |
||
| 229 | ExcelGoal excelGoal, |
||
| 230 | String workoutName, |
||
| 231 | List<ExcelExercise> excelExercises) { |
||
| 232 | final int multiplyCoeff = 7; |
||
| 233 | WorkoutItem workoutItem = new WorkoutItem(); |
||
| 234 | workoutItem.setRowIndex(4 + 4 + workoutItemIndex * multiplyCoeff); |
||
| 235 | workoutItem.setColumnIndex(2 + workoutIndex); |
||
| 236 | final Optional<String> exerciseName = |
||
| 237 | getStringOrEmpty( |
||
| 238 | getCellData( |
||
| 239 | sheet, 5 + 4 + workoutItemIndex * multiplyCoeff, 2 + workoutIndex)); |
||
| 240 | if (!exerciseName.isPresent()) { |
||
| 241 | excelGoal |
||
| 242 | .getErrors() |
||
| 243 | .add( |
||
| 244 | "Exercise name not found. Goal " |
||
| 245 | + excelGoal.getName() |
||
| 246 | + ", workout " |
||
| 247 | + workoutName |
||
| 248 | + "."); |
||
| 249 | return Optional.empty(); |
||
| 250 | } |
||
| 251 | Optional<ExcelExercise> excelExercise = |
||
| 252 | excelExercises.stream() |
||
| 253 | .filter( |
||
| 254 | exercise -> |
||
| 255 | getOnlySymbols(exercise.getExercise_name()) |
||
| 256 | .equalsIgnoreCase( |
||
| 257 | getOnlySymbols(exerciseName.get()))) |
||
| 258 | .findFirst(); |
||
| 259 | if (!excelExercise.isPresent()) { |
||
| 260 | excelGoal |
||
| 261 | .getErrors() |
||
| 262 | .add( |
||
| 263 | "Exercise name (" |
||
| 264 | + exerciseName.get() |
||
| 265 | + ") not recognised. Goal " |
||
| 266 | + excelGoal.getName() |
||
| 267 | + ", workout " |
||
| 268 | + workoutName |
||
| 269 | + "."); |
||
| 270 | } else { |
||
| 271 | workoutItem.setExerciseId(excelExercise.get().getExercise_id()); |
||
| 272 | } |
||
| 273 | Number setsInp = |
||
| 274 | getNumberOrZerro( |
||
| 275 | getCellData( |
||
| 276 | sheet, 5 + 5 + workoutItemIndex * multiplyCoeff, 2 + workoutIndex)); |
||
| 277 | Object repetitionsInp = |
||
| 278 | getStringOrNumberOrNull( |
||
| 279 | getCellData( |
||
| 280 | sheet, 5 + 6 + workoutItemIndex * multiplyCoeff, 2 + workoutIndex)); |
||
| 281 | Object weightInp = |
||
| 282 | getStringOrNumberOrNull( |
||
| 283 | getCellData( |
||
| 284 | sheet, 5 + 7 + workoutItemIndex * multiplyCoeff, 2 + workoutIndex)); |
||
| 285 | workoutItem.getInput().setExercise(exerciseName.orElse(null)); |
||
| 286 | if (repetitionsInp instanceof String || weightInp instanceof String) { |
||
| 287 | workoutItem.getInput().setSets(new ArrayList<>()); |
||
| 288 | for (int index = 0; index < setsInp.intValue(); index += 1) { |
||
| 289 | InputSet inputSet = new InputSet(); |
||
| 290 | if (repetitionsInp instanceof String) { |
||
| 291 | String[] repetitionsInps = ((String) repetitionsInp).split("\\s*,\\s*"); |
||
| 292 | if (exerciseName.orElse("").contains("Plank") |
||
| 293 | || repetitionsInps[index].contains("min") |
||
| 294 | || "Time" |
||
| 295 | .equalsIgnoreCase( |
||
| 296 | getStringOrNull( |
||
| 297 | getCellData( |
||
| 298 | sheet, |
||
| 299 | 5 |
||
| 300 | + 6 |
||
| 301 | + workoutItemIndex |
||
| 302 | * multiplyCoeff, |
||
| 303 | 1)))) { |
||
| 304 | inputSet.setTimeInMin( |
||
| 305 | getFloatOrNull(extractFloatNumbers(repetitionsInps[index]))); |
||
| 306 | } else { |
||
| 307 | inputSet.setRepetitions( |
||
| 308 | getIntegerOrNull(extractNumbers(repetitionsInps[index]))); |
||
| 309 | } |
||
| 310 | } else { |
||
| 311 | if ("Time" |
||
| 312 | .equalsIgnoreCase( |
||
| 313 | getStringOrNull( |
||
| 314 | getCellData( |
||
| 315 | sheet, |
||
| 316 | 5 + 6 + workoutItemIndex * multiplyCoeff, |
||
| 317 | 1)))) { |
||
| 318 | inputSet.setTimeInMin(getFloatOrNull(repetitionsInp)); |
||
| 319 | } else { |
||
| 320 | inputSet.setRepetitions(getIntegerOrNull(repetitionsInp)); |
||
| 321 | } |
||
| 322 | } |
||
| 323 | if (weightInp instanceof String) { |
||
| 324 | String[] weightInps = ((String) weightInp).split("\\s*,\\s*"); |
||
| 325 | inputSet.setWeight( |
||
| 326 | getFloatOrNull( |
||
| 327 | extractFloatNumbers( |
||
| 328 | weightInps[Math.min(index, weightInps.length - 1)]))); |
||
| 329 | } else { |
||
| 330 | inputSet.setWeight(getFloatOrNull(weightInp)); |
||
| 331 | } |
||
| 332 | workoutItem.getInput().getSets().add(inputSet); |
||
| 333 | } |
||
| 334 | } else { |
||
| 335 | InputSet inputSet = new InputSet(); |
||
| 336 | if ("Time" |
||
| 337 | .equalsIgnoreCase( |
||
| 338 | getStringOrNull( |
||
| 339 | getCellData( |
||
| 340 | sheet, 5 + 6 + workoutItemIndex * multiplyCoeff, 1)))) { |
||
| 341 | inputSet.setTimeInMin(getFloatOrNull(repetitionsInp)); |
||
| 342 | } else { |
||
| 343 | inputSet.setRepetitions(getIntegerOrNull(repetitionsInp)); |
||
| 344 | } |
||
| 345 | inputSet.setWeight(getFloatOrNull(weightInp)); |
||
| 346 | workoutItem.getInput().setSets(new ArrayList<>()); |
||
| 347 | for (int index = 0; index < setsInp.intValue(); index += 1) { |
||
| 348 | workoutItem.getInput().getSets().add(inputSet); |
||
| 349 | } |
||
| 350 | } |
||
| 351 | return Optional.of(workoutItem); |
||
| 352 | } |
||
| 452 |
Default branches should deal with the unexpected. At a minimum, they should log the error and if applicable, return a default value (null, empty collection). If you really do not expect the default branch to ever be use, throw a RuntimeException when it is.