State-of-the art robots that could improve surgery for thousands of patients have been approved for use on the NHS
Good Housekeeping's decluttering expert warns against the mistake not to make while spring cleaning
Daddy of a mistake by GoDaddy took Zoom offline for about 90 minutes
A bad mistake by GoDaddy took Zoom offline for almost two hours on Wednesday afternoon, US time.…
Young motorcyclist dies after A120 crash between Braintree and Coggeshall
I'm buying a top floor flat - can I build a roof terrace without planning permission?
Over £275k of unwanted electric cars dumped at the roadside in Nottingham after US Tesla rival went bust
Tesla drivers burn through tyres faster than any others, reveals KwikFit
LG TVs' Integrated Ads Get More Personal With Tech That Analyzes Viewer Emotions
Read more of this story at Slashdot.
Why your pension could be dealt a body blow as Trump eyes tariffs for Britain's top drug firms
Could your wellness routine kill you? Paralysed for life, heart attacks and even DEATH...the horrifying dangers of trendy treatments revealed - as woman dies from cryotherapy session
Aussie influencers 'held at gunpoint' by US police who kicked them out of their 'illegal' Airbnb during Coachella
I live on cruise ships for FREE for half of the year, enjoying the same perks as passengers - but there is an annoying catch
The mummified remains Egypt does NOT want you to see: Shocking footage shows animals used to entertain tourists being abused and left to die among the rubbish behind the pyramids
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.