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: make logic more concise with optional chaining and nullish coalescing operators #12277

Merged
merged 21 commits into from
Feb 5, 2021

Conversation

flemjame-at-amazon
Copy link
Contributor

This is an unnecessary refactor that shrinks logic across CDK, mostly by replacing phrases like:

const var = props.property !== undefined ? props.property : 'defaultValue';
const var2 = props.property === undefined ? 'defaultValue' : props.property;
const var3 = props.property ? props.property.subproperty : 'defaultValue';

with:

const var = props.property ?? 'defaultValue';
const var2 = props.property ?? 'defaultValue';
const var3 = props.property?.subproperty ?? 'defaultValue';

It's a slightly more concise way of writing the logic which takes advantage of TS native operators. I basically did a regex search and replaced obvious logic.

There are a couple modules where the logic itself was unnecessarily redundant, which I replaced. For example:

const port = props.port !== undefined ? props.port : (protocol === undefined ? 80 :
(protocol === elbv2.ApplicationProtocol.HTTPS ? 443 : 80));

Can be replaced by:

const port = props.port ?? (protocol === elbv2.ApplicationProtocol.HTTPS ? 443 : 80);

Because the check for protocol === undefined setting the default of port 80 can be rolled into the check for protocol === elbv2.ApplicationProtocol.HTTPS


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@gitpod-io
Copy link

gitpod-io bot commented Dec 30, 2020

@skinny85
Copy link
Contributor

@flemjame-at-amazon this is a very impressive PR, thanks for the contribution!

Can I ask: did you do the changes manually, or did you employee some automated way to perform these refactorings?

@flemjame-at-amazon
Copy link
Contributor Author

@flemjame-at-amazon this is a very impressive PR, thanks for the contribution!

Can I ask: did you do the changes manually, or did you employee some automated way to perform these refactorings?

I did it manually because parsing code with code isn't my forte, but it was pretty simple to find them all and replace. The main grep was:

grep -r "undefined ? " --include="*.ts" packages/\@aws-cdk/aws-ec2 | grep -v generated | grep -v '{' | less

For each module, then do a build on that module to check that there was no subtle logic differences.

@flemjame-at-amazon
Copy link
Contributor Author

@skinny85 can this be merged? I ask because it covers so many files that it's likely that someone will commit a change which creates a merge conflict

@skinny85
Copy link
Contributor

skinny85 commented Feb 4, 2021

@nija-at can you weigh in here? Does this increase the chance of merge conflicts with v2-main?

@nija-at
Copy link
Contributor

nija-at commented Feb 5, 2021

Thanks for checking in. This shouldn't cause merge conflicts.

Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for your hard work on this @flemjame-at-amazon !

@mergify
Copy link
Contributor

mergify bot commented Feb 5, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: dc4b6ef
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented Feb 5, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 98ab292 into aws:master Feb 5, 2021
@flemjame-at-amazon flemjame-at-amazon deleted the make-concise-with-ts-operators branch February 6, 2021 00:34
TLadd pushed a commit to TLadd/aws-cdk that referenced this pull request Feb 9, 2021
…coalescing operators (aws#12277)

This is an unnecessary refactor that shrinks logic across CDK, mostly by replacing phrases like:

```ts
const var = props.property !== undefined ? props.property : 'defaultValue';
const var2 = props.property === undefined ? 'defaultValue' : props.property;
const var3 = props.property ? props.property.subproperty : 'defaultValue';
```

with:

```ts
const var = props.property ?? 'defaultValue';
const var2 = props.property ?? 'defaultValue';
const var3 = props.property?.subproperty ?? 'defaultValue';
```

It's a slightly more concise way of writing the logic which takes advantage of TS native operators. I basically did a regex search and replaced obvious logic.

There are a couple modules where the logic itself was unnecessarily redundant, which I replaced. For example:
https://github.com/aws/aws-cdk/blob/31132caec4565735d2ccc32b43f078d634a5ca43/packages/%40aws-cdk/aws-ecs/lib/base/base-service.ts#L217-L218

Can be replaced by:
```ts
const port = props.port ?? (protocol === elbv2.ApplicationProtocol.HTTPS ? 443 : 80);
```

Because the check for ```protocol === undefined``` setting the default of port 80 can be rolled into the check for ```protocol === elbv2.ApplicationProtocol.HTTPS```

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
NovakGu pushed a commit to NovakGu/aws-cdk that referenced this pull request Feb 18, 2021
…coalescing operators (aws#12277)

This is an unnecessary refactor that shrinks logic across CDK, mostly by replacing phrases like:

```ts
const var = props.property !== undefined ? props.property : 'defaultValue';
const var2 = props.property === undefined ? 'defaultValue' : props.property;
const var3 = props.property ? props.property.subproperty : 'defaultValue';
```

with:

```ts
const var = props.property ?? 'defaultValue';
const var2 = props.property ?? 'defaultValue';
const var3 = props.property?.subproperty ?? 'defaultValue';
```

It's a slightly more concise way of writing the logic which takes advantage of TS native operators. I basically did a regex search and replaced obvious logic.

There are a couple modules where the logic itself was unnecessarily redundant, which I replaced. For example:
https://github.com/aws/aws-cdk/blob/31132caec4565735d2ccc32b43f078d634a5ca43/packages/%40aws-cdk/aws-ecs/lib/base/base-service.ts#L217-L218

Can be replaced by:
```ts
const port = props.port ?? (protocol === elbv2.ApplicationProtocol.HTTPS ? 443 : 80);
```

Because the check for ```protocol === undefined``` setting the default of port 80 can be rolled into the check for ```protocol === elbv2.ApplicationProtocol.HTTPS```

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
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.

4 participants