Skip to content

Conversation

@jongwooo
Copy link
Contributor

@jongwooo jongwooo commented Dec 12, 2022

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

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, actions/setup-node#639

In util.ts:

AS-IS

export function isCacheFeatureAvailable(): boolean {
  if (!cache.isFeatureAvailable()) {
    if (isGhes()) {
      throw new Error(
        'Caching 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(
        'Caching is only supported on GHES version >= 3.5...'
    );
  }

  core.warning('The runner was not able to contact...');
  return false;
}

Check list:

  • Mark if documentation changes are required.
  • Mark if tests were added or updated to cover the changes.

Signed-off-by: jongwooo <jongwooo.han@gmail.com>
@jongwooo jongwooo changed the title refactor: Use early return pattern refactor: Use early return pattern to avoid nested conditions Dec 12, 2022
@panticmilos
Copy link
Contributor

hi @jongwooo, thank you for the PR. We will take a look at it

@jongwooo jongwooo requested review from Xraduu and panticmilos and removed request for Xraduu and panticmilos December 15, 2022 14:22
Signed-off-by: jongwooo <jongwooo.han@gmail.com>
@dmitry-shibanov dmitry-shibanov merged commit 545223a into actions:main Dec 16, 2022
@jongwooo jongwooo deleted the refactor/use-early-return-pattern branch December 16, 2022 14:13
yeikel pushed a commit to yeikel/setup-java that referenced this pull request Feb 12, 2023
fniephaus pushed a commit to graalvm/setup-java that referenced this pull request Jun 20, 2023
tdfacer pushed a commit to ifit/setup-java that referenced this pull request Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants