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-template-method.md

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

Template Method

Template Method defines an algorithm skeleton, letting subclasses override specific steps. Learn more

Category: Behavioral

TypeScript Example

/**
 * The Abstract Class defines a template method that contains a skeleton of some
 * algorithm, composed of calls to (usually) abstract primitive operations.
 *
 * Concrete subclasses should implement these operations, but leave the template
 * method itself intact.
 */
abstract class AbstractClass {
    /**
     * The template method defines the skeleton of an algorithm.
     */
    public templateMethod(): void {
        this.baseOperation1();
        this.requiredOperations1();
        this.baseOperation2();
        this.hook1();
        this.requiredOperation2();
        this.baseOperation3();
        this.hook2();
    }

    /**
     * These operations already have implementations.
     */
    protected baseOperation1(): void {
        console.log("AbstractClass says: I am doing the bulk of the work");
    }

    protected baseOperation2(): void {
        console.log("AbstractClass says: But I let subclasses override some operations");
    }

    protected baseOperation3(): void {
        console.log("AbstractClass says: But I am doing the bulk of the work anyway");
    }

    /**
     * These operations have to be implemented in subclasses.
     */
    protected abstract requiredOperations1(): void;

    protected abstract requiredOperation2(): void;

    /**
     * These are "hooks." Subclasses may override them, but it's not mandatory
     * since the hooks already have default (but empty) implementation. Hooks
     * provide additional extension points in some crucial places of the
     * algorithm.
     */
    protected hook1(): void {}

    protected hook2(): void {}
}

/**
 * Concrete classes have to implement all abstract operations of the base class.
 * They can also override some operations with a default implementation.
 */
class ConcreteClass1 extends AbstractClass {
    protected requiredOperations1(): void {
        console.log("ConcreteClass1 says: Implemented Operation1");
    }

    protected requiredOperation2(): void {
        console.log("ConcreteClass1 says: Implemented Operation2");
    }
}

/**
 * Usually, concrete classes override only a fraction of base class' operations.
 */
class ConcreteClass2 extends AbstractClass {
    protected requiredOperations1(): void {
        console.log("ConcreteClass2 says: Implemented Operation1");
    }

    protected requiredOperation2(): void {
        console.log("ConcreteClass2 says: Implemented Operation2");
    }

    protected hook1(): void {
        console.log("ConcreteClass2 says: Overridden Hook1");
    }
}

/**
 * The client code calls the template method to execute the algorithm. Client
 * code does not have to know the concrete class of an object it works with, as
 * long as it works with objects through the interface of their base class.
 */
function clientCode(abstractClass: AbstractClass) {
    // ...
    abstractClass.templateMethod();
    // ...
}

console.log("Same client code can work with different subclasses:");
clientCode(new ConcreteClass1());
console.log("");

console.log("Same client code can work with different subclasses:");
clientCode(new ConcreteClass2());

Output

Same client code can work with different subclasses:
AbstractClass says: I am doing the bulk of the work
ConcreteClass1 says: Implemented Operation1
AbstractClass says: But I let subclasses override some operations
ConcreteClass1 says: Implemented Operation2
AbstractClass says: But I am doing the bulk of the work anyway

Same client code can work with different subclasses:
AbstractClass says: I am doing the bulk of the work
ConcreteClass2 says: Implemented Operation1
AbstractClass says: But I let subclasses override some operations
ConcreteClass2 says: Overridden Hook1
ConcreteClass2 says: Implemented Operation2
AbstractClass says: But I am doing the bulk of the work anyway

Also Known As

None widely used

Real-World Example

A data export system where the overall flow (fetch → transform → format → output) is fixed, but subclasses customize the format step (CSV, JSON, XML).

abstract class DataExporter {
    export() {
        const data = this.fetchData();
        const transformed = this.transform(data);
        const formatted = this.format(transformed);
        this.output(formatted);
    }

    private fetchData() {
        return [{ name: "Alice" }, { name: "Bob" }];
    }
    private transform(data: any[]) {
        return data.filter(Boolean);
    }

    protected abstract format(data: any[]): string;

    private output(content: string) {
        console.log(content);
    }
}

class CsvExporter extends DataExporter {
    protected format(data: any[]): string {
        return ["name", ...data.map((d) => d.name)].join("\n");
    }
}

class JsonExporter extends DataExporter {
    protected format(data: any[]): string {
        return JSON.stringify(data, null, 2);
    }
}

new CsvExporter().export();
new JsonExporter().export();

When NOT to Use

  • When the algorithm doesn't have a fixed skeleton — if steps vary widely, use Strategy instead
  • When inheritance hierarchies become deep and rigid
  • In functional codebases, passing functions as parameters (strategy/callback) is often simpler
  • When subclasses need to change the order of steps, not just individual step implementations

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