Mathematics I / Functions and Modularity
Practice question · Multiple choice

Extracting repeated code into a function usually makes a program longer, not shorter, once the definition and calls are counted. Why is that a good trade?

Hints
  1. Suppose you find a bug in that logic. How many places must you edit in each version?
  2. Ask what a well-named function call tells a reader that a block of inline code does not.
Show the answer

B. One place where the logic lives, so a fix is made once.

Why

Duplicated logic means duplicated bugs, and the failure mode is fixing four of five copies, worse than not noticing, because the bug now appears intermittently. The name does real work too: is_valid_email(s) tells a reader what the code accomplishes without their reading how. A call costs a little, so modularity is bought at a small runtime price worth paying.

Read the lesson: Functions and Modularity →

Practise Functions and Modularity

The app has 6 more questions on this lesson, and keeps your place in the course. Mathematics I is free to start.

More questions on Functions and Modularity