Skip to content

Paranoia with null object #33

@shadowspawn

Description

@shadowspawn

I'm on a roll with a paranoia series... Another interesting question that I don't have relevant experience to base a strong opinion on.

@ljharb asked an interesting question in #26:

I hope result.flags is a null object?

Short version: no, should we do that?


Long version

The current answer is no, initialised with

const result = {
    flags: {},
    values: {},
    positionals: []
  };

I like the purity of a null object, but it has a slightly alarming behaviour in logging for users unaware of the behaviour:

 % node -e 'a=Object.create(null); a.foo="bar"; console.log(a)'
[Object: null prototype] { foo: 'bar' }

The documentation covers at some lengths the complications:

which can lead to some support questions:

I like the idea of using a null object in theory. With zero-config parsing in parseArg there is arguably more reason to consider null prototype. However, I don't have any practical experience with working with null objects. I am interested in comments from people with experience, and of how new users cope.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions