CodeSOD: Is We Equal?
Testing for equality is hard. Equal references are certainly equal, but are equal values? What does it mean for two objects to equal" each other? It's especially hard in a language like JavaScript, which is friendly" about type conversions.
In JavaScript land, you're likely to favor a tool like lodash", which provides utility functions like isEqual.
Mohsin was poking around an old corner of their codebase, which hadn't been modified in some time. Waiting there was this helpful" function.
import _ from 'lodash';export function areEqual(prevProps, nextProps) { if (_.isEqual(prevProps, nextProps)) { return true; } return false;}
In this case, our unknown developer is the best kind of correct: grammatically correct. isEqual should rightly be called areEqual, since we're testing if two objects are equal" to each other.
Does that justify implementing a whole new method? Does it justify implementing it with an awkward construct where we use an if to determine if we should return true or false, instead of just, y'know, returning true or false.
isEqual already returns a boolean value, so you don't need that if: return _.isEqual(...) would be quite enough. Given that functions are data in JavaScript, we could even shorten that by export const areEqual = _.isEqual.
Or, we could just not do this at all.
[Advertisement] ProGet supports your applications, Docker containers, and third-party packages, allowing you to enforce quality standards across all components. Download and see how!