• Whatever (unregistered)

    Do you think he wrote a program to automatically generate the code?

  • NPSF3000 (unregistered) in reply to Whatever
    Whatever:
    Do you think he wrote a program to automatically generate the code?

    I refer you to my earlier comment:

    http://thedailywtf.com/Comments/The-Percent-Conversion.aspx#373594

  • Tom (unregistered) in reply to Whatever
    Whatever:
    Do you think he wrote a program to automatically generate the code?
    That's not dynamic enough. First, check the length of the strength value, e.g. "0.99" is length 4. Then, generate the code for all possible values of length 4. Finally, eval the generated code!

    Solves the problem of "0.987", "0.9876"... all without wasting CPU cycles when the value is "0".

  • uns (unregistered) in reply to Schmitter
    Schmitter:
    I am 1 sure there is a better way to do that.
    FTFY
  • Hans (unregistered) in reply to Myth

    On Net Power? Laptops works well on net power with damaged batteries

  • Ben Jammin (unregistered)

    What is faster?

    (SystemInformation.PowerStatus.BatteryLifePercent * 100).ToString() + "%"
    - or -
    SystemInformation.PowerStatus.BatteryLifePercent.ToString("p")
    - or -
    String.Format("{0}%", SystemInformation.PowerStatus.BatteryLifePercent * 100)
  • OldPeter (unregistered)

    TRWTF is the bad performance caused by the linear orientation of all those IF's. It could be drastically improved if arranged in a binary scheme!

  • Ninja (unregistered) in reply to Tom

    This is why (Cr)Apple iPods only update their battery life at 90%, 80%, and 10%.

  • Whatever (unregistered) in reply to NPSF3000

    Can't believe you actually did it! Brilliant!

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

    What other data type does "NA" fit in? What other data type does "1%" fit in for that matter?

    No, the real WTF is that the results are not translated for the local culture and placement of % signs.

  • (cs) in reply to Ben Jammin
    Ben Jammin:
    What is faster?
    (SystemInformation.PowerStatus.BatteryLifePercent * 100).ToString() + "%"
    - or -
    SystemInformation.PowerStatus.BatteryLifePercent.ToString("p")
    - or -
    String.Format("{0}%", SystemInformation.PowerStatus.BatteryLifePercent * 100)

    None of those will produce the same results as the function you're replacing. So does it matter?

  • Ol' Bob (unregistered) in reply to snoofle
    snoofle:
    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

    ROFLMAO!!!!

  • Ninja (unregistered) in reply to wbrianwhite
    if(!(SystemInformation.PowerStatus.BatteryLifePercent % 10) Then
    Return SystemInformation.PowerStatus.BatteryLifePercent * 100).ToString() + "%"

    That would do it, wouldn't it?

  • (cs)

    Those who propose a mathematical approach seem to be missing that the OP code will ONLY display if the ToString is an exact match.

  • JP (unregistered)

    New Requirement: Report to the nearest 10th of a percent... oops.

  • (cs) in reply to TheCPUWizard
    TheCPUWizard:
    Those who propose a mathematical approach seem to be missing that the OP code will ONLY display if the ToString is an exact match.
    I love it: 100% - Perfect! N/A - Hmm, that's wierd 90% - Oh, there it is again. Good to know... N/A - What the fu... 80% - Allllright... N/A - Godammit! . . .
  • Somebody (unregistered) in reply to Frist

    Meine Augen

  • BAF (unregistered) in reply to Ben Jammin

    The first one.

  • Ben Jammin (unregistered) in reply to wbrianwhite
    wbrianwhite:
    Ben Jammin:
    What is faster?
    (SystemInformation.PowerStatus.BatteryLifePercent * 100).ToString() + "%"
    - or -
    SystemInformation.PowerStatus.BatteryLifePercent.ToString("p")
    - or -
    String.Format("{0}%", SystemInformation.PowerStatus.BatteryLifePercent * 100)

    None of those will produce the same results as the function you're replacing. So does it matter?

    I'm genuinely curious as to how it isn't. If you're talking about it missing an "NA", then please tell me the impact of that on my original question.

  • Argle (unregistered) in reply to Ben Jammin
    Ben Jammin:
    Yeah, this could be drastically improved with a switch statement.
    Seen it... though admittedly by a student... though still not excused. Spent an hour discussing C string library and giving examples of all the basics. Gave an assignment to read in lines of text and convert to all uppercase. Everyone else does the 5-10 line program but one thought the whole thing should be an exercise in making a very big switch statement based on the letters on the line.
  • Visual Basic (unregistered)

    FIRST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    TRWTF is Akismet. Really.

  • (cs) in reply to ioiooiioio
    ioiooiioio:
    Shouldn't it also be an "==" instead of '='? Or is that just in java?
    In BASIC, the =-sign is both for assignment and for comparison. In C-like languages (including Java), = assigns and == compares.
    Ben Jammin:
    wbrianwhite:
    None of those will produce the same results as the function you're replacing. So does it matter?

    I'm genuinely curious as to how it isn't. If you're talking about it missing an "NA", then please tell me the impact of that on my original question.

    One of the ways is because if SystemInformation.PowerStatus.BatteryLifePercent is, say, .555, your code will return "55.5%" whereas the original function will return "NA" (since there is no if for .555). Yours will also return "0%" (or "0.00%" or whatever) while the original will return "NA" in that case.

  • Matt (unregistered) in reply to ioiooiioio

    looks like VB to me... and for some reason VB does not have ==

    Captcha: oppeto - pedo op?

  • Jay (unregistered) in reply to PedanticCurmudgeon
    PedanticCurmudgeon:
    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.

    I briefly worked at a job where my boss came by and gave me a serious talking to because I had used a nested IF statement. We don't do things like that here, he explained. It's too difficult to understand. I was forced to re-write it using GOTOs.

  • Bananas (unregistered)

    I cried a little bit.

  • Jay (unregistered) in reply to Gurth
    Gurth:
    ioiooiioio:
    Shouldn't it also be an "==" instead of '='? Or is that just in java?
    In BASIC, the =-sign is both for assignment and for comparison. In C-like languages (including Java), = assigns and == compares.
    Ben Jammin:
    wbrianwhite:
    None of those will produce the same results as the function you're replacing. So does it matter?

    I'm genuinely curious as to how it isn't. If you're talking about it missing an "NA", then please tell me the impact of that on my original question.

    One of the ways is because if SystemInformation.PowerStatus.BatteryLifePercent is, say, .555, your code will return "55.5%" whereas the original function will return "NA" (since there is no if for .555). Yours will also return "0%" (or "0.00%" or whatever) while the original will return "NA" in that case.

    True, but I refuse to accept the idea that a goal of refactoring is to preserve all the existing bugs. Fix bugs ... crazy idea, I know.

  • (cs) in reply to Jay
    Jay:
    It's too difficult to understand.
    I would have asked: "It's too difficult to understand for whom?"

    Don't you just hate it when project managers and PHBs have a hard time understanding the code?

  • (cs) in reply to Jay
    Jay:
    Gurth:
    ioiooiioio:
    Shouldn't it also be an "==" instead of '='? Or is that just in java?
    In BASIC, the =-sign is both for assignment and for comparison. In C-like languages (including Java), = assigns and == compares.
    Ben Jammin:
    wbrianwhite:
    None of those will produce the same results as the function you're replacing. So does it matter?

    I'm genuinely curious as to how it isn't. If you're talking about it missing an "NA", then please tell me the impact of that on my original question.

    One of the ways is because if SystemInformation.PowerStatus.BatteryLifePercent is, say, .555, your code will return "55.5%" whereas the original function will return "NA" (since there is no if for .555). Yours will also return "0%" (or "0.00%" or whatever) while the original will return "NA" in that case.

    True, but I refuse to accept the idea that a goal of refactoring is to preserve all the existing bugs. Fix bugs ... crazy idea, I know.

    Although I agree with you, in some shops, that cowboy attitude would get you in deep shit. Trust me, I've "fixed" things while refactoring only to have to "unfix" my fix because it wasn't in the requirements. They were expecting to see the bug, but no longer did, and that, to them, meant they didn't know what else could have changed, and therefore needed to be retested.

  • bad_management (unregistered)

    One of my most productive days was throwing away 1000 lines of code. — Ken Thompson

  • cappeca (unregistered) in reply to Stabbitha
    Stabbitha:
    Obligatory "The real WTF is VB..."

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

    LOL yeah, people assume when we say TRWTF is language of choice, we don't use language of choice.

  • Tom (unregistered) in reply to C-Octothorpe
    C-Octothorpe:
    Jay:
    It's too difficult to understand.
    I would have asked: "It's too difficult to understand for whom?"

    Don't you just hate it when project managers and PHBs have a hard time understanding the code?

    Easy fix: never let them see the code.

    If they insist, give them a hex dump of the compiled object code, never the source code.

  • (cs) in reply to Argle
    Argle:
    Ben Jammin:
    Yeah, this could be drastically improved with a switch statement.
    Seen it... though admittedly by a student... though still not excused. Spent an hour discussing C string library and giving examples of all the basics. Gave an assignment to read in lines of text and convert to all uppercase. Everyone else does the 5-10 line program but one thought the whole thing should be an exercise in making a very big switch statement based on the letters on the line.
    Hopefully someone suggested that the student choose a different major (assuming the class wasn't an elective).
  • (cs)

    Should be:

            :
            ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.03" Then
                Return "3%"
            ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.02" Then
                Return "2%"
            ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.01" Then
                Return "1%"
            Else
                Return "HAA-ha!"
            End If
        End Get
    End Property
    
  • (cs) in reply to Anketam
    Anketam:
    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.

    I used to have an old Dell Latitude that had a modular bay for the CD-ROM that could be replaced with a second battery. One of the batteries was quite old, and its voltage reporting was messed up. According to Windows, the battery charge would drop from 100 to 0% in about 10 minutes, then continue to run (presumably from the other battery) for another hour at 0% while screaming to be plugged in.

  • (cs)

    Mozhet buyt ti krasivaya, na u menya zhenschina.

  • Is that a suppository up my ass or are you just happy to see me? (unregistered) in reply to C-Octothorpe
    C-Octothorpe:
    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...
    Sorry. Sometimes I make her miss work...

    Fresh outta eggs = no preggers!

  • Ben Jammin (unregistered) in reply to Gurth
    Gurth:
    Ben Jammin:
    I'm genuinely curious as to how it isn't. If you're talking about it missing an "NA", then please tell me the impact of that on my original question.
    One of the ways is because if SystemInformation.PowerStatus.BatteryLifePercent is, say, .555, your code will return "55.5%" whereas the original function will return "NA" (since there is no if for .555). Yours will also return "0%" (or "0.00%" or whatever) while the original will return "NA" in that case.

    I swear, I hate programmers. A simple if(0==...) can handle the "NA" check and correcting precision takes even less keystrokes.

    As, for those who are also curious to which one is best, my quick test seems to point to the first option. The 2nd and 3rd flip flop some times, but it is generally 2nd that wins out. This is the same for the 2 digit precision and whatever precision BatteryLifePercent returns.

    Visual Basic:
    FIRST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    TRWTF is Akismet. Really.

    Thanks for answering the meat of my question instead of picking nits.

  • (cs) in reply to Matt Westwood
    Matt Westwood:
    Mozhet buyt ti krasivaya, no u menya zhenschina.
    Fixed the typo, but I think I speak for all of us when I say there seems to be some context missing.
  • Ben Jammin (unregistered) in reply to BAF
    BAF:
    The first one.
    This guy, too.

    Also, why are the last 2 options around if just manually creating the string is faster? I would think they were super-optimized algorithms used to deprecate the good ol' way of doing it, but just appending the strings to each other is generally a 10-15% decrease in time.

    CAPTCHA - acsi: What stupid retorts do you have for this question I'm acsi-ng?

  • Andrew (unregistered) in reply to Craig
    Craig:
    It baffles me how people like this continue to get jobs as developers.
    Since they've been doing this shit for 5+ years, there's plenty of "5 years experience" job postings that they qualify for.
  • Meep (unregistered) in reply to jdw
    jdw:
    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%"

    What, and confuse it with all the other BatteryLifePercentStrings?

    	ElseIf SystemInformationPowerStatusBatteryLifePercentString = "0.2" Then
                Return "20%"
            ElseIf SystemInformationPowerStatusBatteryLifePercentString = "0.19" Then
                Return "19%"
            ElseIf SystemInformationPowerStatusBatteryLifePercentString = "0.18" Then
                Return "18%"
            ElseIf SystemInformationPowerStatusBatteryLifePercentString = "0.17" Then
                Return "17%"
            ElseIf SystemInformationPowerStatusBatteryLifePercentString = "0.16" Then
                Return "16%"
  • Annonymous (unregistered)

    That will work 99.98% of the time.

  • Mick (unregistered)
    article:
    "Judging from the code the previous programmer wrote, this is sadly one of the better pieces."
    This sentence makes sense (I think) but I don't think it says anything useful.

    Perhaps something out.

  • Professor Q (unregistered) in reply to Nagesh
    Nagesh:
    Maths by decimel is not being hard. Invented by Indian in fact at Hyderabad Unaversity: [image]
    Uhm, can you get new pics. I'm getting sick of the same new york times pics every day....

    TNX

  • ZSXcv (unregistered) in reply to Gurth
    Gurth:
    ioiooiioio:
    Shouldn't it also be an "==" instead of '='? Or is that just in java?
    In BASIC, the =-sign is both for assignment and for comparison. In C-like languages (including Java), = assigns and == compares.
    Ben Jammin:
    wbrianwhite:
    None of those will produce the same results as the function you're replacing. So does it matter?

    I'm genuinely curious as to how it isn't. If you're talking about it missing an "NA", then please tell me the impact of that on my original question.

    One of the ways is because if SystemInformation.PowerStatus.BatteryLifePercent is, say, .555, your code will return "55.5%" whereas the original function will return "NA" (since there is no if for .555). Yours will also return "0%" (or "0.00%" or whatever) while the original will return "NA" in that case.
    So....he's improved the solution (by making it work and be more succinct) and you complain? Methinks you are a turd.

  • FTFY (unregistered) in reply to Matt Westwood
    Matt Westwood:
    Mozhet buyt ti krasivaya, na u menya zhenschina.
    может быть ты красивая но у меня женщина
  • Mr. Magenta (unregistered) in reply to C-Octothorpe
    C-Octothorpe:
    TheCPUWizard:
    Those who propose a mathematical approach seem to be missing that the OP code will ONLY display if the ToString is an exact match.
    I love it: 100% - Perfect! N/A - Hmm, that's wierd 90% - Oh, there it is again. Good to know... N/A - What the fu... 80% - Allllright... N/A - Godammit! . . .

    Or you might see something similar if the battery is charging.

    • Battery life is 89%
    • If statement for 90% fires and misses
    • Battery moves up to 90%
    • If statement for 89% and down fires, but battery always reports 90% now.

    NA

  • (cs) in reply to Ben Jammin
    Ben Jammin:
    Yeah, this could be drastically improved with a switch statement.
    You're absolutely right--it could be: Go to the birch tree out back, cut a switch, and come back and beat the developer with it until he learns not to write code like this.

    OK, that's just pseudocode--feel free to write a 'do while' loop in your language of choice.

  • JJ (unregistered) in reply to Ben Jammin
    Ben Jammin:
    I swear, I hate programmers. A simple if(0==...) [...]
    Know what I hate? Yoda conditions.
  • Craven Weasel (unregistered) in reply to Matt
    Matt:
    looks like VB to me... and for some reason VB does not have ==

    Captcha: oppeto - pedo op?

    Some reason? Is it becuase it descends from a (admittedly horrible) language that predates C?

    The =/== silliness was introduced by Ken Thompson/and or Dennis Ritchie when they implemented "B", even though it was supposed to be derived from BCPL which seems to used the then conventional ALGOL style = for equality, and := for assignment.

    For some reason they changed this, and introduced a myrid of unintended behaviours (bugs) and Yoda conditonals upon the world, as well as making a generation of Pascal trained programmers (like me) brains explode when they first saw it (why oh why oh why?)

    Note - I'm not denigrating C - it's great for some things, only an abritary design decision made in the late 60s.

Leave a comment on “The Percent Conversion”

Log In or post as a guest

Replying to comment #:

« Return to Article