Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. What a gem.

What a gem.

Scheduled Pinned Locked Moved The Weird and The Wonderful
rubycom
37 Posts 27 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A AaronM_NZ

    Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

    Public ReadOnly Property BatteryPercent()
    ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
    Get
    If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
    Return "100%"
    ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
    Return "99%"
    ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
    Return "98%"
    ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
    Return "97%"
    ... etc

    Wow is all I can say.

    0 Offline
    0 Offline
    0bx
    wrote on last edited by
    #7

    This is pure genius, if he gets paid by the line that is. If this method is ran while the battery is charging, is there a chance that it would return "NA" because the battery percentage changed at the correct moment? :)

    Giraffes are not real.

    1 Reply Last reply
    0
    • A AaronM_NZ

      Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

      Public ReadOnly Property BatteryPercent()
      ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
      Get
      If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
      Return "100%"
      ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
      Return "99%"
      ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
      Return "98%"
      ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
      Return "97%"
      ... etc

      Wow is all I can say.

      F Offline
      F Offline
      Fabio Franco
      wrote on last edited by
      #8

      Wow! This is only not worse than what I've seen, because the DB field length related to what I found is small:

      public string GetStringToDatabase(int value)
      {
      if (value > 99999999)
      return "0" + value.ToString();
      else if (value > 9999999)
      return "00" + value.ToString();
      else if (value > 999999)
      return "000" + value.ToString();
      .
      .
      .
      }

      "To alcohol! The cause of, and solution to, all of life's problems" - Homer Simpson

      J 1 Reply Last reply
      0
      • A AaronM_NZ

        Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

        Public ReadOnly Property BatteryPercent()
        ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
        Get
        If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
        Return "100%"
        ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
        Return "99%"
        ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
        Return "98%"
        ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
        Return "97%"
        ... etc

        Wow is all I can say.

        E Offline
        E Offline
        edmurphy99
        wrote on last edited by
        #9

        Maybe they got paid by the line

        1 Reply Last reply
        0
        • A AaronM_NZ

          Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

          Public ReadOnly Property BatteryPercent()
          ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
          Get
          If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
          Return "100%"
          ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
          Return "99%"
          ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
          Return "98%"
          ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
          Return "97%"
          ... etc

          Wow is all I can say.

          B Offline
          B Offline
          BGArts
          wrote on last edited by
          #10

          Should be an philosopher, not a programmer. This is pure, unadulterated logic.

          1 Reply Last reply
          0
          • A AaronM_NZ

            Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

            Public ReadOnly Property BatteryPercent()
            ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
            Get
            If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
            Return "100%"
            ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
            Return "99%"
            ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
            Return "98%"
            ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
            Return "97%"
            ... etc

            Wow is all I can say.

            T Offline
            T Offline
            Tomz_KV
            wrote on last edited by
            #11

            I am wondering if the statement continues to reach 0%. To return a result, the program will have to add a power source somewhere.

            TOMZ_KV

            1 Reply Last reply
            0
            • A AaronM_NZ

              Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

              Public ReadOnly Property BatteryPercent()
              ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
              Get
              If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
              Return "100%"
              ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
              Return "99%"
              ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
              Return "98%"
              ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
              Return "97%"
              ... etc

              Wow is all I can say.

              L Offline
              L Offline
              Lilith C
              wrote on last edited by
              #12

              Could have made a better case for switch/case statements. And if the device needed display "NA" it probably wouldn't have been running anyway.

              I'm not a programmer but I play one at the office

              1 Reply Last reply
              0
              • J Julien Villers

                It couldn't have been done with a generic approach, look at the last line:

                Else
                Return "NA"

                That required a custom solution! Just kidding ;) BTW, shouldn't that be 'N/A'?

                'As programmers go, I'm fairly social. Which still means I'm a borderline sociopath by normal standards.' Jeff Atwood 'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

                L Offline
                L Offline
                Lilith C
                wrote on last edited by
                #13

                Julien Villers wrote:

                BTW, shouldn't that be 'N/A'?

                Not when you have to scrape for every byte you can get.

                I'm not a programmer but I play one at the office

                1 Reply Last reply
                0
                • A AaronM_NZ

                  Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

                  Public ReadOnly Property BatteryPercent()
                  ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
                  Get
                  If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
                  Return "100%"
                  ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
                  Return "99%"
                  ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
                  Return "98%"
                  ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
                  Return "97%"
                  ... etc

                  Wow is all I can say.

                  P Offline
                  P Offline
                  Paulo_JCG
                  wrote on last edited by
                  #14

                  I like the ToString...

                  If SystemInformation.PowerStatus.BatteryLifePercent = 1.00f then ...

                  is such a PITA

                  Paulo Gomes Over and Out :D

                  1 Reply Last reply
                  0
                  • A AaronM_NZ

                    Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

                    Public ReadOnly Property BatteryPercent()
                    ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
                    Get
                    If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
                    Return "100%"
                    ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
                    Return "99%"
                    ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
                    Return "98%"
                    ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
                    Return "97%"
                    ... etc

                    Wow is all I can say.

                    J Offline
                    J Offline
                    James Treworgy
                    wrote on last edited by
                    #15

                    What a shortcut! A qualified programmer would have accounted for the fact that BatteryLifePercent is floating point, and may not be in increments of 1/100th. What if it's 0.975? Obviously this should have been written as:

                    If SystemInformation.PowerStatus.BatteryLifePercent <= 1 And
                    SystemInformation.PowerStatus.BatteryLifePercent > 0.99 then Return "99%"
                    ElseIf SystemInformation.PowerStatus.BatteryLifePercent <= 0.99 And
                    SystemInformation.PowerStatus.BatteryLifePercent > 0.98 then Return "98%"
                    ...

                    :)

                    1 Reply Last reply
                    0
                    • A AaronM_NZ

                      Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

                      Public ReadOnly Property BatteryPercent()
                      ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
                      Get
                      If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
                      Return "100%"
                      ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
                      Return "99%"
                      ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
                      Return "98%"
                      ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
                      Return "97%"
                      ... etc

                      Wow is all I can say.

                      J Offline
                      J Offline
                      Jasmine2501
                      wrote on last edited by
                      #16

                      They are clearly just coding around a bad framework. There's a field called BatteryLifePercent, but it's not actually a percentage number. Obviously the framework is incorrect.

                      1 Reply Last reply
                      0
                      • A AaronM_NZ

                        Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

                        Public ReadOnly Property BatteryPercent()
                        ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
                        Get
                        If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
                        Return "100%"
                        ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
                        Return "99%"
                        ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
                        Return "98%"
                        ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
                        Return "97%"
                        ... etc

                        Wow is all I can say.

                        Sander RosselS Offline
                        Sander RosselS Offline
                        Sander Rossel
                        wrote on last edited by
                        #17

                        Option Strict is off or the Property would have an As clause! Correct would've been:

                        Public ReadOnly Property BatteryPercent As Percent
                        Get
                        If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
                        ' No implicit conversions with Strict On!
                        Return Convert.ToPercentage("100%")
                        ' Etc...

                        Now that just made a world of difference :)

                        It's an OO world.

                        public class Naerling : Lazy<Person>{
                        public void DoWork(){ throw new NotImplementedException(); }
                        }

                        1 Reply Last reply
                        0
                        • A AaronM_NZ

                          Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

                          Public ReadOnly Property BatteryPercent()
                          ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
                          Get
                          If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
                          Return "100%"
                          ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
                          Return "99%"
                          ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
                          Return "98%"
                          ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
                          Return "97%"
                          ... etc

                          Wow is all I can say.

                          Y Offline
                          Y Offline
                          YSLGuru
                          wrote on last edited by
                          #18

                          Could be wrong but it looks like a case of lack of knowledge, not knwoing/understanding that there is a better/more effecoent way to achieve teh same goal. I work in RDBMS (sepcifcally SQL Server) and I routinely see this same kind of bad design choice and usually its due to a lack of knowledge/eductaion on the product/system. For example when the develoepr needs to store a bolean value ( 0 and <>0 ) they use a NUMERIC data type which [by default) uses 9 bytes instead of using the smaller BIT data type.

                          J 1 Reply Last reply
                          0
                          • A AaronM_NZ

                            Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

                            Public ReadOnly Property BatteryPercent()
                            ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
                            Get
                            If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
                            Return "100%"
                            ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
                            Return "99%"
                            ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
                            Return "98%"
                            ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
                            Return "97%"
                            ... etc

                            Wow is all I can say.

                            A Offline
                            A Offline
                            AspDotNetDev
                            wrote on last edited by
                            #19

                            This is why degrees are useful. You may not be the best programmer in the world after graduating, but at least you should learn enough to avoid code like this. Having a get-r-done attitude is fine and all, but a get-r-done intelligently attitude is much better.

                            Somebody in an online forum wrote:

                            INTJs never really joke. They make a point. The joke is just a gift wrapper.

                            J 1 Reply Last reply
                            0
                            • A AaronM_NZ

                              Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

                              Public ReadOnly Property BatteryPercent()
                              ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
                              Get
                              If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
                              Return "100%"
                              ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
                              Return "99%"
                              ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
                              Return "98%"
                              ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
                              Return "97%"
                              ... etc

                              Wow is all I can say.

                              W Offline
                              W Offline
                              weberrich
                              wrote on last edited by
                              #20

                              Although, I know doesn't look all that good. There may be come consideration to optimization. Does the huge case statement cost less than a few function calls? Just a thought, and like to play devils advocate.

                              1 Reply Last reply
                              0
                              • Y YSLGuru

                                Could be wrong but it looks like a case of lack of knowledge, not knwoing/understanding that there is a better/more effecoent way to achieve teh same goal. I work in RDBMS (sepcifcally SQL Server) and I routinely see this same kind of bad design choice and usually its due to a lack of knowledge/eductaion on the product/system. For example when the develoepr needs to store a bolean value ( 0 and <>0 ) they use a NUMERIC data type which [by default) uses 9 bytes instead of using the smaller BIT data type.

                                J Offline
                                J Offline
                                Jorgen Andersson
                                wrote on last edited by
                                #21

                                That's horrible. Everyone knows that you should use a varchar and store it as 'True' or 'False'.

                                Light moves faster than sound. That is why some people appear bright, until you hear them speak. List of common misconceptions

                                1 Reply Last reply
                                0
                                • A AaronM_NZ

                                  Stumbled across this gem today. http://pastebin.com/4Nx8yggU[^] For preservation sake, here is a snippet, but you get the idea:

                                  Public ReadOnly Property BatteryPercent()
                                  ' This code will retrieve the BatteryLifePercent property and convert it to a percent.
                                  Get
                                  If SystemInformation.PowerStatus.BatteryLifePercent.ToString = "1" Then
                                  Return "100%"
                                  ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.99" Then
                                  Return "99%"
                                  ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.98" Then
                                  Return "98%"
                                  ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.97" Then
                                  Return "97%"
                                  ... etc

                                  Wow is all I can say.

                                  F Offline
                                  F Offline
                                  firegryphon
                                  wrote on last edited by
                                  #22

                                  That is a joke right? Does this joke include enough to go all the way to 0%?

                                  1 Reply Last reply
                                  0
                                  • A AspDotNetDev

                                    This is why degrees are useful. You may not be the best programmer in the world after graduating, but at least you should learn enough to avoid code like this. Having a get-r-done attitude is fine and all, but a get-r-done intelligently attitude is much better.

                                    Somebody in an online forum wrote:

                                    INTJs never really joke. They make a point. The joke is just a gift wrapper.

                                    J Offline
                                    J Offline
                                    Julien Villers
                                    wrote on last edited by
                                    #23

                                    You were joking right? I saw graduates that couldn't tell a variable from an array from a class from an object (etc...). Oh, and they weren't like barely getting their grades, in fact, they were top of the class (due to compensating programming with communication or economics, plus getting their projects done by others).

                                    'As programmers go, I'm fairly social. Which still means I'm a borderline sociopath by normal standards.' Jeff Atwood 'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

                                    A 1 Reply Last reply
                                    0
                                    • S smcnulty2000

                                      And well commented. I think we can all appreciate the thoroughness of the approach.

                                      _____________________________ Give a man a mug, he drinks for a day. Teach a man to mug... The difference between an ostrich and the average voter is where they stick their heads.

                                      M Offline
                                      M Offline
                                      Mark AJA
                                      wrote on last edited by
                                      #24

                                      Don't know what language this is, but I'm sure you could use the following to reduce the code length:

                                      ' ...
                                      Return (SystemInformation.PowerStatus.BatteryLifePercent.ToString *100) & "%"
                                      ' . End of function.

                                      S P 2 Replies Last reply
                                      0
                                      • J Julien Villers

                                        You were joking right? I saw graduates that couldn't tell a variable from an array from a class from an object (etc...). Oh, and they weren't like barely getting their grades, in fact, they were top of the class (due to compensating programming with communication or economics, plus getting their projects done by others).

                                        'As programmers go, I'm fairly social. Which still means I'm a borderline sociopath by normal standards.' Jeff Atwood 'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

                                        A Offline
                                        A Offline
                                        AspDotNetDev
                                        wrote on last edited by
                                        #25

                                        Nope, not joking. Not everybody who goes through college is a better programmer than a non-college programmer. Though I'd say more college grads have better sense. Of course, that's from my anecdotal experience*, so YMMV. *For example, I wrote my 558 Lines of QuickBasic Glory before I went to college. Relying on file IO, "unnecessary" optimizations, and third party libraries seemed like too much work at the time.

                                        Somebody in an online forum wrote:

                                        INTJs never really joke. They make a point. The joke is just a gift wrapper.

                                        J 1 Reply Last reply
                                        0
                                        • M Mark AJA

                                          Don't know what language this is, but I'm sure you could use the following to reduce the code length:

                                          ' ...
                                          Return (SystemInformation.PowerStatus.BatteryLifePercent.ToString *100) & "%"
                                          ' . End of function.

                                          S Offline
                                          S Offline
                                          smcnulty2000
                                          wrote on last edited by
                                          #26

                                          There you go, thinking like a programmer again. Where's that going to get ya? :)

                                          _____________________________ Give a man a mug, he drinks for a day. Teach a man to mug... The difference between an ostrich and the average voter is where they stick their heads.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups