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-mediator.md

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

Mediator

Mediator reduces direct dependencies by routing communication through a central object. Learn more

Category: Behavioral

TypeScript Example

/**
 * The Mediator interface declares a method used by components to notify the
 * mediator about various events. The Mediator may react to these events and
 * pass the execution to other components.
 */
interface Mediator {
    notify(sender: object, event: string): void;
}

/**
 * Concrete Mediators implement cooperative behavior by coordinating several
 * components.
 */
class ConcreteMediator implements Mediator {
    private component1: Component1;

    private component2: Component2;

    constructor(c1: Component1, c2: Component2) {
        this.component1 = c1;
        this.component1.setMediator(this);
        this.component2 = c2;
        this.component2.setMediator(this);
    }

    public notify(sender: object, event: string): void {
        if (event === "A") {
            console.log("Mediator reacts on A and triggers following operations:");
            this.component2.doC();
        }

        if (event === "D") {
            console.log("Mediator reacts on D and triggers following operations:");
            this.component1.doB();
            this.component2.doC();
        }
    }
}

/**
 * The Base Component provides the basic functionality of storing a mediator's
 * instance inside component objects.
 */
class BaseComponent {
    protected mediator: Mediator;

    constructor(mediator?: Mediator) {
        this.mediator = mediator!;
    }

    public setMediator(mediator: Mediator): void {
        this.mediator = mediator;
    }
}

/**
 * Concrete Components implement various functionality. They don't depend on
 * other components. They also don't depend on any concrete mediator classes.
 */
class Component1 extends BaseComponent {
    public doA(): void {
        console.log("Component 1 does A.");
        this.mediator.notify(this, "A");
    }

    public doB(): void {
        console.log("Component 1 does B.");
        this.mediator.notify(this, "B");
    }
}

class Component2 extends BaseComponent {
    public doC(): void {
        console.log("Component 2 does C.");
        this.mediator.notify(this, "C");
    }

    public doD(): void {
        console.log("Component 2 does D.");
        this.mediator.notify(this, "D");
    }
}

/**
 * The client code.
 */
const c1 = new Component1();
const c2 = new Component2();
const mediator = new ConcreteMediator(c1, c2);

console.log("Client triggers operation A.");
c1.doA();

console.log("");
console.log("Client triggers operation D.");
c2.doD();

Output

Client triggers operation A.
Component 1 does A.
Mediator reacts on A and triggers following operations:
Component 2 does C.

Client triggers operation D.
Component 2 does D.
Mediator reacts on D and triggers following operations:
Component 1 does B.
Component 2 does C.

Also Known As

Controller, Intermediary

Real-World Example

A form with interdependent fields — changing the country field updates the state/province dropdown, currency display, and phone format. The mediator coordinates these updates so fields don't reference each other directly.

class FormMediator {
    private country: CountryField;
    private state: StateField;
    private currency: CurrencyDisplay;

    register(country: CountryField, state: StateField, currency: CurrencyDisplay) {
        this.country = country;
        this.state = state;
        this.currency = currency;
    }

    notify(sender: string, event: string) {
        if (sender === "country" && event === "change") {
            const selected = this.country.getValue();
            this.state.updateOptions(selected);
            this.currency.setCurrency(selected === "US" ? "USD" : "EUR");
        }
    }
}

When NOT to Use

  • When only two components communicate — direct coupling is simpler
  • When the mediator becomes a "God object" that knows too much about every component
  • In React, lifting state up or using Context often achieves the same decoupling more naturally
  • When the mediator itself becomes hard to maintain due to too many notification types

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