CSCI 370 - Lecture 9 Review: Principles of DRY, YAGNI, Delegation, and Intro to Agile


DRY Principle (Don’t Repeat Yourself)

Definition

The DRY principle emphasizes that each piece of knowledge or logic should exist in only one place in a codebase. Repeating logic across multiple places makes it harder to maintain and update code, as changes would need to be made in multiple locations.

Example

class AreaCalculator {
    public int area(int length, int width) {
        return length * width;
    }

    public int volume(int length, int width, int height) {
        int baseArea = area(length, width);
        return baseArea * height;
    }
}

Here, the volume method reuses the area method instead of duplicating the multiplication logic. This allows centralized control over how the area is calculated. If the calculation changes (e.g., for performance reasons), only the area method needs to be updated.

Benefits

Real-World Analogy

A method calculating string length might work in English, but break in Unicode or other languages. Keeping a single method ensures language compatibility issues are only dealt with once.


YAGNI (You Aren’t Gonna Need It)

Definition

YAGNI promotes writing only the code you currently need, not what you think you might need later. Avoid including features or methods that are not immediately required by the system.

Key Idea

Unused code:

Practical Application

Analogy

Leaving old or unused code in your codebase is like hoarding—eventually it becomes a fire hazard (or in programming, a maintenance nightmare).


Delegation Principle

Definition

Delegate behavior to the appropriate class, especially in inheritance hierarchies. Avoid writing child-specific logic in a parent class.

Bad Example (Anti-Pattern)

class Window {
    String color;
    public void draw() {
        if (color.equals("red")) {
            drawRedWindow();
        } else if (color.equals("blue")) {
            drawBlueWindow();
        }
    }
}

Good Example (Using Delegation)

class Window {
    public void draw() {
        // to be overridden
    }
}

class RedWindow extends Window {
    public void draw() {
        drawRedWindow();
    }
}

class BlueWindow extends Window {
    public void draw() {
        drawBlueWindow();
    }
}

Benefits


Introduction to Agile Methodology

What is Agile?

“Agile” refers to a set of software development methodologies based on iterative development, where requirements and solutions evolve through collaboration.

Definition of Agile (Literal)

Agile means flexible, quick to adapt—being able to shift direction with minimal overhead.

Agile Principles in Software Engineering

Historical Context

Agile vs. Waterfall

Agile Waterfall
Iterative, incremental Sequential, rigid
Adaptable to change Difficult to adapt mid-cycle
Continuous delivery Single final delivery

Real-World Insight

Due to intense demand for programmers during the early 2000s (e.g., Y2K fixes, internet boom), the industry needed faster, more adaptive development processes. Agile arose from this pressure.


Summary

These principles together help create cleaner, more maintainable, and efficient software systems.