Index of every page on this site: llms.txt
All skills
grahamcrackers avatar

/clean-code-principles

@ca83de4
Run it yourself

Clean code principles for readable, maintainable TypeScript and React codebases. Covers naming, functions, abstraction, composition, error handling, comments, and code smells. Use when writing new code, refactoring, reviewing code quality, or when the user asks about clean code, readability, or maintainability.

Use this Skill: https://skilld.dev/gh/grahamcrackers/skills/clean-code-principles

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencespattern-observer.md

≈1.4k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Observer

Observer defines a subscription mechanism to notify multiple objects about state changes. Learn more

Category: Behavioral

TypeScript Example

/**
 * The Subject interface declares a set of methods for managing subscribers.
 */
interface Subject {
    // Attach an observer to the subject.
    attach(observer: Observer): void;

    // Detach an observer from the subject.
    detach(observer: Observer): void;

    // Notify all observers about an event.
    notify(): void;
}

/**
 * The Subject owns some important state and notifies observers when the state
 * changes.
 */
class ConcreteSubject implements Subject {
    /**
     * @type {number} For the sake of simplicity, the Subject's state, essential
     * to all subscribers, is stored in this variable.
     */
    public state: number;

    /**
     * @type {Observer[]} List of subscribers. In real life, the list of
     * subscribers can be stored more comprehensively (categorized by event
     * type, etc.).
     */
    private observers: Observer[] = [];

    /**
     * The subscription management methods.
     */
    public attach(observer: Observer): void {
        const isExist = this.observers.includes(observer);
        if (isExist) {
            return console.log("Subject: Observer has been attached already.");
        }

        console.log("Subject: Attached an observer.");
        this.observers.push(observer);
    }

    public detach(observer: Observer): void {
        const observerIndex = this.observers.indexOf(observer);
        if (observerIndex === -1) {
            return console.log("Subject: Nonexistent observer.");
        }

        this.observers.splice(observerIndex, 1);
        console.log("Subject: Detached an observer.");
    }

    /**
     * Trigger an update in each subscriber.
     */
    public notify(): void {
        console.log("Subject: Notifying observers...");
        for (const observer of this.observers) {
            observer.update(this);
        }
    }

    /**
     * Usually, the subscription logic is only a fraction of what a Subject can
     * really do. Subjects commonly hold some important business logic, that
     * triggers a notification method whenever something important is about to
     * happen (or after it).
     */
    public someBusinessLogic(): void {
        console.log("\nSubject: I'm doing something important.");
        this.state = Math.floor(Math.random() * (10 + 1));

        console.log(`Subject: My state has just changed to: ${this.state}`);
        this.notify();
    }
}

/**
 * The Observer interface declares the update method, used by subjects.
 */
interface Observer {
    // Receive update from subject.
    update(subject: Subject): void;
}

/**
 * Concrete Observers react to the updates issued by the Subject they had been
 * attached to.
 */
class ConcreteObserverA implements Observer {
    public update(subject: Subject): void {
        if (subject instanceof ConcreteSubject && subject.state < 3) {
            console.log("ConcreteObserverA: Reacted to the event.");
        }
    }
}

class ConcreteObserverB implements Observer {
    public update(subject: Subject): void {
        if (subject instanceof ConcreteSubject && (subject.state === 0 || subject.state >= 2)) {
            console.log("ConcreteObserverB: Reacted to the event.");
        }
    }
}

/**
 * The client code.
 */

const subject = new ConcreteSubject();

const observer1 = new ConcreteObserverA();
subject.attach(observer1);

const observer2 = new ConcreteObserverB();
subject.attach(observer2);

subject.someBusinessLogic();
subject.someBusinessLogic();

subject.detach(observer2);

subject.someBusinessLogic();

Output

Subject: Attached an observer.
Subject: Attached an observer.

Subject: I'm doing something important.
Subject: My state has just changed to: 6
Subject: Notifying observers...
ConcreteObserverB: Reacted to the event.

Subject: I'm doing something important.
Subject: My state has just changed to: 1
Subject: Notifying observers...
ConcreteObserverA: Reacted to the event.
Subject: Detached an observer.

Subject: I'm doing something important.
Subject: My state has just changed to: 5
Subject: Notifying observers...

Also Known As

Event-Subscriber, Listener, Pub/Sub

Real-World Example

An event bus for a React application where components subscribe to domain events (e.g., cart updated, user logged in) without direct coupling.

type EventHandler<T = unknown> = (data: T) => void;

class EventBus {
    private listeners = new Map<string, Set<EventHandler>>();

    on<T>(event: string, handler: EventHandler<T>) {
        if (!this.listeners.has(event)) {
            this.listeners.set(event, new Set());
        }
        this.listeners.get(event)!.add(handler as EventHandler);
        return () => this.off(event, handler);
    }

    off<T>(event: string, handler: EventHandler<T>) {
        this.listeners.get(event)?.delete(handler as EventHandler);
    }

    emit<T>(event: string, data: T) {
        this.listeners.get(event)?.forEach((handler) => handler(data));
    }
}

const bus = new EventBus();
const unsubscribe = bus.on<{ itemId: string }>("cart:updated", (data) => {
    console.log(`Cart updated: ${data.itemId}`);
});
bus.emit("cart:updated", { itemId: "abc-123" });
unsubscribe();

When NOT to Use

  • When there's only one subscriber — direct function calls are simpler
  • When event chains become hard to trace and debug ("event spaghetti")
  • When synchronous, direct communication between two known objects is clearer
  • In React, prefer lifting state up or using Context over custom event buses for most UI state

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 warning9mo4 checks · Risk SAFE
  • Gen Agent Trust Hub9mo

    This skill is a comprehensive documentation library for clean code principles and design patterns in TypeScript and React. It contains no executable code, network operations, or security risks.

  • Socket9mo

    No alerts

  • Snyk9mo

    Risk: LOW · No issues

  • Runlayer9mo

    1/25 files flagged

Signed by skilld at ca83de4. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 9 months ago

README badge

README badge for grahamcrackers/skills/clean-code-principles