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

refactor: Use early return pattern in cache-utils.ts #643

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jongwooo
Copy link
Contributor

@jongwooo jongwooo commented Dec 14, 2022

Signed-off-by: jongwooo jongwooo.han@gmail.com

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 cache-utils.ts:

AS-IS

export const getPackageManagerInfo = async (packageManager: string) => {
  if (packageManager === 'npm') {
    return supportedPackageManagers.npm;
  } else if (packageManager === 'pnpm') {
    return supportedPackageManagers.pnpm;
  } else if (packageManager === 'yarn') {
    const yarnVersion = await getPackageManagerVersion('yarn', '--version');

    core.debug(`Consumed yarn version is ${yarnVersion}`);

    if (yarnVersion.startsWith('1.')) {
      return supportedPackageManagers.yarn1;
    } else {
      return supportedPackageManagers.yarn2;
    }
  } else {
    return null;
  }
};

TO-BE

export const getPackageManagerInfo = async (packageManager: string) => {
  if (packageManager === 'npm') {
    return supportedPackageManagers.npm;
  }

  if (packageManager === 'pnpm') {
    return supportedPackageManagers.pnpm;
  }

  if (packageManager === 'yarn') {
    const yarnVersion = await getPackageManagerVersion('yarn', '--version');
    core.debug(`Consumed yarn version is ${yarnVersion}`);

    if (yarnVersion.startsWith('1.')) {
      return supportedPackageManagers.yarn1;
    }

    return supportedPackageManagers.yarn2;
  }
  
  return null;
};

Related issue

#636

Check list

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

@jongwooo jongwooo requested a review from a team December 14, 2022 18:26
@jongwooo jongwooo changed the title refactor: Use early return pattern to avoid nested conditions refactor: Use early return pattern in cache-utils.ts Dec 14, 2022
src/cache-utils.ts Outdated Show resolved Hide resolved
Signed-off-by: jongwooo <jongwooo.han@gmail.com>
@jongwooo jongwooo force-pushed the refactor/use-early-return-pattern branch from 502a50d to b1daf0a Compare December 29, 2022 05:16
@jongwooo jongwooo requested a review from a team as a code owner December 29, 2022 05:16
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.

1 participant