Skip to content

Suggestion: Use early return pattern to avoid nested conditions #1012

@jongwooo

Description

@jongwooo

Description

Return early is the way of writing functions or methods so that the expected positive result is returned at the end of the function and the rest of the code terminates the execution (by returning or throwing an exception) when conditions are not met.

In actionUtils.ts:

AS-IS

export function isCacheFeatureAvailable(): boolean {
    if (!cache.isFeatureAvailable()) {
        if (isGhes()) {
            logWarning(
                `Cache action is only supported on GHES version >= 3.5...`
            );
        } else {
            logWarning(
                "An internal error has occurred ..."
            );
        }
        return false;
    }

    return true;
}

TO-BE

export function isCacheFeatureAvailable(): boolean {
  if (cache.isFeatureAvailable()) {
    return true;
  }

  if (isGhes()) {
    logWarning(
      `Cache action is only supported on GHES version >= 3.5...`
    );
    return false;
  }

  logWarning(
    "An internal error has occurred ..."
  );
  return false;
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy