CodeSOD: Property Flippers
Kleyguerth was having a hard time tracking down a bug. A _hasPicked flag was "magically" toggling itself to on. It was a bug introduced in a recent commit, but the commit in question was thousands of lines, and had the helpful comment "Fixed some stuff during the tests".
In several places, the TypeScript code checks a property like so:
if (!this.checkAndPick) { // do stuff }Now, TypeScript, being a Microsoft language, allows properties to be just, well, properties, or it allows them to be functions with getters and setters.
You see where this is going. Once upon a time was a property that just checked another, private property, and returned its value, like so:
private get checkAndPick() { return this._hasPicked; }Sane, reasonable choice. I have questions about why a private getter exists, but I'm not here to pick nits.
As we progress, someone changed it to this:
private get checkAndPick() { return this._hasPicked || (this._hasPicked = true); }This forces the value to true, and returns true. This always returns true. I don't like it, because using a property accessor to mutate things is bad, but at least the property name is clear- checkAndPick tells us that an item is being picked. But what's the point of the check?
Still, this version worked as people expected it to. It took our fixer to take it to the next level:
private get checkAndPick() { return this._hasPicked || !(this._hasPicked = true); }This flips _hasPicked to true if it's not already true- but if it does, returs false. The badness of this code is rooted in the badness of the previous version, because a property should never be used this way. And while this made our fixer's tests turn green, it broke everything for everyone else.
Also: do not, do not use property accessors to mutate state. Only setters should mutate state, and even then, they should only set a field based on their input. Complicated logic does not belong in properties. Or, as this case shows, even simple logic doesn't, if that simple logic is also stupid.
Blockchain just became an utterly mainstream part of the global financial system
Blockchains are still synonymous with the wild world of cryptocurrencies, but on Monday, 30 banks and SWIFT – the world’s most important cross-border payment service – made them an utterly mainstream part of the global financial system.…
Business confidence crashes to record low as bosses demand 'credible plan' to kick-start floundering economy
The village that Roy built: Welcome to the mysterious community in rural Norfolk that has become a global phenomenon...after baffled TikTokers thought they'd 'walked into a cult'
They weathered vile abuse from US Ryder Cup fans... But now insiders reveal strains STILL exist between Rory McIlroy and the steely wife he came close to divorcing last year
The day Labour dragged politics into the gutter: Desperate Starmer accuses Farage of not liking Britain... triggering bitter war of words
LAURA CRAIK: Zendaya helps give the mini a leg up at the Louis Vuitton show in Paris
'I lost a stone a month on diet and it's nothing to do with jabs'
Duchess Sophie just stepped out in the ultimate neutral outfit for autumn - and her exact suit is still in stock
Relationship 'red flags' that DON'T spell disaster: TRACEY COX reveals whether arguments and lack of sex are truly a problem - and 5 issues you really should be worried about
Suffering from recurring UTIs? Leading nurse says it could be a warning sign of deadly kidney cancer
Taliban impose tele-ban and take Afghanistan offline
Afghanistan has dropped off the global internet.…