Feed aggregator
US Investigates Waymo Robotaxis Over Safety Around School Buses
Read more of this story at Slashdot.
ISP Deceived Customers About Fiber Internet, German Court Finds
Read more of this story at Slashdot.
JetBrains Survey Declares PHP Declining, Then Says It Isn't
Read more of this story at Slashdot.
TikTok's New Policies Remove Promise To Notify Users Before Government Data Disclosure
Read more of this story at Slashdot.
Apple's Planned Foldable iPad With 18-inch Screen Hits Development Snags
Read more of this story at Slashdot.
KDE Plasma 6.5 Released
Read more of this story at Slashdot.
Amazon's DNS Problem Knocked Out Half the Web, Likely Costing Billions
Read more of this story at Slashdot.
France and Spain Call on EU To Uphold 2035 Combustion Engine Ban
Read more of this story at Slashdot.
OpenAI Debuts AI-Powered Browser With Memory and Agent Features
Read more of this story at Slashdot.
Apple Attacks EU Crackdown in Digital Law's Biggest Court Test
Read more of this story at Slashdot.
London Became a Global Hub for Phone Theft. Now We Know Why.
Read more of this story at Slashdot.
US Narrows Who Pays $100,000 H-1B Visa Fee
Read more of this story at Slashdot.
Japanese Convenience Stores Are Hiring Robots Run By Workers in the Philippines
Read more of this story at Slashdot.
Amazon Plans To Avoid Hiring 600,000 Workers Through Automation by 2033, Leaked Documents Show
Read more of this story at Slashdot.
Lloyds Banking Group Claims Microsoft Copilot Saves Staff 46 Minutes a Day
Read more of this story at Slashdot.
Alibaba Cloud Says It Cut Nvidia AI GPU Use By 82% With New Pooling System
Read more of this story at Slashdot.
SpaceX Launches 10,000th Starlink Satellite
Read more of this story at Slashdot.
CodeSOD: A Percentage of Refactoring
Joseph was doing a refactoring effort, merging some duplicated functions into one, cleaning up unused Java code that really should have been deleted ages ago, and so on. But buried in that pile of code that needed cleaning up, Joseph found this little bit of code, to validate that an input was a percentage.
@Override public Integer validatePercent(final String perc, final int currentPerc){ char[] percProc= perc.toCharArray(); char[] newPerc = new char[perc.length()]; int percent=0; int y=0; if(percProc.length>4){ return -1; } for(int x=0;x<percProc.length;x++){ if(Character.isDigit(percProc[x])){ newPerc[y]=percProc[x]; y++; } } if(y==0){ return -1; } String strPerc=(new String(newPerc)); strPerc=strPerc.trim(); if(strPerc.length()!=0){ percent=Integer.parseInt(strPerc); if(percent<0){ return -1; }else if(percent>100){ return -1; }else if(Integer.parseInt(strPerc)==currentPerc){ return -1; }else{ return Integer.parseInt(strPerc); } }else{ return-1; } }This validation function takes a string and an integer as an input, and immediately copies the string into an array, and makes a bonus array that's empty to start.
We reject strings longer than 4 characters. Then, we iterate over our input array and check each character; if that character is a digit, we copy it into the newPerc array, otherwise we… don't. If we copied at least one character this way, we continue- otherwise we reject the input.
Which right off the bat, this means that we accept 5, .05 and .0A5.
We take our newPerc array and turn it back into a string, trimming off any whitespace (which I'm fairly certain whitespace isn't a digit, last I checked, so there's nothing to trim).
If the string is greater than 0 characters, we parse it into an integer. If the result is less than zero, we reject it. Fun fact, isDigit also doesn't consider - a digit, so there's no chance we have a negative number here. If it's greater than 100 we reject it. If, when we parse it into an integer a second time, it's equal to the currentPerc input parameter, we also reject it. Otherwise, we return the result of parsing the string into an integer a third time.
So this isn't truly a validate function. It's a parse function. A strange one that doesn't work the way any sane person would want. And most annoying, at least in Java land, is that it handles errors by returning a -1, letting the caller check the return value and decide how to proceed, instead of throwing an exception.
Also, why reject the input if it equals the current value? I'd say that I'll puzzle over that, but the reality is that I won't. It's a stupid choice that I'd rather just not think more about.
[Advertisement] Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.Mystery Object From 'Space' Strikes United Airlines Flight Over Utah
Read more of this story at Slashdot.
$62 SanDisk Memory Card Found Intact At Titan Wreck Site
Read more of this story at Slashdot.
