• (disco) in reply to dkf
    dkf:
    you're just demonstrating your general ignorance.

    Uhh, I was commenting on whether I can use String vs string as a type name in C#. I'm not ignorant about C# although I will say I give a rats ass about Java and it can go to hell. Double that sentiment once Oracle took over.

  • (disco)

    Honestly, I can see code like this being used in certain edge cases. Sure, you'll never be able to go beyond 12, but some cases might require this kind of code.

    function lpad(String s, int lengthResult=12):
    if(s.length > lengthResult):
    throw new WayTooBigException()
    for(int i = s.length; i < lengthResult; i++):
    s = " " + s
    return s

    I'd use this code if I wanted to sacrifice memory usage and cpu usage for readability. (The overhead involved in the strcat within the loop is bad.)

    function lpad(String s, int lengthResult=12):
    int lengthS = s.length
    if(lengthS > lengthResult):
    throw new WayTooBigException()
    String s_new = malloc(lengthResult + 1)
    for(int i = 0; i < lengthResult - lengthS; i++):
    s_new[i] = ' '
    for(int i = lengthResult - lengthS; i < lengthResult; i++):
    s_new[i] = s[i - lengthResult + lengthS]
    s_new[lengthResult] = '\0'
    return new_s

    This is the sort of algorithm I'd use if I wanted to sacrifice readability for memory and cpu usage. This is probably the type of algorithm that comes to mind when you see a WTF like this.

    function lpad12(String s):
    switch(s.length):
    case 0:
    return "            "
    case 1:
    return "           " + s
    //etc
    case 12:
    return s
    default:
    throw new WayTooBigException();

    You normally think that this type of code is a joke. However, in a production environment where cpu and memory usage is a serious concern, you can sacrifice adaptability, maintainability, and space on disk for that little bit of cpu gain that you get from using a jump table instead of a for loop. This also doesn't have the overhead of the strcat function, making it a better option, cpu-wise, than the first one.

    Even so, I've never been in an environment so performance-intense that the first or second option wouldn't be better. I'll take readable code over code that's a few nanoseconds faster any day.

  • (disco) in reply to Jack_Penick
    Jack_Penick:
    You normally think that this type of code is a joke. However, in a production environment where cpu and memory usage is a serious concern, you can sacrifice adaptability, maintainability, and space on disk for that little bit of cpu gain that you get from using a jump table instead of a for loop.

    @antiquarian, can we get a rating on this one?

  • (disco) in reply to boomzilla
    boomzilla:
    You normally think that this type of code is a joke. However, in a production environment where cpu and memory usage is a serious concern, you can sacrifice adaptability, maintainability, and space on disk for that little bit of cpu gain that you get from using a jump table instead of a for loop.

    @antiquarian, can we get a rating on this one?

    I've never had to program for anything that requires high performance, so if you have to program for high performance, please don't take my word for it - run performance tests. I'm basing this statement on intuition alone.

    I imagine the gains would be so inconceivably small when padding to 12 that there's no point in using an algorithm like this, but if we were padding to, say, 600, it might be significant.

  • (disco) in reply to Jack_Penick
    Jack_Penick:
    I imagine the gains would be so inconceivably small when padding to 12 that there's no point in using an algorithm like this, but if we were padding to, say, 600, it might be significant.

    The biggest (and probably dominant) loss in that scenario would be from the code itself being larger…

  • (disco) in reply to dkf
    dkf:
    The biggest (and probably dominant) _loss_ in that scenario would be from the code itself being larger…

    What are you talking about? I thought jump tables = magic!

  • (disco)
    public static String padRegistrationNumber(String registrationNumber) {
        return String.format("%12s", registrationNumber == null ? "" : registrationNumber); // assuming it's java
    }
    
  • (disco)

    PHP TO THE RESCUE!

    <?=str_pad($registrationNumber, 12);?>
    

    There. Best language ever. We can all go home now.

  • (disco) in reply to Jack_Penick
    Jack_Penick:
    I thought jump tables = magic!

    Notice that “!” there? Jump tables aren't just magic, they're magic factorial and so need quite a bit of room to store.

  • (disco)

    Note that he doesn't actually say there could be a better way to do this, but that there is a better way to do this. He (or maybe she) knows full well what they are doing and that it is terrible. Perhaps management told them something like "Do this that way or else" or "We need more lines of code, that way is too short"?

  • Axel (unregistered)

    Fuck, Remy, learn to spell "led." It's only three fucking letters.

Leave a comment on “Accurate Comments”

Log In or post as a guest

Replying to comment #:

« Return to Article