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

Revise for consistency and readability #188

Closed
wants to merge 9 commits into from

Conversation

elidoran
Copy link
Contributor

This PR does some code cleanup. It makes some things consistent:

  1. text which may be colored is declared in variables in one place
  2. text coloring happens in one place
  3. string template avoids having logic and only specifies where variable text is included

Changes:

  1. moves remaining parts of notify message into variables
  2. checks once if we should color text instead of multiple ternary expressions
  3. colors text in one block (if statement)
  4. reformats notify message templates by removing ternary expressions
  5. reuses useUnicode in later code by moving it up a scope

I think this makes the code easier to read and maintain.

@elidoran elidoran requested a review from a team as a code owner April 15, 2019 17:02
@isaacs isaacs force-pushed the release-next branch 2 times, most recently from 896149d to 31718e7 Compare June 29, 2019 21:55
@isaacs
Copy link
Contributor

isaacs commented Jul 10, 2019

Thanks, but I try to avoid patches like this that move stuff around and don't really add much value otherwise, unless it addresses a more serious code architecture problem. Especially, replacing const statements with let statements is something we generally frown upon. (A let has higher cognitive burden, because you have to read the code looking for places where it might change.)

@isaacs isaacs closed this Jul 10, 2019
@elidoran
Copy link
Contributor Author

I hear ya: "code cleanup unwanted here". I'll avoid submitting similar PR's.

You're correct, it doesn't add a feature, fix a bug, or correct a performance issue. It only addresses readability/maintainability.

As for saying there's no value in cleaning up, compare the cognitive burden of these two code blocks:

// version 1:
notifier.notify({
  message: `New ${type} version of ${pkg.name} available! ${
    useColor ? color.red(old) : old
  } ${useUnicode ? '→' : '->'} ${
    useColor ? color.green(latest) : latest
  }\n` +
  `${
    useColor ? color.yellow('Changelog:') : 'Changelog:'
  } ${
    useColor ? color.cyan(changelog) : changelog
  }\n` +
  `Run ${
    useColor
      ? color.green(`npm install -g ${pkg.name}`)
      : `npm i -g ${pkg.name}`
  } to update!`
})
// version 2:
notifier.notify({
  message:
    `New ${type} version of ${pkg.name} available! ${old} ${arrow} ${latest}\n` +
    `${changelogLabel} ${changelog}\n` +
    `Run ${installCommand} to update!`
})

Is avoiding switching variables old and latest from const to let so important it's reason enough to retain the messy version 1? We have the let keyword for a reason. I agree const is preferable everywhere it's possible, and, sometimes let is used when it should be a const instead, like several lines up in that file.

I expanded on what the original author already did. They separated it into 3 blocks:

  1. declare some values and text needed
  2. conditionally change the type to a color version
  3. build template string

But, some color logic made its way into section three where static strings are repeated.

So, I moved the color logic and strings in section three to section two so all colorizing is done in one spot, no strings are repeated, and that made the template string much more readable/maintainable. It seems worth it to me; that's why I offered it up as a PR.

We all write code which needs some cleanup we don't always have time to do ourselves.

Happy coding.

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.

2 participants