Free degree-level programming lessons for careful independent study.

Degree Level Programmes · Programming 2 · Lesson 6

Controlled Boundary Breaking

Understand that design rules sometimes bend, but every boundary break needs a clear reason and a maintenance cost.

Lesson Overview

Understand that design rules sometimes bend, but every boundary break needs a clear reason and a maintenance cost.

Portfolio focus: Find one place where exposing state would be tempting.

ConceptPragmatic design choices
Run fileControlledBoundaryBreakingDemo.java
BaselineAUDIT: Created portfolio artefact
Evidence3 tasks

Starter: think before typing

Before running this pragmatic design choices example, find the line where the main idea becomes active. Write a prediction: what must already be true for that line to work, and what should be different after it runs? The checked run ends with `AUDIT: Created portfolio artefact`; predict how the focus line helps produce that evidence.

Learning Objectives

  • Explain why object boundaries exist.
  • Recognise when direct access would make code fragile.
  • Use package-private access or helper methods deliberately.
  • Justify a boundary exception in writing.

Learning Outcomes

  • By the end of the lesson, you can explain why object boundaries exist.
  • By the end of the lesson, you can recognise when direct access would make code fragile.
  • By the end of the lesson, you can use package-private access or helper methods deliberately.
  • By the end of the lesson, you can justify a boundary exception in writing.

Why this idea exists

Boundaries exist to protect meaning: private state, module edges and object responsibilities stop unrelated code from reaching in and changing details it should not own.

Controlled boundary breaking exists because real software sometimes needs carefully limited exceptions for testing, performance, migration, package-level collaboration or framework integration.

This topic fits the arc by adding professional judgement to earlier rules. A boundary exception is not a shortcut; it is a documented trade-off that should make the maintenance cost visible.

Deep dive

Mechanism in this example

The important mechanism is visible around `AuditLog.record("Created portfolio artefact");`. Read it as a concrete move in the program, not as decorative syntax: identify what value, object, branch, call or boundary is being created at that point.

Design pressure

This topic fits the arc by adding professional judgement to earlier rules. A boundary exception is not a shortcut; it is a documented trade-off that should make the maintenance cost visible.

Failure mode to watch

For Controlled Boundary Breaking, deliberately disturb the assumption behind `AuditLog.record("Created portfolio artefact");`: use an awkward value, missing input, wrong order of calls or boundary case. The useful question is how that disturbance exposes a weakness in the pragmatic design choices design.

Extension step

Extend the example by doing this: Design a method that preserves the boundary. The point is to make one small change that forces you to revisit the concept, rather than adding unrelated features.

Portfolio standard

The portfolio note should not repeat the lesson wording. It should show the edited code, the run result, and your own explanation of this evidence: write when you would accept a controlled exception.

Lesson visual

A locked object boundary with a carefully labelled maintenance hatch, showing that access exceptions need explicit reasons.
A locked object boundary with a carefully labelled maintenance hatch, showing that access exceptions need explicit reasons.Download visual

Type this and run it

Create ControlledBoundaryBreakingDemo.java, type the program, and run it before changing anything. This section is about reproducing the checked baseline.

public class ControlledBoundaryBreakingDemo {
  public static void main(String[] args) {
    AuditLog.record("Created portfolio artefact");
  }
}

class AuditLog {
  static void record(String message) {
    System.out.println("AUDIT: " + message);
  }
}

Build and run it with:

javac ControlledBoundaryBreakingDemo.java && java ControlledBoundaryBreakingDemo

Expected baseline: AUDIT: Created portfolio artefact

Run the code in your browser

Use the editor as an experiment surface. First run the checked version, then make one small change to the part of the program that demonstrates pragmatic design choices and compare the new behaviour with the reference output.

Line-by-line explanation

Read the code as a sequence of responsibilities. The focus line for this lesson is AuditLog.record("Created portfolio artefact");; the surrounding lines prepare it, use its result or make the behaviour observable.

public class ControlledBoundaryBreakingDemo {

This names the runnable class for the Controlled Boundary Breaking example, giving the compiler and JVM one clear unit to build.

public static void main(String[] args) {

This is the program entry point. In Controlled Boundary Breaking, it keeps the demonstration of pragmatic design choices in one traceable starting script.

AuditLog.record("Created portfolio artefact");

This calls AuditLog.record with "Created portfolio artefact" in Controlled Boundary Breaking. Look for the method definition to see what work actually happens.

}

This closes the innermost Controlled Boundary Breaking block, so the immediately preceding method, branch or loop has finished.

}

This closes the outer Controlled Boundary Breaking structure, returning the reader to the surrounding class or file.

class AuditLog {

This starts a supporting class so Controlled Boundary Breaking can separate the lesson idea into its own named responsibility.

static void record(String message) {

This starts record, a named Controlled Boundary Breaking operation. Its parameters describe what information comes in; its body decides what work is done.

System.out.println("AUDIT: " + message);

This prints "AUDIT: " + message as the observable evidence for Controlled Boundary Breaking. The output lets the learner check whether the pragmatic design choices idea behaved as predicted.

}

This closing brace number 3 completes another layer of the Controlled Boundary Breaking source structure Java has been checking.

}

This closing brace number 4 completes another layer of the Controlled Boundary Breaking source structure Java has been checking.

Worked example

From code to explanation

Problem: Use Controlled Boundary Breaking to complete a small portfolio-quality step: Find one place where exposing state would be tempting.

Method: Locate the line `AuditLog.record("Created portfolio artefact");`, explain the exact role it plays, then decide what you would change to extend the example without changing the whole program.

Reveal worked answer

The checked run should produce `AUDIT: Created portfolio artefact`. A strong answer links the result back to pragmatic design choices: what was created, selected, stored, called or protected, and why that matters for the portfolio task.

Trace the program

Before: Before the key operation, identify the relevant value, object, branch or resource that the pragmatic design choices concept depends on.

During: Trace `AuditLog.record("Created portfolio artefact");` as the Controlled Boundary Breaking example executes. Say whether that operation creates data, checks a condition, calls behaviour, stores information or crosses a boundary.

After: Compare the run with the expected evidence: `AUDIT: Created portfolio artefact`.

Change: Now design a method that preserves the boundary, run again, and explain the smallest reason the behaviour changed.

Common misconception

A common mistake in controlled boundary breaking is treating the example as a finished answer. For pragmatic design choices, the important question is narrower: which operation carries the idea, what does it make possible, and what would break if you changed it carelessly?

Quick checks

1. In this Controlled Boundary Breaking example, what is the best reason to focus on `AuditLog.record("Created portfolio artefact");`?

2. Which evidence is strongest after you edit and rerun this example?

How to study this lesson

For Controlled Boundary Breaking, predict how pragmatic design choices changes the run before you press Run.

Use the first portfolio task as your main edit: Find one place where exposing state would be tempting.

Use the second task as your variation: Design a method that preserves the boundary.

Finish with evidence, not a diary entry: Write when you would accept a controlled exception.

Portfolio Practice

  1. Find one place where exposing state would be tempting.
  2. Design a method that preserves the boundary.
  3. Write when you would accept a controlled exception.

Final self-check

Can you explain the key operation?

Explain the line identified in the quick check in one or two sentences. Your answer should say what it does before the output AUDIT: Created portfolio artefact appears.

Can you justify the portfolio evidence?

Your evidence should include the original run, one edited run, and a short note explaining how the edit affected pragmatic design choices.

Study route

Practise pragmatic design choices by predicting the Java example, typing it, running it in the browser, tracing the result and saving portfolio evidence.

Next, move into Polymorphism: Cost and Benefit and carry forward one improvement from this lesson into the next program.