| Total Lines | 97 |
| Code Lines | 90 |
| 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:
| 1 | package org.gannacademy.cdf.turtlelogo; |
||
| 221 | @Override |
||
| 222 | public void run() { |
||
| 223 | if (!threadStarted) { |
||
| 224 | threadStarted = true; |
||
| 225 | while (true) { |
||
|
|
|||
| 226 | if (activeInstruction == null) { |
||
| 227 | if (!instructions.isEmpty()) { |
||
| 228 | activeInstruction = instructions.remove(); |
||
| 229 | switch (activeInstruction.getVerb()) { |
||
| 230 | case MOVE: |
||
| 231 | MOVE_targetSteps = activeInstruction.getDoubleParam(); |
||
| 232 | MOVE_steps = 0; |
||
| 233 | break; |
||
| 234 | case MOVE_TO: |
||
| 235 | double dx = activeInstruction.getPointParam().getX() - getX(), |
||
| 236 | dy = activeInstruction.getPointParam().getY() - getY(); |
||
| 237 | MOVE_targetSteps = Math.hypot(dx, dy); |
||
| 238 | MOVE_steps = 0; |
||
| 239 | MOVE_TO_tempHeadingInRadians = Math.atan2(dy, dx); |
||
| 240 | break; |
||
| 241 | case TURN: |
||
| 242 | TURN_targetDegrees = activeInstruction.getDoubleParam(); |
||
| 243 | TURN_degrees = 0; |
||
| 244 | break; |
||
| 245 | case HEAD: |
||
| 246 | activeInstruction.convertTo(Verb.TURN); |
||
| 247 | if (Math.abs(getHeadingInDegrees() - activeInstruction.getDoubleParam()) > 180.0) { |
||
| 248 | TURN_targetDegrees = (360 - Math.abs(getHeadingInDegrees() - activeInstruction.getDoubleParam())) * (getHeadingInDegrees() > activeInstruction.getDoubleParam() ? 1 : -1); |
||
| 249 | } else { |
||
| 250 | TURN_targetDegrees = getHeadingInDegrees() - activeInstruction.getDoubleParam(); |
||
| 251 | } |
||
| 252 | TURN_degrees = 0; |
||
| 253 | break; |
||
| 254 | case PEN_UP: |
||
| 255 | super.penUp(); |
||
| 256 | activeInstruction = null; |
||
| 257 | break; |
||
| 258 | case PEN_DOWN: |
||
| 259 | super.penDown(); |
||
| 260 | activeInstruction = null; |
||
| 261 | break; |
||
| 262 | case PEN_COLOR: |
||
| 263 | super.penColor(activeInstruction.getColorParam()); |
||
| 264 | activeInstruction = null; |
||
| 265 | break; |
||
| 266 | case PEN_WIDTH: |
||
| 267 | super.penWidth(activeInstruction.getDoubleParam()); |
||
| 268 | activeInstruction = null; |
||
| 269 | break; |
||
| 270 | case HIDE: |
||
| 271 | super.hide(); |
||
| 272 | activeInstruction = null; |
||
| 273 | break; |
||
| 274 | case SHOW: |
||
| 275 | super.show(); |
||
| 276 | activeInstruction = null; |
||
| 277 | break; |
||
| 278 | case TELEPORT: |
||
| 279 | super.teleport(activeInstruction.getPointParam().getX(), activeInstruction.getPointParam().getY()); |
||
| 280 | activeInstruction = null; |
||
| 281 | break; |
||
| 282 | case HOME: |
||
| 283 | super.home(); |
||
| 284 | activeInstruction = null; |
||
| 285 | break; |
||
| 286 | case SPEED: |
||
| 287 | frameDelay = activeInstruction.getLongParam(); |
||
| 288 | activeInstruction = null; |
||
| 289 | break; |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } else if (System.currentTimeMillis() > tick + frameDelay) { |
||
| 293 | tick = System.currentTimeMillis(); |
||
| 294 | switch (activeInstruction.getVerb()) { |
||
| 295 | case MOVE: |
||
| 296 | case MOVE_TO: |
||
| 297 | if (Math.abs(MOVE_steps) >= Math.abs(MOVE_targetSteps)) { |
||
| 298 | if (activeInstruction.getVerb() == Verb.MOVE) { |
||
| 299 | super.move(MOVE_targetSteps); |
||
| 300 | } else { |
||
| 301 | super.moveTo(activeInstruction.getPointParam().getX(), activeInstruction.getPointParam().getY()); |
||
| 302 | } |
||
| 303 | activeInstruction = null; |
||
| 304 | } else { |
||
| 305 | MOVE_steps += (MOVE_targetSteps > 0.0 ? 1 : -1); |
||
| 306 | } |
||
| 307 | break; |
||
| 308 | case TURN: |
||
| 309 | if (Math.abs(TURN_degrees) >= Math.abs(TURN_targetDegrees)) { |
||
| 310 | super.turn(TURN_targetDegrees); |
||
| 311 | activeInstruction = null; |
||
| 312 | } else { |
||
| 313 | TURN_degrees += (TURN_targetDegrees > 0.0 ? 1 : -1) * (frameDelay >= TURN_SPEED_CUTOFF ? 1 : TURN_SPEED_CUTOFF - frameDelay); |
||
| 314 | } |
||
| 315 | break; |
||
| 316 | } |
||
| 317 | getTerrarium().repaint(); |
||
| 318 | } |
||
| 355 |