Heat can make Li-Ion batteries explode. Or restore their capacity, say Chinese boffins
Researchers at China’s Ningbo Institute of Materials Technology and Engineering have found a way to restore the energy density of old Lithium-Ion batteries by heating them to over 150°C.…
Royal chef reveals Queen Elizabeth II's 'less traditional' supper she ate every Good Friday
Demi Moore, 62, named as one of TIME's 100 Most Influential People after career resurgence and Oscar nod as she joins Serena Williams and Ed Sheeran on the star-studded list
Jack Whitehall's sister, Molly Wilkinson, reveals which man in the Whitehall family is secretly funnier than him on the latest celebrity episode of the Mail's 'The Apple & The Tree' podcast
CodeSOD: Static State
Today's Anonymous submitter was reviewing some C++ code, and saw this perfectly reasonable looking pattern.
class SomeClass { public: void setField(int val); int getField(); }Now, we can talk about how overuse of getters and setters is itself an antipattern (especially if they're trivial- you've just made a public variable with extra steps), but it's not wrong and there are certainly good reasons to be cautious with encapsulation. That said, because this is C++, that getField should really be declared int getField() const- appropriate for any method which doesn't cause a mutation to a class instance.
Or should it? Let's look at the implementation.
void SomeClass::setField(int val) { setGetField(true, val); } void SomeClass::getField() { return setGetField(false); }Wait, what? Why are we passing a boolean to a method called setGet. Why is there a method called setGet? They didn't go and make a method that both sets and gets, and decide which they're doing based on a boolean flag, did they?
int SomeClass::setGetField(bool set, int val) { static int s_val = 0; if (set) { s_val = val; } return s_val; }Oh, good, they didn't just make a function that maybe sets or gets based on a boolean flag. They also made the state within that function a static field. And yes, function level statics are not scoped to an instance, so this is shared across all instances of the class. So it's not encapsulated at all, and we've blundered back into Singletons again, somehow.
Our anonymous submitter had two reactions. Upon seeing this the first time, they wondered: "WTF? This must be some kind of joke. I'm being pranked."
But then they saw the pattern again. And again. After seeing it fifty times, they wondered: "WTF? Who hired these developers? And can that hiring manager be fired? Out of a cannon? Into the sun?"
[Advertisement] Picking up NuGet is easy. Getting good at it takes time. Download our guide to learn the best practice of NuGet for the Enterprise.How Bob Geldof's daughters have all found love with creative men who enjoy low-key lifestyles - after growing up in the limelight and coping with a series of tragic family deaths
Margot Robbie brings her newborn son home to Australia for the first time as she enjoys the holidays with her mother Sarie Kessler
Revulsion at Katy Perry over her 'ultimate insult' to NASA astronauts stranded in space for 9 months
Inside the funeral Prince Philip designed for himself - from a split-second reunion between William and Harry to a bespoke Land Rover
How a turned-off Prince and a disastrous wedding night doomed the Stuart dynasty: New Mail podcast shares the salacious stories behind the worst Royal weddings in history
King Charles issues special Easter message ahead of holiday festivities
Lauren Sanchez's ugly wardrobe malfunction that exposed the desperate truth of what lies beneath, revealed by JANE TIPPETT
Female Olympic rower in privacy war with Henley neighbour over their plan to build a gym studio in the garden
Exact dates Dartford Crossing set to close soon
Eerie maritime mystery as ghost ship washes up on Florida beach
Tesco, Asda, Sainsbury’s, Lidl, Aldi and more Easter bank holiday weekend 2025 opening times
Entitled CEO slammed for 'appalling' photo gloating over economy passengers from first class lounge
Essex cycling routes set to be improved with £1.4 investment
Astronomers Detect a Possible Signature of Life on a Distant Planet
Read more of this story at Slashdot.