New Oct 8, 2024

no-unused-binary-expressions: From code review nit to ecosystem improvements

Libraries, Frameworks, etc. All from ESLint Blog View no-unused-binary-expressions: From code review nit to ecosystem improvements on eslint.org

Four years ago, while doing a code review at work, I was surprised that Flow had not warned about an unnecessary null check. Last month, TypeScript 5.6 released with validation rules that disallows useless nullish and truthy checks which uncovered nearly 100 existing bugs in the top 800 TypeScript repos on GitHub.

The two events are connected because that moment in code review four years ago led me to write the no-constant-binary-expressions rule which catches a wide variety of bugs. Examples include:

// Expecting empty objects to be falsy
const foo = { ...config } || {};

// Confusing precedence of !
const foo1 = !x == null;

// Confusing ?? or || precedence
const foo2 = x === y ?? true;

The no-constant-binary-expression rule, in turn, helped inspire the newly added TypeScript validations.

Given the protracted timeline and the many intermediate steps, I thought it would be interesting to reflect on how we got here. How did this observation in one code review snowball into a significant positive impact to developers? And how could the snowball continue to grow?

To answer these questions, it helps to review the timeline of events.

Timeline

Reflections

There were a number of key factors that contributed to this small observation in code review helping spur a meaningful ecosystem wide improvement:

What’s Next

It’s taken four years for the ripple of Brad’s initial observation on that internal post to reach this point, but I think the ideas here have the potential to resonate even further:

Conclusion

This effort spanned multiple years and multiple organizations.

As a motivated individual within Meta, a for-profit company, I was able to spark a conversation which spawned an idea. I was then able to leverage ESLint to validate that idea internally. Once validated, the ESLint project, a not-for-profit organization, brought the idea to a large audience via it’s broad adoption, recommended rule set, and blog. With the idea documented and validated at scale, engineers at Microsoft, another for-profit company, were able to bring the validations to an even larger audience via their open source project TypeScript.

Each organization played a different, but key, role according to its strengths and position. I believe a common thread throughout this process, which enabled this scale of collaboration, was the active socialization of ideas. From asking questions during code review, to asking questions of local experts, to sharing ideas in public blog posts and tweets, each expanding circle of socialization improved the idea and ultimately helped bring it to the broad audience it has reached today.


Thanks to Brad Zacher for his initial key observation and ongoing encouragement, to Nicholas C. Zakas and Milos Djermanovic for significant contributions to the rule during code review, and to Ryan Cavanaugh for bringing these same types of validation to the TypeScript ecosystem.

Scroll to top