• Frist (unregistered)

    Frist ...

    My brain hurts

  • JakeyC (unregistered)

    If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "frist" Then Return "frist"

  • saltire (unregistered)

    My first frist!

  • ifStatementer (unregistered)

    Must be nice to be paid by LoC

  • (cs)

    Mathematical calculations drain the battery too much.

  • (cs)

    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 ..."

  • Ben Jammin (unregistered)

    Yeah, this could be drastically improved with a switch statement.

  • DonaldK (unregistered) in reply to renewest
    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...

  • Anon (unregistered)

    Does SystemInformation.PowerStatus.BatteryLifePercent.ToString always return 2 d.p? The documentation for BatteryLifePercent doesn't say either way.

  • Anon (unregistered)
  • (cs)

    Fail. The strings should be loaded from an XML file.

  • (cs) in reply to Ben Jammin
    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

  • Nagesh (unregistered)

    Maths by decimel is not being hard. Invented by Indian in fact at Hyderabad Unaversity:

    [image]
  • (cs) in reply to DonaldK
    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! :-(

  • (cs)

    This is what we in the business call "paid by the line".

  • 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.

  • Decius (unregistered) in reply to Anon
    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.
  • 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.

  • Anon (unregistered) in reply to Decius
    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?

  • Mr. Blutarsky (unregistered)

    What if the battery power is 0.0?

  • Myth (unregistered) in reply to Mr. Blutarsky
    Mr. Blutarsky:
    What if the battery power is 0.0?

    Then how is the code going to run?

  • (cs)

    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!

  • The Great Lobachevsky (unregistered) in reply to Cad Delworth

    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)

  • Shutterbug (unregistered) in reply to Ben Jammin
    Ben Jammin:
    Yeah, this could be drastically improved with a switch statement.

    ROFLMAO!

    capture: pecus - just pecus

  • 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".

  • Shutterbug (unregistered) in reply to Anon
    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 ...

  • (cs) in reply to Cad Delworth
    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...
  • suscipit (unregistered)

    so, 0.991 is NA...

  • 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.

  • 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.

  • (cs) in reply to Myth
    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?

  • (cs) in reply to slamers
    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.
  • Stabbitha (unregistered)

    Obligatory "The real WTF is VB..."

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

  • 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.

  • Matt (unregistered) in reply to suscipit
    suscipit:
    so, 0.991 is NA...
    Don't worry, there is a similar function for rounding numbers like that to the hundredths.
  • (cs) in reply to Myth
    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.

  • Craig (unregistered)

    It baffles me how people like this continue to get jobs as developers.

  • (cs) in reply to Paul Neumann
    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.

  • 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%"
  • 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)

  • 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!

  • (cs)

    Hey this is VB, why don't they just mulitply the string by 100 and concatanate the "%" character?

  • 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.

  • NPSF3000 (unregistered)

    http://pastebin.com/jDa45U9h

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

  • Crisw (unregistered) in reply to The Great Lobachevsky
    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.

  • ioiooiioio (unregistered)

    Shouldn't it also be an "==" instead of '='? Or is that just in java?

  • Schmitter (unregistered)

    I am 100% sure there is a better way to do that.

  • (cs) in reply to Schmitter
    Schmitter:
    I am
    (Users.TryGet(typeof(Schmitter)).LevelOfSureness * 100).ToString() + "%"
    sure there is a better way to do that.
    FTFY
  • (cs) in reply to C-Octothorpe
    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.

  • (cs) in reply to Frist
    Frist:
    Frist ...

    My brain hurts

    cloth on his head I'm going to operate!!!!

Leave a comment on “The Percent Conversion”

Log In or post as a guest

Replying to comment #:

« Return to Article