Skip to main content

CodeSOD: Static State

2 months 3 weeks ago

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.
Remy Porter

Astronomers Detect a Possible Signature of Life on a Distant Planet

2 months 3 weeks ago
Astronomers have detected what may be the strongest evidence yet of extraterrestrial life on K2-18b, a massive exoplanet orbiting a star 120 light-years from Earth. The research team, led by Cambridge astronomer Nikku Madhusudhan, published their findings today in the Astrophysical Journal Letters. Using the James Webb Space Telescope, researchers found significant concentrations of dimethyl sulfide and dimethyl disulfide in K2-18b's atmosphere. On Earth, these sulfur compounds are exclusively produced by living organisms, particularly marine algae. "It is in no one's interest to claim prematurely that we have detected life," said Madhusudhan, though he described the findings as "a revolutionary moment" and "the first time humanity has seen potential biosignatures on a habitable planet." The team detected the signals during two separate observations, with the second showing an even stronger signature. Their analysis suggests K2-18b may be a "Hycean" planet -- covered with warm oceans and wrapped in a hydrogen-rich atmosphere -- with concentrations of dimethyl sulfide thousands of times higher than Earth levels. Other scientists remain cautious. Christopher Glein of the Southwest Research Institute suggested K2-18b could instead be "a massive hunk of rock with a magma ocean and a thick, scorching hydrogen atmosphere." Further observations with Webb and future NASA telescopes will be necessary to confirm whether K2-18b is truly habitable or inhabited, though planned budget cuts may impact follow-up research. Further reading: Water Found On a Potentially Life-Friendly Alien Planet (2019).

Read more of this story at Slashdot.

msmash