Feed aggregator
Cyberattack Hits Blood-Donation Nonprofit OneBlood
Read more of this story at Slashdot.
Germany Says China Was Behind a 2021 Cyberattack on Government Agency
Read more of this story at Slashdot.
AWS Quietly Scales Back Some DevOps Services
Read more of this story at Slashdot.
Air New Zealand Is First Major Airline To Scrap 2030 Emissions Target
Read more of this story at Slashdot.
Microsoft is Removing Ads From Skype
Read more of this story at Slashdot.
Intel To Cut Thousands of Jobs To Reduce Costs
Read more of this story at Slashdot.
Why We Might Never Know the Truth About Ultra-Processed Foods
Read more of this story at Slashdot.
Delta CEO Says CrowdStrike-Microsoft Outage Cost the Airline $500 Million
Read more of this story at Slashdot.
The Future of Science Publishing
Read more of this story at Slashdot.
Brazil's Radical Plan To Tax Global Super-Rich To Tackle Climate Crisis
Read more of this story at Slashdot.
$5,000 Exoskeleton Pants Promise to Make You a Better Hiker
Read more of this story at Slashdot.
A Blood Test Accurately Diagnosed Alzheimer's 90% of the Time, Study Finds
Read more of this story at Slashdot.
CodeSOD: Yes, No, NO NO NO NO
Mike was doing work for a mobile services provider. He found this in their code:
private static YesNoType toYesNo(String isYes) { if (isYes != null) { if (isYes.equalsIgnoreCase("Y")) { return YesNoType.fromString("Yes"); } else { return YesNoType.fromString("No"); } } else { return YesNoType.fromString("No"); } } /** * @param isYes * @return */ private static YesNoType toYesNo(boolean isYes) { if (isYes) { return YesNoType.fromString("Yes"); } else { return YesNoType.fromString("No"); } } /** * @param isYes * @return */ private static String fromYesNo(YesNoType isYes) { if (isYes != null) { String resultStr = isYes.toString(); if (resultStr.equalsIgnoreCase("Yes")) { return ("Yes"); } else { return ("No"); } } else { return ("No"); } } /** * @param isYes * @return */ private static boolean isYesNo(YesNoType isYes) { boolean isBroadbandUser = false; if (isYes != null && isYes.toString().equalsIgnoreCase("Yes")) { isBroadbandUser = true; } return isBroadbandUser; }Look, I'm barely even interested in the functions here. They're all varying degrees of bad, sure, but I really want to focus on YesNoType. I've seen people reinvent all sorts of wheels, but to reinvent a boolean in Java of all places, that's novel. And like every reinvention, it looks like they reinvented it badly: it's a stringly typed boolean, at least in terms of how it's used here.
That's not to say that this code isn't full of other horrors, but YesNoType is definitely the root of all evil here.
But let's dig in, anyway, starting with toYesNo(String). If the input string is "Y", we store a "Yes" in a YesNoType. Otherwise, we store "No". This means we can set a YesNoType with a "Y", but when set, it always returns a "Yes", which means we're already in a space where we have inconsistent values.
Now, toYesNo(boolean) tells us that we can't construct a YesNoType directly off a boolean, which seems like a glaring mistake.
fromYesNo, on the other hand, does a wonderful check to see if the YesNoType matches (ignoring case) a "Yes", so that it can return a "Yes". It almost seems like we shouldn't need this at all if we had any sense, but we don't.
But the real capper is our isYesNo function, which converts our YesNoType into a boolean, aka the thing we should have been using the whole time. Because this code was clearly copy/pasted from somewhere less generic, we have a local variable called isBroadbandUser. A local variable we don't need, as we could just do this as a single return. Or, better yet, we could have just used a boolean the whole time.
At worst, we could have written a pair of functions bool fromString(String) and String fromBool(boolean) and called it a day. No new type. No weird string comparisons. A boolean is the simplest thing you can have, and to do it so wrong is an achievement.
[Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!A New White House Report Embraces Open-Source AI
Read more of this story at Slashdot.
Meta To Pay Record $1.4 Billion To Settle Texas Facial Recognition Suit
Read more of this story at Slashdot.
Dark Angels Ransomware Receives Record-Breaking $75 Million Ransom
Read more of this story at Slashdot.
Passkey Adoption Has Increased By 400 Percent In 2024
Read more of this story at Slashdot.
Perplexity AI Will Share Revenue With Publishers After Plagiarism Accusations
Read more of this story at Slashdot.
California DMV Puts 42 Million Car Titles On Blockchain To Fight Fraud
Read more of this story at Slashdot.
Meta's AI Safety System Defeated By the Space Bar
Read more of this story at Slashdot.