Comment On The Percent Conversion

"Lucky me," writes Joe from the Submit-To-WTF Visual Studio Add-In, "I just inherited a home-grown system information application." [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Re: The Percent Conversion

2012-02-02 09:02 • by Frist (unregistered)
Frist ...

My brain hurts

Re: The Percent Conversion

2012-02-02 09:02 • by JakeyC (unregistered)
If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "frist" Then
Return "frist"

Re: The Percent Conversion

2012-02-02 09:03 • by saltire (unregistered)
My first frist!

Re: The Percent Conversion

2012-02-02 09:03 • by ifStatementer (unregistered)
Must be nice to be paid by LoC

Re: The Percent Conversion

2012-02-02 09:04 • by renewest
Mathematical calculations drain the battery too much.

Re: The Percent Conversion

2012-02-02 09:08 • by Cad Delworth
Hm. It appears mathematics, or even plain arithmetic, was not one of the original developer's strengths? How do people like this deal with everyday arithmetic problems like, say, checking their change?

"That'll be $3.78, sir."

"Thanks."
[Hands over $5 bill, receives $1.22 in change]

"Umm..."
[stares blankly at bill and coins]

"Is there a problem, sir?"

"Um ... I don't know ..."

Re: The Percent Conversion

2012-02-02 09:09 • by Ben Jammin (unregistered)
Yeah, this could be drastically improved with a switch statement.

Re: The Percent Conversion

2012-02-02 09:10 • by DonaldK (unregistered)
373553 in reply to 373550
renewest:
Mathematical calculations drain the battery too much.


Quite. Storage for big binaries is not an issue these days, and heavy mathematical calculations (such as multiplying by 100 and rounding / trimming) cause high CPU usage, which warms up our planet. We should do our part against global warming...

Re: The Percent Conversion

2012-02-02 09:15 • by Anon (unregistered)
Does SystemInformation.PowerStatus.BatteryLifePercent.ToString always return 2 d.p? The documentation for BatteryLifePercent doesn't say either way.

Re: The Percent Conversion

2012-02-02 09:16 • by Anon (unregistered)

Re: The Percent Conversion

2012-02-02 09:23 • by RichP
Fail. The strings should be loaded from an XML file.

Re: The Percent Conversion

2012-02-02 09:23 • by snoofle
373558 in reply to 373552
Ben Jammin:
Yeah, this could be drastically improved with a switch statement.
Not enterprisey enough; what if they need the values in different bases? Needs XML:

<?xml version="1.0" encoding="UTF-8"?>
<doc>
<Fractions>
<value>
<reading>1.0"</reading>
<percentDecimal>100.0%</percentDecimal>
<percentOctal>144.0%</percentOctal>
<percentHex>64.0%</percentHex>
<percentBinary>1100100.0%</percentBinary>
</value>
<value>
<reading>.999"</reading>
<percentDecimal>99.9%</percentDecimal>
...
</value>
...
<value>
<reading>0.01</reading>
<percentDecimal>1.0%</percentDecimal>
...
</value>
...
<value>
<reading>0.001</reading>
<percentDecimal>0.1%</percentDecimal>
...
</value>
<value>
<reading>0</reading>
<percentDecimal>Your battery has no power - you can't see this</percentDecimal>
...
</value>

</Fractions>
</doc>


[Edit] RichP - you just beat me to it

Re: The Percent Conversion

2012-02-02 09:30 • by Nagesh (unregistered)
Maths by decimel is not being hard. Invented by Indian in fact at Hyderabad Unaversity:



Re: The Percent Conversion

2012-02-02 09:32 • by @Deprecated
373560 in reply to 373553
DonaldK:
renewest:
Mathematical calculations drain the battery too much.


Quite. Storage for big binaries is not an issue these days, and heavy mathematical calculations (such as multiplying by 100 and rounding / trimming) cause high CPU usage, which warms up our planet. We should do our part against global warming...


The interesting thing is that as the battery charge gets lower, this function will consume more and more battery power.

Say, there's a bug for you:
What if the BatteryLifePercent went up (charging) right in the middle of the (up to) 99 calls to ToString?
(You'd get NA)

What if you're running on a desktop? You'll always get "NA", the worst case for this code. But hey, you're plugged in, so who cares?

The planet cares! :-(

Re: The Percent Conversion

2012-02-02 09:32 • by Yaos
This is what we in the business call "paid by the line".

Re: The Percent Conversion

2012-02-02 09:40 • by Vincent (unregistered)
Incredibly enough, this code lives in its own method, which is properly named. This WTF would be much better if it was found buried inside a mess of templates or in the Paint() method.

Re: The Percent Conversion

2012-02-02 09:44 • by Decius (unregistered)
373564 in reply to 373555
Anon:
Does SystemInformation.PowerStatus.BatteryLifePercent.ToString always return 2 d.p? The documentation for BatteryLifePercent doesn't say either way.

Hopefully not- the code assumes that it returns 1 place in the cases of 10%, 20%, 90%, and presumably the other decades. Properly, the code preserves that lack of precision by displaying 90% instead of 90.% or 9.0*10^1, or any other method which would indicate the precision of the measurement.

Re: The Percent Conversion

2012-02-02 09:45 • by DCRoss (unregistered)
That's terrible code. What happens if the method for converting from decimal to percent changes?

It doesn't even try to deal with adding the leap-percent that comes around every few years, or to adjust for local Percent Savings Time.

Re: The Percent Conversion

2012-02-02 09:49 • by Anon (unregistered)
373566 in reply to 373564
Decius:
Anon:
Does SystemInformation.PowerStatus.BatteryLifePercent.ToString always return 2 d.p? The documentation for BatteryLifePercent doesn't say either way.

Hopefully not- the code assumes that it returns 1 place in the cases of 10%, 20%, 90%, and presumably the other decades. Properly, the code preserves that lack of precision by displaying 90% instead of 90.% or 9.0*10^1, or any other method which would indicate the precision of the measurement.


Oooh. Good point. I didn't even notice that part. So does this code even work then?

Re: The Percent Conversion

2012-02-02 10:00 • by Mr. Blutarsky (unregistered)
What if the battery power is 0.0?

Re: The Percent Conversion

2012-02-02 10:07 • by Myth (unregistered)
373568 in reply to 373567
Mr. Blutarsky:
What if the battery power is 0.0?


Then how is the code going to run?

Re: The Percent Conversion

2012-02-02 10:10 • by slamers
Maybe we need to start forcing programmers to have a license to be able to operate - just like we do for many other professions. This person would definintely have to have their license suspended for at least a year!

Re: The Percent Conversion

2012-02-02 10:11 • by The Great Lobachevsky (unregistered)
373570 in reply to 373551
When I was a real little kid, about 3, and I would go to the store with my family, I totally didn't get the concept of giving change.

In this situation, my little child brain believed that the $5 bill was now only worth $3.78, and wondered how they kept track of it only being worth that instead of the amout printed on it. :)

I have a feeling the original developer still thinks the way I did when I was 3.

captcha: mara - Mara preserve us! (insert lame "arrow in the knee" joke here)

Re: The Percent Conversion

2012-02-02 10:18 • by Shutterbug (unregistered)
373571 in reply to 373552
Ben Jammin:
Yeah, this could be drastically improved with a switch statement.


ROFLMAO!

capture: pecus - just pecus

Re: The Percent Conversion

2012-02-02 10:19 • by BentFranklin (unregistered)
The funniest thing is that it runs the method SystemInformation.PowerStatus.BatteryLifePercent.ToString so many times that it could actually drain the battery by 1% in the middle of the function and give the wrong answer. Or, if the battery is charging, it could skip the right answer and fall all the way down to "NA".

Re: The Percent Conversion

2012-02-02 10:22 • by Shutterbug (unregistered)
373573 in reply to 373566
Anon:
Decius:
Anon:
Does SystemInformation.PowerStatus.BatteryLifePercent.ToString always return 2 d.p? The documentation for BatteryLifePercent doesn't say either way.

Hopefully not- the code assumes that it returns 1 place in the cases of 10%, 20%, 90%, and presumably the other decades. Properly, the code preserves that lack of precision by displaying 90% instead of 90.% or 9.0*10^1, or any other method which would indicate the precision of the measurement.


Oooh. Good point. I didn't even notice that part. So does this code even work then?


Only about 80.3% of the time ...

Re: The Percent Conversion

2012-02-02 10:22 • by C-Octothorpe
373574 in reply to 373551
Cad Delworth:
Hm. It appears mathematics, or even plain arithmetic, was not one of the original developer's strengths? How do people like this deal with everyday arithmetic problems like, say, checking their change?

"That'll be $3.78, sir."

"Thanks."
[Hands over $5 bill, receives $1.22 in change]

"Umm..."
[stares blankly at bill and coins]

"Is there a problem, sir?"

"Um ... I don't know ..."
Oh yeah, this is the guy who is always in front of you when you're in a rush when the penny counting granny has the day off...

Re: The Percent Conversion

2012-02-02 10:22 • by suscipit (unregistered)
so, 0.991 is NA...

Re: The Percent Conversion

2012-02-02 10:25 • by venio (unregistered)
You should look at the StackOverflow. There's gazillion questions that resemble amateurs such as this one. A ton of self-called programmers out there don't want to use more brains than it's needed to simply copy-paste code.

Re: The Percent Conversion

2012-02-02 10:26 • by Lone Marauder (unregistered)
Dear Sweet Lord. I'm not a coder, just a lowly network geek... and *I* wouldn't write a script like that.

Re: The Percent Conversion

2012-02-02 10:29 • by java.lang.Chris;
373578 in reply to 373568
Myth:
Mr. Blutarsky:
What if the battery power is 0.0?


Then how is the code going to run?


When the device is attached to a charger and turned on after the battery's gone flat?

Re: The Percent Conversion

2012-02-02 10:37 • by PedanticCurmudgeon
373579 in reply to 373569
slamers:
Maybe we need to start forcing programmers to have a license to be able to operate - just like we do for many other professions. This person would definintely have to have their license suspended for at least a year!
That depends on who is appointed to the licensing board. If the licensing board were full of bad programmers, we might have our licenses suspended for using design patterns.

Re: The Percent Conversion

2012-02-02 10:39 • by Stabbitha (unregistered)
Obligatory "The real WTF is VB..."

Now back to getting paid to write VB ... sigh ...

Re: The Percent Conversion

2012-02-02 10:50 • by Paul Neumann (unregistered)
I had to look carefully for the WTF in this code. Then it came to me all at once:
Public ReadOnly Property BatteryPercent() As String


I'm not certain what everyone is going about. A switch will only be more efficient if the entire jump table can fit into standard memory. Once you've gone beyond 6.4 cases, embedded if's are the only way to go.

Re: The Percent Conversion

2012-02-02 10:53 • by Matt (unregistered)
373584 in reply to 373575
suscipit:
so, 0.991 is NA...

Don't worry, there is a similar function for rounding numbers like that to the hundredths.

Re: The Percent Conversion

2012-02-02 10:57 • by Anketam
373585 in reply to 373568
Myth:
Mr. Blutarsky:
What if the battery power is 0.0?


Then how is the code going to run?
You can be running off of power cable while having a defective battery which is dead. This happen to my laptop from 8 years ago, so it showed 0.0% charged (or if it was running this code N/A) and Unable to charge.

Re: The Percent Conversion

2012-02-02 11:00 • by Craig (unregistered)
It baffles me how people like this continue to get jobs as developers.

Re: The Percent Conversion

2012-02-02 11:03 • by C-Octothorpe
373587 in reply to 373583
Paul Neumann:
I had to look carefully for the WTF in this code. Then it came to me all at once:
Public ReadOnly Property BatteryPercent() As String


I'm not certain what everyone is going about. A switch will only be more efficient if the entire jump table can fit into standard memory. Once you've gone beyond 6.4 cases, embedded if's are the only way to go.
I agree 100%. Now the only think left for you to do is to throw yourself into a wood chipper. I'm sure you would make great compost.

Re: The Percent Conversion

2012-02-02 11:05 • by jdw (unregistered)
What awful code. I mean, he should have assigned SystemInformation.PowerStatus.BatteryLifePercent.ToString to a variable first. That would have saved him a bunch of typing. Here are some comparison snippets:

        ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.2" Then

Return "20%"
ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.19" Then
Return "19%"
ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.18" Then
Return "18%"
ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.17" Then
Return "17%"
ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.16" Then
Return "16%"


	ElseIf BatteryLifePercentString = "0.2" Then

Return "20%"
ElseIf BatteryLifePercentString = "0.19" Then
Return "19%"
ElseIf BatteryLifePercentString = "0.18" Then
Return "18%"
ElseIf BatteryLifePercentString = "0.17" Then
Return "17%"
ElseIf BatteryLifePercentString = "0.16" Then
Return "16%"

Re: The Percent Conversion

2012-02-02 11:07 • by Rob Reilink (unregistered)
TRWTF is of course that the API apparently returns a fraction of the battery life (0-1) instead of a percent as the member name 'BatteryLiferPercent' suggests (see also this MS bug report)

Re: The Percent Conversion

2012-02-02 11:10 • by Rob (unregistered)
The best part is how this becomes more and more power-inefficient as the user's power goes lower! What a great feature!

Re: The Percent Conversion

2012-02-02 11:21 • by frits
Hey this is VB, why don't they just mulitply the string by 100 and concatanate the "%" character?

Re: The Percent Conversion

2012-02-02 11:24 • by Warren (unregistered)
I get it, the problem is he didn't give that the return type is a string, let alone add a version that returned an integer instead.

Re: The Percent Conversion

2012-02-02 11:25 • by NPSF3000 (unregistered)
http://pastebin.com/jDa45U9h

If he keeps this up, he'll get a bonus for the extra LOC!

Re: The Percent Conversion

2012-02-02 11:34 • by Crisw (unregistered)
373595 in reply to 373570
The Great Lobachevsky:
When I was a real little kid, about 3, and I would go to the store with my family, I totally didn't get the concept of giving change.

In this situation, my little child brain believed that the $5 bill was now only worth $3.78, and wondered how they kept track of it only being worth that instead of the amout printed on it. :)

I have a feeling the original developer still thinks the way I did when I was 3.

captcha: mara - Mara preserve us! (insert lame "arrow in the knee" joke here)


I used to be a comedian like you, until a mob beat me senseless for overusing a forced meme.

Re: The Percent Conversion

2012-02-02 11:35 • by ioiooiioio (unregistered)
Shouldn't it also be an "==" instead of '='? Or is that just in java?

Re: The Percent Conversion

2012-02-02 11:35 • by Schmitter (unregistered)
I am 100% sure there is a better way to do that.

Re: The Percent Conversion

2012-02-02 11:40 • by C-Octothorpe
373598 in reply to 373597
Schmitter:
I am
(Users.TryGet(typeof(Schmitter)).LevelOfSureness * 100).ToString() + "%"
sure there is a better way to do that.
FTFY

Re: The Percent Conversion

2012-02-02 11:47 • by Lockwood
373599 in reply to 373598
C-Octothorpe:
Schmitter:
I am
(Users.TryGet(typeof(Schmitter)).LevelOfSureness * 100).ToString() + "%"
sure there is a better way to do that.
FTFY


Edit: I derped a little there.

Re: The Percent Conversion

2012-02-02 11:56 • by erikal
373600 in reply to 373546
Frist:
Frist ...

My brain hurts


*cloth on his head* I'm going to operate!!!!
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Add Comment