Conditions | 21 |
Total Lines | 81 |
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 org.usfirst.frc.team3695.robot.auto.CommandGroupAuto.CommandGroupAuto(Position,Goal) 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 org.usfirst.frc.team3695.robot.auto; |
||
18 | public CommandGroupAuto(Position position, Goal goal) { |
||
19 | //Get the state of the switches and scale for each round |
||
20 | gameData = DriverStation.getInstance().getGameSpecificMessage(); |
||
21 | |||
22 | // make sure everything is in the right state/position up here |
||
23 | Robot.SUB_CLAMP.closeArms(); |
||
24 | // EX: making sure flap is closed before auto starts |
||
25 | switch (position) { |
||
26 | case LEFT: |
||
27 | switch (goal){ |
||
28 | case RUN_FOR_IT: |
||
29 | addSequential(new CyborgCommandDriveUntilError(Position.FORWARD)); |
||
30 | break; |
||
31 | case SWITCH: |
||
32 | if (gameData.charAt(0) == 'L'){ //When the switch is on the left |
||
33 | addParallel(new CyborgCommandDriveDistance(Constants.Autonomous.DIST_TO_SWITCH_FROM_SIDE)); |
||
34 | addSequential(new CyborgCommandGoToMid()); |
||
35 | addSequential(new CyborgCommandRotateDegrees(Constants.Autonomous.ROT_90_CLOCKWISE)); |
||
36 | } else { //When the switch is on the right |
||
37 | |||
38 | } |
||
39 | break; |
||
40 | case ENEMY_SWITCH: |
||
41 | break; |
||
42 | case SCALE: |
||
|
|||
43 | break; |
||
44 | case BEST_OPTION: |
||
45 | break; |
||
46 | } |
||
47 | break; |
||
48 | |||
49 | case CENTER: |
||
50 | switch (goal){ |
||
51 | case RUN_FOR_IT: |
||
52 | addSequential(new CyborgCommandDriveUntilError(Position.FORWARD)); |
||
53 | break; |
||
54 | case SWITCH: |
||
55 | //Raise the mast to midpoint and pass the portal to avoid collisions |
||
56 | addParallel(new CyborgCommandGoToMid()); |
||
57 | addSequential(new CyborgCommandDriveDistance(Constants.Autonomous.DIST_PASS_PORTAL)); |
||
58 | if (gameData.charAt(0) == 'L'){ //When the switch is on the left |
||
59 | addSequential(new CyborgCommandRotateDegrees(Constants.Autonomous.ROT_90_COUNTERCLOCKWISE)); |
||
60 | addSequential(new CyborgCommandDriveDistance(Constants.Autonomous.DIST_CENTER_LINE_SWITCH_ALIGN)); |
||
61 | addSequential(new CyborgCommandRotateDegrees(Constants.Autonomous.ROT_90_CLOCKWISE)); |
||
62 | addSequential(new CyborgCommandDriveDistance(Constants.Autonomous.DIST_WALL_TO_BLOCKS |
||
63 | + Constants.Autonomous.DIST_BLOCKS_TO_SWITCH |
||
64 | - Constants.Autonomous.DIST_PASS_PORTAL)); |
||
65 | |||
66 | } else { //When the switch is on the right |
||
67 | addSequential(new CyborgCommandRotateDegrees(Constants.Autonomous.ROT_90_CLOCKWISE)); |
||
68 | addSequential(new CyborgCommandDriveDistance(Constants.Autonomous.DIST_CENTER_LINE_SWITCH_ALIGN)); |
||
69 | addSequential(new CyborgCommandRotateDegrees(Constants.Autonomous.ROT_90_COUNTERCLOCKWISE)); |
||
70 | addSequential(new CyborgCommandDriveDistance(Constants.Autonomous.DIST_WALL_TO_BLOCKS |
||
71 | + Constants.Autonomous.DIST_BLOCKS_TO_SWITCH |
||
72 | - Constants.Autonomous.DIST_PASS_PORTAL)); |
||
73 | } |
||
74 | break; |
||
75 | case ENEMY_SWITCH: |
||
76 | break; |
||
77 | case SCALE: |
||
78 | break; |
||
79 | case BEST_OPTION: |
||
80 | break; |
||
81 | } |
||
82 | break; |
||
83 | |||
84 | case RIGHT: |
||
85 | switch (goal) { |
||
86 | case RUN_FOR_IT: |
||
87 | addSequential(new CyborgCommandDriveUntilError(Position.FORWARD)); |
||
88 | break; |
||
89 | case SWITCH: |
||
90 | break; |
||
91 | case ENEMY_SWITCH: |
||
92 | break; |
||
93 | case SCALE: |
||
94 | break; |
||
95 | case BEST_OPTION: |
||
96 | break; |
||
97 | } |
||
98 | break; |
||
99 | } |
||
102 |
case statements can be bundled like this: