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

Why use undefined for leaf nodes rather than an empty list of children? #60

Closed
Ptival opened this issue Oct 17, 2016 · 1 comment
Closed

Comments

@Ptival
Copy link

Ptival commented Oct 17, 2016

It seems to be one of those JavaScriptisms that really infuriate me, but I will leave this as a suggestion.

The hierarchy method fills the children field with a list of children if any, and undefined otherwise.

It seems to me that returning an empty list would be much more elegant and practical. For instance I could do:

// using ES5+ operators
node.children.map(...)
node.children.reduce(...)

Is there any good reason for not returning an empty list?
Good thing TypeScript with strictNullChecks warned me...

@mbostock
Copy link
Member

It was a matter of preference. I preferred to use null to represent leaf nodes rather than an empty array, but certainly it would have been reasonable to use an empty array instead. I am unlikely to change the behavior now as it would not be backwards-compatible, although I suppose I could consider it for a future major version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants