We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

DRY Code

Another "rule of thumb" for writing maintainable code is "Don't Repeat Yourself" (DRY). It means that, when possible, you should avoid writing the same code in multiple places. Repeating code can be bad because:

  • If you need to change it, you have to change it in multiple places
  • If you forget to change it in one place, you'll have a bug
  • It's more work to write it over and over again

Assignment

Your manager noticed that "Age of Dragons" has a lot of repetitive code. She's asked you to update the fight_soldiers function so that the DPS (damage-per-second) calculation logic is only written in one place.

Notice how these two lines are practically identical:

soldier_one_dps = soldier_one["damage"] * soldier_one["attacks_per_second"]
soldier_two_dps = soldier_two["damage"] * soldier_two["attacks_per_second"]

The soldier with the greater DPS will win the fight!