Conditions | 25 |
Total Lines | 106 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 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; |
||
20 | public CommandGroupAuto(Position position, Goal goal) { |
||
21 | //Get the state of the switches and scale for each round |
||
22 | gameData = DriverStation.getInstance().getGameSpecificMessage(); |
||
23 | |||
24 | // make sure everything is in the right state/position up here |
||
25 | Robot.SUB_CLAMP.closeArms(); |
||
26 | |||
27 | |||
28 | // EX: making sure flap is closed before auto starts |
||
29 | switch (position) { |
||
30 | case LEFT: |
||
31 | switch (goal){ |
||
32 | case RUN_FOR_IT: |
||
33 | addSequential(new CyborgCommandDriveUntilError()); |
||
34 | break; |
||
35 | case SWITCH: |
||
36 | if (gameData.charAt(0) == 'L'){ //When the switch is on the left |
||
37 | addSequential(new CyborgCommandDriveDistance(AutonomousConstants.DIST_TO_SWITCH_FROM_SIDE)); |
||
38 | addSequential(new CyborgCommandRotateDegrees(AutonomousConstants.ROT_90_CLOCKWISE)); |
||
39 | Robot.SUB_MANIPULATOR.spit(); |
||
40 | } else { //When the switch is on the right |
||
41 | |||
42 | } |
||
43 | Robot.SUB_MANIPULATOR.spit(); |
||
44 | break; |
||
45 | case ENEMY_SWITCH: |
||
46 | if (gameData.charAt(2) == 'L'){ //When switch is on the left |
||
|
|||
47 | |||
48 | } else { //When switch is on the right |
||
49 | |||
50 | } |
||
51 | break; |
||
52 | case SCALE: |
||
53 | if (gameData.charAt(1) == 'L'){ //When scale is on the left |
||
54 | |||
55 | } else { //When scale is on the right |
||
56 | |||
57 | } |
||
58 | break; |
||
59 | |||
60 | case BEST_OPTION: |
||
61 | break; |
||
62 | } |
||
63 | break; |
||
64 | |||
65 | case CENTER: |
||
66 | switch (goal){ |
||
67 | case RUN_FOR_IT: |
||
68 | addSequential(new CyborgCommandDriveUntilError()); |
||
69 | break; |
||
70 | case SWITCH: |
||
71 | addSequential(new CyborgCommandDriveDistance(AutonomousConstants.DIST_PASS_PORTAL)); |
||
72 | if (gameData.charAt(0) == 'L'){ //When the switch is on the left |
||
73 | addSequential(new CyborgCommandRotateDegrees(AutonomousConstants.ROT_90_COUNTERCLOCKWISE)); |
||
74 | addSequential(new CyborgCommandDriveDistance(AutonomousConstants.DIST_CENTER_LINE_SWITCH_ALIGN)); |
||
75 | addSequential(new CyborgCommandRotateDegrees(AutonomousConstants.ROT_90_CLOCKWISE)); |
||
76 | addSequential(new CyborgCommandDriveDistance(AutonomousConstants.DIST_WALL_TO_BLOCKS |
||
77 | + AutonomousConstants.DIST_BLOCKS_TO_SWITCH |
||
78 | - AutonomousConstants.DIST_PASS_PORTAL)); |
||
79 | |||
80 | } else { //When the switch is on the right |
||
81 | addSequential(new CyborgCommandRotateDegrees(AutonomousConstants.ROT_90_CLOCKWISE)); |
||
82 | addSequential(new CyborgCommandDriveDistance(AutonomousConstants.DIST_CENTER_LINE_SWITCH_ALIGN)); |
||
83 | addSequential(new CyborgCommandRotateDegrees(AutonomousConstants.ROT_90_COUNTERCLOCKWISE)); |
||
84 | // addParallel(new CyborgCommandGoToMid()); |
||
85 | addSequential(new CyborgCommandDriveDistance(AutonomousConstants.DIST_WALL_TO_BLOCKS |
||
86 | + AutonomousConstants.DIST_BLOCKS_TO_SWITCH |
||
87 | - AutonomousConstants.DIST_PASS_PORTAL)); |
||
88 | } |
||
89 | break; |
||
90 | case ENEMY_SWITCH: |
||
91 | break; |
||
92 | case SCALE: |
||
93 | break; |
||
94 | case BEST_OPTION: |
||
95 | break; |
||
96 | } |
||
97 | break; |
||
98 | |||
99 | case RIGHT: |
||
100 | switch (goal) { |
||
101 | case RUN_FOR_IT: |
||
102 | addSequential(new CyborgCommandDriveUntilError()); |
||
103 | break; |
||
104 | case SWITCH: |
||
105 | if (gameData.charAt(0) == 'R'){ //When the switch is on the right |
||
106 | addParallel(new CyborgCommandDriveDistance(AutonomousConstants.DIST_TO_SWITCH_FROM_SIDE)); |
||
107 | addSequential(new CyborgCommandRotateDegrees(AutonomousConstants.ROT_90_COUNTERCLOCKWISE)); |
||
108 | } else { //When the switch is on the left |
||
109 | |||
110 | } |
||
111 | break; |
||
112 | case ENEMY_SWITCH: |
||
113 | |||
114 | break; |
||
115 | case SCALE: |
||
116 | if (gameData.charAt(1) == 'R'){ //When scale is on the right |
||
117 | |||
118 | } else { //When scale is on the left |
||
119 | |||
120 | } |
||
121 | break; |
||
122 | case BEST_OPTION: |
||
123 | break; |
||
124 | } |
||
125 | break; |
||
126 | } |
||
128 | } |
Using constants for hard-coded numbers is a best practice. A constant’s name can explain the rationale behind this magic number. It is also easier to find if you ever need to change it.