Skip to main content

Coded Smorgasbord: High Strung

1 month 1 week ago

Most languages these days have some variation of "is string null or empty" as a convenience function. Certainly, C#, the language we're looking at today does. Let's look at a few example of how this can go wrong, from different developers.

We start with an example from Jason, which is useless, but not a true WTF:

/// <summary> /// Does the given string contain any characters? /// </summary> /// <param name="strToCheck">String to check</param> /// <returns> /// true - String contains some characters. /// false - String is null or empty. /// </returns> public static bool StringValid(string strToCheck) { if ((strToCheck == null) || (strToCheck == string.Empty)) return false; return true; }

Obviously, a better solution here would be to simply return the boolean expression instead of using a conditional, but equally obvious, the even better solution would be to use the built-in. But as implementations go, this doesn't completely lose the plot. It's bad, it shouldn't exist, but it's barely a WTF. How can we make this worse?

Well, Derek sends us an example line, which is scattered through the codebase.

if (Port==null || "".Equals(Port)) { /* do stuff */}

Yes, it's frequently done as a one-liner, like this, with the do stuff jammed all together. And yes, the variable is frequently different- it's likely the developer responsible saved this bit of code as a snippet so they could easily drop it in anywhere. And they dropped it in everywhere. Any place a string got touched in the code, this pattern reared its head.

I especially like the "".Equals call, which is certainly valid, but inverted from how most people would think about doing the check. It echos Python's string join function, which is invoked on the join character (and not the string being joined), which makes me wonder if that's where this developer started out?

I'll never know.

Finally, let's poke at one from Malfist. We jump over to Java for this one. Malfist saw a function called checkNull and foolishly assumed that it returned a boolean if a string was null.

public static final String checkNull(String str, String defaultStr) { if (str == null) return defaultStr ; else return str.trim() ; }

No, it's not actually a check. It's a coalesce function. Okay, misleading names aside, what is wrong with it? Well, for my money, the fact that the non-null input string gets trimmed, but the default string does not. With the bonus points that this does nothing to verify that the default string isn't null, which means this could easily still propagate null reference exceptions in unexpected places.

I've said it before, and I'll say it again: strings were a mistake. We should just abolish them. No more text, everybody, we're done.

.comment { border: none; } [Advertisement] Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.
Remy Porter

World's Oceans Fail Key Health Check As Acidity Crosses Critical Threshold For Marine Life

1 month 1 week ago
An anonymous reader quotes a report from The Guardian: The world's oceans have failed a key planetary health check for the first time, primarily due to the burning of fossil fuels, a report has shown. In its latest annual assessment, the Potsdam Institute for Climate Impact Research said ocean acidity had crossed a critical threshold for marine life. This makes it the seventh of nine planetary boundaries to be transgressed, prompting scientists to call for a renewed global effort to curb fossil fuels, deforestation and other human-driven pressures that are tilting the Earth out of a habitable equilibrium. The report, which follows earlier warnings about ocean acidity, comes at a time of recordbreaking ocean heat and mass coral bleaching. Oceans cover 71% of the Earth's surface and play an essential role as a climate stabilizer. The new report calls them an "unsung guardian of planetary health", but says their vital functions are threatened. The 2025 Planetary Health Check noted that since the start of the industrial era, oceans' surface pH has fallen by about 0.1 units, a 30-40% increase in acidity, pushing marine ecosystems beyond safe limits. Cold-water corals, tropical coral reefs and Arctic marine life are especially at risk. This is primarily due to the human-caused climate crisis. When carbon dioxide from oil, coal and gas burning enters the sea, it forms carbonic acid. This reduces the availability of calcium carbonate, which many marine organisms depend upon to grow coral, shells or skeletons. Near the bottom of the food chain, this directly affects species like oysters, molluscs and clams. Indirectly, it harms salmon, whales and other sea life that eat smaller organisms. Ultimately, this is a risk for human food security and coastal economies. Scientists are concerned that it could also weaken the ocean's role as the planet's most important heat absorber and its capacity to draw down 25-30% of the carbon dioxide in the atmosphere. Marine life plays an important role in this process, acting as a "biotic bump" to sequester carbon in the depths. In the report, all of the other six breached boundaries -- climate change, biosphere integrity, land system change, freshwater use, biogeochemical flows, and novel entities -- showed a worsening trend. But the authors said the addition of the only solely ocean-centerd category was a alarming development because of its scale and importance.

Read more of this story at Slashdot.

BeauHD