Jennifer Garner's teen Fin, 16, channels dad Ben Affleck's iconic style in rare outing
Priceless gems stolen by 'highly organised gang' in audacious Louvre heist could be broken down and lost for ever
SpaceX Launches 10,000th Starlink Satellite
Read more of this story at Slashdot.
Owner of up-market delicatessen goes to war with her neighbours after they complained about her wealthy customers landing their helicopters outside her store
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.
Here are the seasonal train changes and station closures in Essex this Christmas
Julia Roberts just stepped out in Princess Beatrice's go-to shirt - here's exactly how she styled it
Security guard behind Holly Willoughby rape and murder plot hopes to appeal sentence
Decade-long battle with driving school was 'hell' for trainee instructor
AWS admits more bits of its cloud broke as it recovered from DynamoDB debacle
Amazon Web Services has revealed that its efforts to recover from the massive mess at its US-EAST-1 region caused other services to fail.…
New A414 speed cameras to be installed following fatal crashes
More air miles in the name of Net Zero! Keir Starmer WILL attend COP climate summit in Brazil - his 40th foreign trip in just 16 months as PM
Police hunting for missing Berlin schoolgirl who vanished six years ago aged 15 'will search grandparents' home'
Hollywood hairstylist's family slams Charles Manson killer who slaughtered Sharon Tate for playing the victim
This Harry Potter child actor from Essex has a whopping $50million net worth
Controversial plan to build 550 homes near 'famous' Essex fossil rejected
This Harry Potter child actor from Essex has a whopping $50million net worth
Furious Kenny Loggins demands Trump REMOVE 'unauthorized' Top Gun hit Danger Zone from wild AI clip
Alibaba reveals 82 percent GPU resource savings – but this is no DeepSeek moment
Chinese tech giant Alibaba has published a paper detailing scheduling tech it has used to achieve impressive utilization improvements across the GPU fleet it uses to power inferencing workloads – which is nice, but not a breakthrough that will worry AI investors.…