Click here to Skip to main content

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrasing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: The most creative log ever PinmemberFreak307-Oct-13 22:44 
GeneralRe: The most creative log ever PinprofessionalFredrik Bornander10-Oct-13 4:40 
GeneralRe: The most creative log ever PinmemberKornfeld Eliyahu Peter7-Oct-13 1:58 
GeneralRe: The most creative log ever PinmemberGuy Lavi7-Oct-13 2:11 
GeneralRe: The most creative log ever PinprofessionalBrisingr Aerowing7-Oct-13 4:36 
GeneralRe: The most creative log ever PinmemberRage11-Oct-13 0:18 
GeneralRe: The most creative log ever PinmemberGuy Lavi12-Oct-13 20:39 
JokeRedundancy at its finest [modified] PinmemberEdward Giles5-Oct-13 21:49 
I found this code in the My.Power extension for Visual Studio:
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%"
                ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.96" Then
                    Return "96%"
...
                ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.06" Then
                    Return "6%"
                ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.05" Then
                    Return "5%"
                ElseIf SystemInformation.PowerStatus.BatteryLifePercent.ToString = "0.04" Then
                    Return "4%"
                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 "NA"
                End If
            End Get
        End Property
What idiot would write this?
How about:
If SystemInformation.PowerStatus.BatteryLifePercent <= 0 OrElse _
   SystemInformation.PowerStatus.BatteryLifePercent > 1 OrElse _
   IsNothing(SystemInformation.PowerStatus.BatteryLifePercent) OrElse _
   Single.IsNaN(SystemInformation.PowerStatus.BatteryLifePercent) OrElse _
   Single.IsInfinity(SystemInformation.PowerStatus.BatteryLifePercent) Then
   Return "NA"
Else
   Return FormatNumber(SystemInformation.PowerStatus.BatteryLifePercent * 100, 0) & "%"
End If
P.S. Code shortened by about 96%

modified 6-Oct-13 3:59am.

GeneralRe: Redundancy at its finest PinmemberKornfeld Eliyahu Peter6-Oct-13 9:03 
GeneralRe: Redundancy at its finest PinprofessionalBrisingr Aerowing6-Oct-13 14:33 
GeneralRe: Redundancy at its finest PinmemberRyanDev7-Oct-13 2:32 
GeneralRe: Redundancy at its finest PinprofessionalBernhard Hiller6-Oct-13 22:14 
GeneralRe: Redundancy at its finest PinmemberEdward Giles11-Nov-13 23:34 
GeneralRe: Redundancy at its finest PinprotectorOriginalGriff6-Oct-13 22:29 
GeneralRe: Redundancy at its finest PinmemberRyanDev7-Oct-13 2:33 
GeneralRe: Redundancy at its finest Pinmember0bx7-Oct-13 23:48 
GeneralRe: Redundancy at its finest PinmemberMacSpudster5-Nov-13 5:47 
JokeHow to zero-pad a number? PinmemberSortaCore1-Oct-13 0:27 
GeneralRe: How to zero-pad a number? PinprofessionalNagy Vilmos1-Oct-13 0:49 
GeneralRe: How to zero-pad a number? PinmemberSortaCore1-Oct-13 0:55 
GeneralRe: How to zero-pad a number? PinprofessionalPIEBALDconsult1-Oct-13 4:18 
GeneralRe: How to zero-pad a number? PinprofessionalRichard Deeming1-Oct-13 4:27 
GeneralRe: How to zero-pad a number? PinprofessionalPIEBALDconsult1-Oct-13 6:27 
GeneralRe: How to zero-pad a number? PinprofessionalRichard Deeming1-Oct-13 7:09 
GeneralRe: How to zero-pad a number? PinprofessionalPIEBALDconsult1-Oct-13 10:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 28 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid