Impact of
Program Design
Explore how design choices in code structure, algorithms, and data representation affect functionality, readability, and efficiency of software systems.
What is Program Design?
Program design is the process of planning how a program will be structured before and during implementation. It encompasses decisions about class hierarchy, method decomposition, data representation, and algorithm selection.
Three Pillars of Good Design
Why Design Matters
Core Principle: Method Decomposition
A well-designed program breaks complex tasks into small, focused methods. Each method should do one thing well and have a clear, descriptive name. This is the Single Responsibility Principle applied at the method level.
Class Design & Encapsulation
In Java, encapsulation means bundling data (instance variables) with the methods that operate on that data, while hiding internal state behind access modifiers. This is the foundation of robust class design.
private so external code cannot directly modify them.getX() and setX() to control how data is read and changed.Procedural vs Object-Oriented
| Procedural | Object-Oriented | |
|---|---|---|
| Organization | Functions that act on separate data | Objects that bundle data + behavior |
| Data Access | Global or passed as parameters | Private fields, controlled via methods |
| Reusability | Copy-paste functions | Inheritance & polymorphism |
| Scalability | Hard to manage as size grows | Modular: add classes independently |
| AP CSA Focus | Background knowledge | Primary paradigm tested |
How to Apply Good Design
int vs double, arrays vs ArrayList, based on requirements.Example: Poor Design
Example: Better Design
Design Impact: Algorithm Choice
Choosing the right algorithm is a critical design decision. Searching for an item in a sorted array can be done two ways:
Choosing Data Representation
How you store data shapes what operations are easy or hard. The AP CSA exam expects you to reason about arrays vs ArrayLists and primitive vs reference types.
| Array | ArrayList | |
|---|---|---|
| Size | Fixed at creation | Grows dynamically |
| Type | Primitives or objects | Objects only (autoboxing) |
| Insert/Remove | Manual shifting: \( O(n) \) | Built-in: still \( O(n) \) |
| Best When | Size is known, speed matters | Size varies, convenience matters |
One public counting interface, two hidden mechanisms
Two implementations can provide the same public counting behavior while storing their internal state differently. A representation change must preserve the interface’s promised behavior.Preserve public behavior across implementations
Starting at zero, one Counter stores count and increments it; another stores negativeCount and decrements it, returning its negation. Three increment calls make both getCount results 3. Their private representations differ while the public trace agrees.Design with Inheritance
Readable Code: Naming Conventions
double d;
void doStuff()
boolean flag;
double totalPrice;
void calculateGPA()
boolean isEligible;
Common Mistakes
public instead of private. This breaks encapsulation and allows invalid states.0.08 instead of named constants like TAX_RATE. Hard to understand and change.Design Principles Summary
| Principle | Good Practice | Anti-Pattern |
|---|---|---|
| Method Size | Small, single-purpose | Monolithic "god method" |
| Data Hiding | private fields + getters/setters |
public fields everywhere |
| Naming | Descriptive: getTotal() |
Cryptic: gt() |
| Algorithm | Match to data constraints | Always brute-force |
| Reusability | Inheritance, parameters | Copy-paste code blocks |
Question 1 of 5
Which of the following best describes the primary benefit of encapsulation in program design?
Question 2 of 5
A sorted array of \( 1{,}000{,}000 \) elements is searched using binary search. What is the maximum number of comparisons needed?
Question 3 of 5
Consider the following code:
public class Student {
public String name;
public int grade;
public double gpa;
}
A programmer uses this class throughout a large application. Later, the school changes its GPA policy so that GPA cannot exceed 4.0.
What design flaw makes this change difficult to implement?
gpa is public, any code anywhere can write student.gpa = 5.0;. If it were private with a setter, the setter could enforce the 4.0 maximum in one place.Question 4 of 5
A method named process() performs input validation, calculation, and output formatting. Which design principle does it violate?
validateInput(), calculate(), and formatOutput().Question 5 of 5
Which of the following is an advantage of using an ArrayList over a standard array?
ArrayList over arrays is dynamic resizing. Arrays have a fixed length set at creation, while ArrayLists automatically expand and contract as elements are added or removed.