Skip to content

refactor: Use early return pattern to avoid nested conditions - #302

Merged
dmitry-shibanov merged 2 commits into
actions:mainfrom
jongwooo:refactor/use-early-return-pattern
Dec 16, 2022
Merged

dmitry-shibanov merged 2 commits into
actions:mainfrom
jongwooo:refactor/use-early-return-pattern

Conversation

@jongwooo

@jongwooo jongwooo commented Dec 12, 2022 •

Copy link
Copy Markdown
Contributor

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

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

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 requested a review from a team December 12, 2022 12:24
@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
Copy Markdown
Contributor

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

Comment thread src/cache-utils.ts Resolved
Comment thread __tests__/cache-utils.test.ts Resolved
Signed-off-by: jongwooo <jongwooo.han@gmail.com>
@dmitry-shibanov
dmitry-shibanov merged commit bb5ff97 into actions:main Dec 16, 2022
adilhusain-s pushed a commit to adilhusain-s/setup-go that referenced this pull request Feb 6, 2023
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.

6 participants