Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jongwooo opened this issue Dec 4, 2022 · 2 comments
Closed

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

jongwooo opened this issue Dec 4, 2022 · 2 comments
Labels
feature request New feature or request to improve the current logic

Comments

@jongwooo
Copy link
Contributor

jongwooo commented Dec 4, 2022

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.

See actions/cache#1012

In cache-utils.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;
}
@jongwooo jongwooo added feature request New feature or request to improve the current logic needs triage labels Dec 4, 2022
@dmitry-shibanov
Copy link
Contributor

Hello @jongwooo. Thank you for your report. We'll take a look on it.

@dsame
Copy link
Contributor

dsame commented Dec 12, 2022

Issue is resolved with #639

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request to improve the current logic
Projects
None yet
Development

No branches or pull requests

3 participants