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

Use Optional Chaining (?.) #4

Merged
merged 2 commits into from
May 15, 2021

Conversation

p42-ai[bot]
Copy link

@p42-ai p42-ai bot commented May 15, 2021

The optional chaining operator returns the value of an object property when the object is available and undefined otherwise.
.? is similar to the standard . chaining operator, with an added check if the object is defined.

The optional chaining operator enables you to write concise and safe chains of connected objects when some of those objects can be null or undefined.
Before the introduction of optional chaining in ES2020, the && operator was often used to check if an object is available (obj && obj.value).

This refactoring simplifies existing checks to use the optional chaining pattern:

  • x && x.a becomes x?.a
  • x != null && x.a becomes x?.a
  • x !== null && x !== undefined && x.a becomes x?.a
  • x && x.a && x.a.b && x.a.b.c && x.a.b.c.d becomes x?.a?.b?.c?.d
  • etc.

Learn More: Optional Chaining (MDN)

The refactoring replaces falsy checks with nullish checks.

For example, when a && a.b is replaced with a?.b, it changes the execution for certain types, e.g. the empty string "" is falsy but not nullish.

However, in many cases these semantic changes will lead actually to more correct behavior.
For example, text && text.length will return the empty string, but not its length, whereas text?.length will return 0 for the empty string.

Learn more: Truthy (MDN), Falsy (MDN), Nullish (MDN)

This refactoring can affects the number of calls made to methods with side effects.

For example, the refactoring changes:

let a = x() != null && x().a;

into

let a = x()?.a;

If f(1) has a side effect, it would have been called one or two times before the refactoring, and once after the refactoring.
This means that the side effect would have been called a different number of times, potentially changing the behavior.

**The optional chaining operator returns the value of an object property when the object is available and `undefined` otherwise.**
`.?` is similar to the standard `.` chaining operator, with an added check if the object is defined.

The optional chaining operator enables you to **write concise and safe chains of connected objects** when some of those objects can be `null` or `undefined`.
Before the introduction of optional chaining in ES2020, the `&&` operator was often used to check if an object is available (`obj && obj.value`).

This refactoring simplifies existing checks to use the optional chaining pattern:

* `x && x.a` becomes `x?.a`
* `x != null && x.a` becomes `x?.a`
* `x !== null && x !== undefined && x.a` becomes `x?.a`
* `x && x.a && x.a.b && x.a.b.c && x.a.b.c.d` becomes `x?.a?.b?.c?.d`
* etc.

Learn More: [Optional Chaining (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining)

**The refactoring replaces falsy checks with nullish checks.**

For example, when `a && a.b` is replaced with `a?.b`, it changes the execution for certain types, e.g. the empty string `""` is falsy but not nullish.

However, in many cases these semantic changes will lead actually to more correct behavior.
For example, `text && text.length` will return the empty string, but not its length, whereas `text?.length` will return `0` for the empty string.

Learn more: [Truthy (MDN)](https://developer.mozilla.org/en-US/docs/Glossary/Truthy), [Falsy (MDN)](https://developer.mozilla.org/en-US/docs/Glossary/Falsy), [Nullish (MDN)](https://developer.mozilla.org/en-US/docs/Glossary/Nullish)

**This refactoring can affects the number of calls made to methods with side effects.**

For example, the refactoring changes:

```javascript
let a = x() != null && x().a;
```

into

```javascript
let a = x()?.a;
```

If `f(1)` has a side effect, it would have been called one or two times before the refactoring, and once after the refactoring.
This means that the side effect would have been called a different number of times, potentially changing the behavior.
@github-actions
Copy link

Thanks for opening a pull request!

If this is not a minor PR. Could you open an issue for this pull request on JIRA? https://issues.apache.org/jira/browse/ARROW

Opening JIRAs ahead of time contributes to the Openness of the Apache Arrow project.

Then could you also rename pull request title in the following format?

ARROW-${JIRA_ID}: [${COMPONENT}] ${SUMMARY}

or

MINOR: [${COMPONENT}] ${SUMMARY}

See also:

@domoritz domoritz changed the base branch from master to modern-js May 15, 2021 17:14
@domoritz domoritz merged commit fd81938 into modern-js May 15, 2021
@domoritz domoritz deleted the p42/optional_chaining/1621098613171 branch May 15, 2021 17:16
domoritz pushed a commit that referenced this pull request Jul 7, 2021
Before change:

```
Direct leak of 65536 byte(s) in 1 object(s) allocated from:
    #0 0x522f09 in
    #1 0x7f28ae5826f4 in
    #2 0x7f28ae57fa5d in
    #3 0x7f28ae58cb0f in
    #4 0x7f28ae58bda0 in
    ...
```

After change:
```
Direct leak of 65536 byte(s) in 1 object(s) allocated from:
    #0 0x522f09 in posix_memalign (/build/cpp/debug/arrow-dataset-file-csv-test+0x522f09)
    #1 0x7f28ae5826f4 in arrow::(anonymous namespace)::SystemAllocator::AllocateAligned(long, unsigned char**) /arrow/cpp/src/arrow/memory_pool.cc:213:24
    #2 0x7f28ae57fa5d in arrow::BaseMemoryPoolImpl<arrow::(anonymous namespace)::SystemAllocator>::Allocate(long, unsigned char**) /arrow/cpp/src/arrow/memory_pool.cc:405:5
    #3 0x7f28ae58cb0f in arrow::PoolBuffer::Reserve(long) /arrow/cpp/src/arrow/memory_pool.cc:717:9
    #4 0x7f28ae58bda0 in arrow::PoolBuffer::Resize(long, bool) /arrow/cpp/src/arrow/memory_pool.cc:741:7
    ...
```

Closes apache#10498 from westonpace/feature/ARROW-13027--c-fix-asan-stack-traces-in-ci

Authored-by: Weston Pace <weston.pace@gmail.com>
Signed-off-by: Antoine Pitrou <antoine@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant