Should I explicitly write the public keyword or omit it when visibility isn't relevant in a...

Discussão em 'Angular' iniciado por JS-JeevaSaravanan, Abril 18, 2025.

  1. In a TypeScript project (Angular / NestJS), I’m working on a class where all methods are intended to be accessible by other parts of the app. There are no private or protected members in this file—everything is effectively public.

    Some developers explicitly declare public for every method:

    public funcName(): void {
    // ...
    }


    Others prefer to rely on the default (public) visibility and omit it:

    funcName(): void {
    // ...
    }

    My reasoning:

    • Since there’s nothing in this file that deals with visibility, adding public creates a misleading emphasis—making others think access level matters here.
    • It can cause developers to pause and wonder: “Why is this public?” or “Is something else private?”
    • Omitting public in such cases avoids unnecessary visual noise and mental overhead.
    • It also keeps things cleaner and more consistent with the rest of the codebase, which tends to only use access modifiers when needed.
    Question:


    From a readability and consistency perspective, which approach is preferred in the TypeScript/JavaScript community?

    1. Explicitly write public for clarity, even when everything is already public?
    2. Omit public when visibility isn’t relevant, to keep the code simple and intention-focused?

    Would love to hear how others approach this in real-world projects.

    Continue reading...

Compartilhe esta Página