Refactoring: Changing Structure Without Changing Behaviour · graine 1 · A4, économe en encre. Le corrigé s'imprime sur sa propre page, pour les adultes.
En anglais

Tidying code without breaking it

Informatique · Génie logiciel · de 19 à 21 ans
Nom ______________________   Date ____________
  1. Which check should you run before editing and after each small refactoring step?

    • Only the compiler's syntax check
    • Only the code's line count
    • The relevant automated tests
  2. Which must be preserved during a refactoring?

    • Returned values and effects
    • The order of private helper definitions
    • The internal arrangement of helper functions
  3. Replacing a whole block in one edit automatically rules out refactoring, even if all observable behaviour is preserved.

    En entourer un :   Vrai   Faux

  4. You will rename and move a class. Which plan makes failures easier to locate?

    • Rename and move together, then update imports
    • Move the class, leave its existing uses in place, then test
    • Separate the changes, update uses and imports, and check each
  5. A billing function reads input, computes a bill and prints it. Which extraction plan best supports preserving its behaviour?

    • Extract the bill and revise its rounding
    • Extract one job, preserve its inputs and effects, then test
    • Extract printing and check just the returned total
  6. A previously passing check fails after one extraction. What is a useful next action?

    • Update the expectation to match the extracted code
    • Investigate the failure; undo the latest edit if needed
    • Finish extracting the remaining jobs before inspecting this failure
  7. Passing tests after every refactoring step prove that behaviour is unchanged for all inputs and effects.

    En entourer un :   Vrai   Faux

  8. A teammate combines a bug fix with helper extraction and labels the whole change refactoring. How should the review distinguish the work?

    • Separate the intended behaviour change from the restructuring
    • Review the extracted helpers and leave the bug fix implicit
    • Use the passing old tests to review the combined change as restructuring
LightMySky · lightmysky.comW1-mt_HHZufOyTg4-s1

Corrigé

Pour les adultes. Replier cette page avant de passer le reste.

Tidying code without breaking it W1-mt_HHZufOyTg4-s1

  1. The relevant automated tests · Relevant tests compare tested behaviour before and after the edit. A pass is evidence for those checks, not proof of full equivalence.
  2. Returned values and effects · Refactoring preserves observable behaviour. Private helper definitions may be reordered or rearranged while preserving that behaviour and promised public import paths.
  3. Faux · Edit size alone does not decide the classification. Small steps make behaviour-preserving changes easier to check.
  4. Separate the changes, update uses and imports, and check each · Separate checks help identify which change introduced a problem. Either rename-first or move-first can work.
  5. Extract one job, preserve its inputs and effects, then test · Extract one job while preserving what it receives, returns and does. Check before extracting the next job.
  6. Investigate the failure; undo the latest edit if needed · Inspect the failure before continuing. Undoing the small edit can restore the baseline while you find its cause.
  7. Faux · Passing tests establish their checks. Untested inputs, effects or weak assertions can leave a behaviour change undetected.
  8. Separate the intended behaviour change from the restructuring · The bug fix intentionally changes behaviour. The extraction should preserve behaviour and can be reviewed separately.