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

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

Singleton

Singleton ensures a class has only one instance with a global access point. The constructor is private; clients obtain the instance via a static getter. Learn more

Category: Creational

TypeScript Example

/**
 * The Singleton class defines an `instance` getter, that lets clients access
 * the unique singleton instance.
 */
class Singleton {
    static #instance: Singleton;

    /**
     * The Singleton's constructor should always be private to prevent direct
     * construction calls with the `new` operator.
     */
    private constructor() {}

    /**
     * The static getter that controls access to the singleton instance.
     *
     * This implementation allows you to extend the Singleton class while
     * keeping just one instance of each subclass around.
     */
    public static get instance(): Singleton {
        if (!Singleton.#instance) {
            Singleton.#instance = new Singleton();
        }

        return Singleton.#instance;
    }

    /**
     * Finally, any singleton can define some business logic, which can be
     * executed on its instance.
     */
    public someBusinessLogic() {
        // ...
    }
}

/**
 * The client code.
 */
function clientCode() {
    const s1 = Singleton.instance;
    const s2 = Singleton.instance;

    if (s1 === s2) {
        console.log("Singleton works, both variables contain the same instance.");
    } else {
        console.log("Singleton failed, variables contain different instances.");
    }
}

clientCode();

Output

Singleton works, both variables contain the same instance.

Also Known As

(no widely-used alternative names)

Real-World Example

A logger service or an API client instance that should be shared across the entire application.

class ApiClient {
    private static instance: ApiClient;
    private baseUrl: string;

    private constructor(baseUrl: string) {
        this.baseUrl = baseUrl;
    }

    static getInstance(baseUrl = "/api"): ApiClient {
        if (!ApiClient.instance) {
            ApiClient.instance = new ApiClient(baseUrl);
        }
        return ApiClient.instance;
    }

    async get<T>(path: string): Promise<T> {
        const res = await fetch(`${this.baseUrl}${path}`);
        return res.json();
    }
}

// Same instance everywhere
const api = ApiClient.getInstance();

When NOT to Use

  • Widely considered an anti-pattern — makes testing difficult (hard to mock/replace)
  • When you need multiple instances in tests or different configurations
  • In frontend apps, prefer module-scoped instances or dependency injection via Context/providers
  • Introduces hidden global state and tight coupling
  • When the "single instance" requirement can be satisfied by ES module scope (modules are singletons by default in JS)

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