Skip to content
New issue

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

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

Already on GitHub? # to your account

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

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

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

jongwooo opened this issue Dec 4, 2022 · 2 comments
Assignees
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()) {
      throw new Error(
        'Cache action is only supported on GHES version >= 3.5...'
      );
    } else {
      core.warning(
        'The runner was not able to contact...'
      );
    }

    return false;
  }

  return true;
}

TO-BE

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

  if (isGhes()) {
    throw new Error(
      'Cache action is only supported on GHES version >= 3.5...'
    );
  }

  core.warning(
    'The runner was not able to contact...'
  );
  return false;
}
@jongwooo jongwooo added feature request New feature or request to improve the current logic needs triage labels Dec 4, 2022
@e-korolevskii
Copy link
Contributor

Hi @jongwooo

We will investigate this issue and come back to you.

@e-korolevskii
Copy link
Contributor

Hi @jongwooo!

Thanks for your contribution!
The changes will be available in the next release. I am closing this issue for now!

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

No branches or pull requests

2 participants