Skip to content

Move away from relying on JS truthiness #3538

Closed

Description

@iAmWillShepherd

See PR #3535 for more context.

I think we should be explicit in our conditional statements instead of relying on truthiness. This should limit gotchas such as the one referenced above.

let a: ReadonlyArray<number> | null = null
let s: string | undefined = undefined

if (a) { }
if (a != null) { } //preferred

a = [0, 1, 2, 3]

if (a.length) { }
if (a.length > 0) { } //preferred

Activity

  1. added
    tech-debt
    on Dec 7, 2017
  2. j-f1 commented on Dec 7, 2017

    @j-f1
    Contributor

    I’ll take a look at this.

    1️⃣ ❓:

    Should this throw?

    let x: boolean = myFunction()
    
    if (x) { }
    
  3. joshaber commented on Dec 7, 2017

    @joshaber
    Contributor

    👍

    if (a !== null) { } //preferred
    if (s !== undefined) { } //preferred
    

    I'd lump these two together and suggest that we prefer:

    if (s != null) { } //preferred
    

    It's rare that we care about the distinction between null and undefined, and I'd go further and say that caring is usually an anti-pattern.

  4. iAmWillShepherd commented on Dec 7, 2017

    @iAmWillShepherd
    ContributorAuthor

    The only reason I included undefined is that we are using optional types.

    Consider this:

    function foo(obj?: SomeObject) {
      let a = obj.someProperty   //obj can't be null here, but it can be undefined, which means this fails
    }
    

    In this case, we'd have to check for undefined. We've been getting around this issue by relying on the truthiness of undefined.

  5. iAmWillShepherd commented on Dec 7, 2017

    @iAmWillShepherd
    ContributorAuthor

    Should this throw?

    let x: boolean = myFunction()
    
    if (x) { }
    

    @j-f1 no it should not.

    The intent behind this issue is that we should be explicit instead of relying on truthiness. I think booleans are exempt from this since they are the primitives all other conditional expressions evaluate.

  6. joshaber commented on Dec 7, 2017

    @joshaber
    Contributor

    The only reason I included undefined is that we are using optional types.

    Consider this:

    function foo(obj?: SomeObject) {
      let a = obj.someProperty   //obj can't be null here, but it can be undefined, which means this fails
    }
    

    In this case, we'd have to check for undefined. We've been getting around this issue by relying on the truthiness of undefined.

    Hmm, I'm not sure I understand?

    != null coerces undefined so that both null != null and null != undefined are false. Or in other words, it generalizes across both null and undefined.

  7. iAmWillShepherd commented on Dec 7, 2017

    @iAmWillShepherd
    ContributorAuthor

    But null !== undefined return true.

    I prefer using === and !== as opposed to == and !=, but so long as we make sure we remember to use the loose equality operators, we should be fine.

  8. joshaber commented on Dec 8, 2017

    @joshaber
    Contributor

    I prefer using === and !== as opposed to == and !=

    Me too! To be clear, I'm only talking about the case of null. I think differentiating between null and undefined in the context of an if is making a distinction without a difference. What we're trying to check is whether the value is something or nothing, regardless of whether the particular form of nothing is null or undefined.

    http://adripofjavascript.com/blog/drips/equals-equals-null-in-javascript.html does a better job of explaining this than I think I am doing 😂

  9. j-f1 commented on Dec 8, 2017

    @j-f1
    Contributor

    Me too! To be clear, I'm only talking about the case of null.

    In fact, that’s already configured as part of the eqeqeq ESLint rule:

    desktop/.eslintrc.yml

    Lines 65 to 67 in f12ff8c

    eqeqeq:
    - error
    - smart

  10. iAmWillShepherd commented on Dec 8, 2017

    @iAmWillShepherd
    ContributorAuthor

    @joshaber that made sense, also thanks for the article. So I'm down for using == and != only when checking against null or undefined.

  11. iAmWillShepherd commented on Dec 8, 2017

    @iAmWillShepherd
    ContributorAuthor

    @j-f1 were you intending to tackle this work?

  12. j-f1 commented on Dec 8, 2017

    @j-f1
    Contributor

    were you intending to tackle this work?

    Yes 👍

  13. iAmWillShepherd commented on Dec 8, 2017

    @iAmWillShepherd
    ContributorAuthor

    Could you open a PR to track since I can't assign you to the issue?

  14. j-f1 commented on Dec 8, 2017

    @j-f1
    Contributor

    Could you open a PR to track?

    Yup. ⬆️

  15. j-f1 commented on Jan 8, 2018

    @j-f1
    Contributor

    From my PR above, these appear to be the requirements:

    • value is not declared (or inferred) to be a boolean
    • value is inside any number of &&, ||, or ! expressions and either:
      • the test of an if statement, or
      • the test of a ternary.
  16. iAmWillShepherd commented on Jan 9, 2018

    @iAmWillShepherd
    ContributorAuthor

    @j-f1 not sure I understand...

  17. j-f1 commented on Jan 9, 2018

    @j-f1
    Contributor

    @iAmWillShepherd Those are the requirements for a node to be reported by this rule.

  18. iAmWillShepherd commented on Jan 9, 2018

    @iAmWillShepherd
    ContributorAuthor

    Got it.

    value is inside any number &&, ||, or ! expressions and either:

    This threw me off because I read it as a value is in a number, &&, ||, or ! expression.

  19. j-f1 commented on Jan 9, 2018

    @j-f1
    Contributor

    Looks like I forgot an of when typing that part. Edited above to correct.

  20. iAmWillShepherd commented on May 25, 2018

    @iAmWillShepherd
    ContributorAuthor

    Closing this out since the team has started being explicit with null and undefined checks.

  21. locked as resolved and limited conversation to collaborators on Jul 24, 2018
  22. unlocked this conversation on Sep 27, 2018
  23. j-f1 commented on Dec 15, 2018

    @j-f1
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    tech-debt

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions