• (nodebb)

    but that raises a question: why would your year string be longer than 15 characters?

    "The Year of Our Lord Nineteen Hundred And Seventy Eight" is considerably longer than fifteen characters.

  • (nodebb)

    "Nineteen Hundred and Seventy Two" -- 29 characters....

    Addendum 2021-07-27 06:38: (Did not see Steve's comment...)

  • Pike (unregistered)

    Also known as the Y1Q problem.

  • Rhialto (unregistered)

    sprintf (or your local equivalent) by any other name is YearPadText ... for certain variations of the operand

  • Prime Mover (unregistered)

    Aha ... so TRWTF is:

        Dim valLen As Integer
        valLen = val.Len
    

    rather than using val.Len directly in the switch statement? Amirite?

  • Andy Miller (unregistered)

    Now I'm worried about the year 100,000,000,000,000 bug !

  • RLB (unregistered) in reply to Andy Miller

    VB.Net: for when you're not worried about the sun going Black Dwarf.

  • Wharrgarbl (unregistered)

    "An" VB.Net?

  • WTFGuy (unregistered)

    @PrimeMover

    Any time I see VB.Net code that's not obviously brand new fresh, I suspect that it's really a port from VB6 and was originally written in VB-about-3. And for sure I think of the possibility the dev who wrote it dates from VB-about-3. Or that the book they learned VB.Net from last year came from way back when.

    This coding gem smells to me like it started life eons ago as an If/ElseIf/ElseIf/ElseIf/.../ElseIf/Else construct. Where hoisting the repeated property access into a single access saved in a temp makes sense. In the days before VB compilers, much less optimizing ones, that would need to be done manually by the dev.

    Though I do wonder why one would ever need to right-pad a year string out to 15 chars.

    Talking about separation of concerns, this code is separating concerns a bit more than most. Even setting aside the inevitable copypasta of the same exact function into different modules, it makes me wonder how many other specialized similar functions there are: FirstNamePadTextLength10(), FristNamePadTextLength15(), LastNamePadTextLength15(), CItyPadTextTo12Long(), etc., etc.

  • (nodebb)

    This fuction takes a string value and expects it to be a year. How do you know it's a year? If you pass in any value that's not a year, God will strike you with lightning and flood your office.

  • Stella (unregistered)

    This code can be greatly simplified:

    Protected Function YearPadText(ByVal val As String) As String
        Dim valLen As Integer
        valLen = val.Len
    
        Select Case Not (valLen >= 15)
            Case True
                val = YearPadText(val + " ")
        End Select
        Return val
    End Function
    
  • DeeKay (unregistered) in reply to Stella

    My eyes are burning!

  • David Jackson (unregistered) in reply to Steve_The_Cynic

    Or for that matter

    MMDCCLXXIV Ab Urbe Condita

  • The Shadow (doesn't) Knows (unregistered)

    Frist thing that comes to mind is they're building a display for a green screen which doesn't have absolute positioning.

  • Carl Witthoft (google) in reply to Stella

    no no no , it's gotta be


    Case True val = YearPadText" " + (val)

    Addendum 2021-07-27 12:17: arrggh, with open parenthesis in the right place, of course

  • ZZartin (unregistered)

    If only VBscript had function to left you easily grab a substring so this could be done in one line.

  • Hrolf (unregistered)

    If only we had case fall-through we could save a lot of white space in between those quote marks.

  • Prime Mover (unregistered) in reply to WTFGuy

    @WTFGuy

    It is of course possible that it started as a simple routine to pad a 2-digit (or 1-digit) year out to a string of length 4, then someone decided: hey! That's cool! We can use this to pad out the (whatever field) out to fill a 15-character field! Just need to add a few more options ... rename it? Nah, that compromises the integrity of the codebase, leave it as is.

    So you end up with:

    FirstNamePadded = YearPadText (FirstName)
    
    LastNamePadded = YearPadText (LastName)
    

    ... and so on, whatever the syntax of this appalling language is.

  • I dunno LOL ¯\(°_o)/¯ (unregistered)

    Those who do not learn PRINT USING are doomed to re... using it. (okay, so there's no sprintf equivalent, shush)

    And yes, you know that "YearPadText" ended up being re... usinged for everything.

  • (nodebb)

    The fix:

    Protected Function YearPadText(ByVal val As String) As String
        Dim valLen As Integer = val.Len
    
        If valLen >= 15 Then
            Return val
        Else
            Return val & Space(15 - valLen)
        End If
    End Function
    

    That answers the how, but the question still remains, "Why?"

  • Martin (unregistered)

    nuget install leftpad

  • NoLand (unregistered) in reply to Nutster

    Regarding the why, this is probably not meant to apply padding to the year on itself, but rather providing a spacing to go between a 16-chars max. label and a year in a list view. So the input would be the text label and the year would be printed following to it. Much like a 16-character width tab stop.

  • eric bloedow (unregistered)
    Comment held for moderation.
  • bob (unregistered)

    I'm concerned that seeing these code snippets is making me a worse programmer

  • SomeGuyOnTheInternet (unregistered) in reply to WTFGuy

    Great comment, and I agree that it's entirely possible for correct code to become a WTF with the evolution of the language and porting through language versions.

    That said, even going back to QBasic, I think this snippet is a WTF. The usual way to pad strings back in those days was to add spaces, then use LEFT$() or RIGHT$() to grab the required length string. I would expect ported code to look something like

    Microsoft.VisualBasic.Left(val & Space(15), 1, 15)
    

    Also Val() has been a reserved keyword since those days too. Using it as a variable name is another WTF.

Leave a comment on “Without Any Padding”

Log In or post as a guest

Replying to comment #:

« Return to Article