Concepts / Code Organization and Structure

Code Organization and Structure

Reuse means writing a set of instructions once, naming it, and calling that name multiple times throughout a program

  • Programming

The Cost of Repetition

Imagine that a program must check whether a number is valid in five different places. Without reuse, the complete checking process would be written five separate times. The program would contain repeated instructions, and any improvement or correction would have to be applied to every copy. Reuse addresses this problem by allowing the instructions to be written once, given a name, and used wherever they are needed.

Reuse is the practice of writing a set of instructions once, naming it, and calling that name multiple times throughout a program. The named set becomes a reusable part of the program rather than a block that must be copied into every location where its work is needed.

usesusesusesValidation logiccopy 1ValidateEmailone definitionValidation logiccopy 2Sign-upcallValidation logiccopy 3Profile updatecallForm entrycall
What changes when repeated instructions are written once and called multiple times instead?

Execution Detours

Reuse changes the path that execution follows. When the program reaches a call to a named instruction set, it does not simply move to the next line. Execution jumps to the definition of that named set, runs all of its steps, and then returns to the location immediately after the call. The program therefore follows a detour before continuing its original work.

reachesjumps tofinishescontinues atEarlier programstepsCheckValidityrun all stepsCall CheckValidityReturnFollowing programsteps
What happens to program execution when it reaches a call to a named instruction set?

What do you think happens?

A program reaches a call to CheckValidity. Does it execute the next program statement first, or does it run CheckValidity before continuing?

  • It executes the next statement first.
  • It jumps to CheckValidity, completes its steps, and then continues.
  • It stops permanently at the call.
Reveal answer

Answer: It jumps to CheckValidity, completes its steps, and then continues.

A call creates a detour in execution. The program moves to the named instruction set, completes all of its steps, and returns to the location after the call.

One Definition, Many Callers

A single named instruction set can be invoked from many different locations. For example, ValidateEmail can be called during sign-up, during a profile update, and when an email is entered in a form. Each call reaches the same named definition and runs the same instructions. What changes is the location that makes the call, not the reusable instruction set itself.

callscallscallsValidateEmailone named definitionSign-upcallerProfile updatecallerForm entrycaller
How can several different places in a program connect to the same named set of instructions?

Tracing ValidateEmail

A program needs to validate an email during sign-up and later during a profile update. How does reuse organize this work?

Define once: The instructions for validating an email are placed in one named instruction set called ValidateEmail.

First call: When the sign-up part of the program calls ValidateEmail, execution moves to that definition and completes its steps.

First return: After validation finishes, execution returns to the program location immediately after the sign-up call.

Second call: When the profile-update part later calls ValidateEmail, execution moves to the same definition again.

Second return: After the same validation steps finish, execution returns to the location immediately after the profile-update call.

The validation instructions exist in one place, while two different program locations use them. A change to the definition applies to both callers.

Names as Abstractions

Naming a set of instructions creates an abstraction. The name hides the individual steps inside the set, allowing a reader to understand the program at a higher level. A line that calls ValidateEmail communicates the purpose of the operation without requiring the reader to trace every validation step immediately.

This abstraction also makes change easier. If the details of email validation need to be corrected or improved, the definition of ValidateEmail can be changed in one location. The places that call it can remain unchanged because they rely on the named operation rather than on its internal details.

Mistakes in Reasoning

  • Copying the same instructions into every location that needs them

    Each copy must be maintained separately, and a correction may not be applied consistently to all copies.

    Fix: Write the instructions once, give them a name, and call that name from each location.

  • Assuming execution simply continues to the next statement after a call

    Execution first jumps to the named instruction set, completes its steps, and then returns.

    Fix: Trace the call as a detour: call, jump to the definition, complete the steps, return, then continue.

  • Treating each caller as if it had its own separate definition

    The power of reuse comes from several locations invoking the same named instruction set.

    Fix: Look for the one-to-many relationship between one definition and multiple calls.

  • Believing that callers must know every internal step

    The name acts as an abstraction that hides the internal complexity from the calling locations.

    Fix: Separate the high-level purpose of the named instruction set from the details inside its definition.

EASY

A program checks whether a number is valid in five different places. Describe how you would organize the instructions using reuse. Then trace what happens when the program reaches the call in the third location.

Hints
  • Give the checking instructions one name.
  • Identify the five locations as callers of that name.
  • For the third location, explain the jump to the definition, completion of the steps, and return to the next statement.

A Reusable Design Habit

When the same logic is needed in several places, look for an opportunity to name it and reuse it instead of copying it. Think in two connected parts: the definition, where the instructions exist, and the callers, where the name is used. Then trace execution through the call and back to the point where the program paused.

  1. Reuse writes a set of instructions once, gives it a name, and calls that name wherever the instructions are needed. A call sends execution to the definition, and execution returns after the definition finishes. One named instruction set can serve many callers, reducing duplication and making changes easier to apply consistently. Naming also creates an abstraction that helps readers understand a program without following every internal step.

Key Takeaways

  • Reuse means writing instructions once, naming them, and calling that name multiple times.
  • Reuse reduces duplicated instructions and makes programs easier to maintain and modify.
  • Execution jumps from a call to the named definition, completes its steps, and returns to continue the original program.
  • One named instruction set can be called from many different program locations.
  • The name creates an abstraction that hides internal details while preserving a clear high-level purpose.