Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: The joys of bad code

by demerphq (Chancellor)
on Oct 26, 2004 at 14:29 UTC ( #402603=note: print w/ replies, xml ) Need Help??


in reply to The joys of bad code

This charmer was found in the codebase im working on now. I almost fell out of my chair when I first encountered it:

Public Function MyMonthName(ByVal m As Long, _ Optional ByVal appr As Boolean = False) As + String Dim tmp As String Select Case m Case 1: tmp = "January" Case 2: tmp = "February" Case 3: tmp = "March" Case 4: tmp = "April" Case 5: tmp = "May" Case 6: tmp = "June" Case 7: tmp = "July" Case 8: tmp = "August" Case 9: tmp = "September" Case 10: tmp = "October" Case 11: tmp = "November" Case 12: tmp = "December" Case Else: tmp = Format(m, "0") End Select If appr Then MyMonthName = Left(tmp, 3) Else MyMonthName = tmp End If End Function

---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi

    Flux8



Comment on Re: The joys of bad code
Download Code
Re^2: The joys of bad code
by thor (Priest) on Oct 26, 2004 at 15:35 UTC
    Perhaps I don't know enough Visual Basic (I think this is what this is anyways...). Care to elaborate so I too can fall out of my chair?

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

      Glad somebody was brave enough to ask. It seems like reasonable code to me, too. There are better ways to do it in other languages, but if you're writing in VB, you have to live with the limitations that implies. (In Perl I'd either use DateTime or make it a hash lookup.)

      "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

        A hash lookup? Why not an array? Anyway, i wouldnt have laughed so hard if it had been written something like the following:

        Public Function GetMonthName(ByVal m As Long, _ Optional ByVal abbr As Boolean = False) As String Static Months(12) As String If (Months(1) <> "January") Then Months(1) = "January" Months(2) = "February" Months(3) = "March" Months(4) = "April" Months(5) = "May" Months(6) = "June" Months(7) = "July" Months(8) = "August" Months(9) = "September" Months(10) = "October" Months(11) = "November" Months(12) = "December" End If Dim tmp As String If (m >= 1 And m <= 12) Then tmp = Months(m) Else tmp = Format(m, "0") End If If abbr Then GetMonthName = Left(tmp, 3) Else GetMonthName = tmp End If End Function

        ---
        demerphq

          First they ignore you, then they laugh at you, then they fight you, then you win.
          -- Gandhi

          Flux8


        An array lookup would work, too.
        Actually, there's a pretty nifty native way to do this. Probably less efficient than a hash lookup, but more consistent.
        Function MyMonthName(ByVal m As Long, Optional ByVal appr As Boolean = + False) As String Dim v v = CDate("2004-" & m & "-01") Dim fmt As String If appr Then fmt = "MMM" Else fmt = "MMMM" End If MyMonthName = Format(v, fmt) End Function
Re^2: The joys of bad code
by bart (Canon) on Oct 26, 2004 at 20:13 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://402603]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2014-08-21 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (136 votes), past polls