refactor: Use early return pattern to avoid nested conditions by jongwooo · Pull Request #1013 · actions/cache

Signed-off-by: jongwooo jongwooo.han@gmail.com
Issue #1012

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;
}

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (add or update README or docs)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.