Kill it with fire



  • This is a project I have to maintain. And by maintain I mean delete parts of it and rewrite them essentially from scratch. The following is a code snippet from a jsp page. What it's supposed to do is grab a bunch of image filenames from the database and build a page where there are 4 images per row.

              <%
               while (rset.next()) {
                if (x==0){%>
                <tr>
                <%}
                x=x+1;
                if (x<6){%>
                   <td valign="top" align="left"><img align="left" border="0" alt="<%=myUtil.rQuotes(rset.getString(5),false,true)%>" src="<%=rset.getString(1)%><%=rset.getString(2)%>"></td>
                <%}%>
                <%if (x==5){%></tr><%}%>           
                <%if (x==6){%><tr><td>&nbsp;</td></tr><tr><%}%>
                <%if (x>=6&&x<11){ %>
                    <td valign="top" align="left"><img align="left" border="0" alt="<%=myUtil.rQuotes(rset.getString(5),false,true)%>" src="<%=rset.getString(1)%><%=rset.getString(2)%>"></td>
                   <%}%>            
               <%if (x==10){%></tr><%}%>
               <%if (x==11){%><tr><td><img src="/00/keno.gif" width="1" height="15"></td></tr><tr><%}%>
               <%if (x>=11&&x<16){ %>
                   <td valign="top" align="left"><img align="left" border="0" alt="<%=myUtil.rQuotes(rset.getString(5),false,true)%>" src="<%=rset.getString(1)%><%=rset.getString(2)%>"></td>                <%}%>
               <%}%>



  • @DOA said:

    This is a project I have to maintain. And by maintain I mean delete parts of it and rewrite them essentially from scratch.

    To be fair, that's pretty much what "maintain" means everywhere.

    @DOA said:

    The following is a code snippet from a jsp page. What it's supposed to do is grab a bunch of image filenames from the database and build a page where there are 4 images per row.

    I don't speak PHP, but I presume you can replace all that hideous inline HTML with predefined named string constants of some kind, can't you?  And then you can refactor it into a real loop-and-case construct.

    It looks roughly like what someone might write if they'd heard of state machines, but didn't really get it.

     

    Also if they'd heard of looping, but didn't really get it. 



  • @DaveK said:

     I don't speak PHP, but I presume you can replace all that hideous inline HTML with predefined named string constants of some kind, can't you?  And then you can refactor it into a real loop-and-case construct.

     

    Good thing too since this is JavaScript.

    This is actually a common construct among those who a.) are new to JavaScript and J2EE in general, and b.) aren't using any frameworks / JSTL / jQuery / JSF (or something that has built in tag libraries for such routines).



  • @amischiefr said:

    Good thing too since this is JavaScript.
     

    Javascript is what I do in ASP-classic and client-side. The example above must be Java, unless Java Server Pages allows JavaScript to be used as a language ANd extends it with methods such as getString.



  •  JSP with 100% Scriplet goodness.



  • @DaveK said:

    @DOA said:
    This is a project I have to maintain. And by maintain I mean delete parts of it and rewrite them essentially from scratch.

    To be fair, that's pretty much what "maintain" means everywhere.
    I wish I'd get to just add functionality as opposed to getting rid of the crap for once. What can I say, I'm a dreamer...@DaveK said:
    It looks roughly like what someone might write if they'd heard of state machines, but didn't really get it.

    Also if they'd heard of looping, but didn't really get it.

    Also note that the back-end allows you to have as many images as you like. But you'll only see up to 12 of them, because really, why would you need more?

     



  • @DaveK said:

    refactor it into a real loop-and-case construct

    case? What's wrong with just doing this instead?

    <% if (x % 4 == 0) { %>
        </tr><tr>
    <% } %>



  • @SlyEcho said:

    @DaveK said:
    refactor it into a real loop-and-case construct

    case? What's wrong with just doing this instead?

    <% if (x % 4 == 0) { %>
        </tr><tr>
    <% } %>

    Nothing at all, but I wasn't even going to begin to think about the detail of the implementation until we'd factored out all those inline strings and could see whether they all actually matched and then be able to finally read the code in some kind of even-half-legible form and see whether it actually was doing the exact same thing each time round the 'unrolled loop'.  So giving an algorithmic description of it as a "loop and case" is reasonable, because what you have there is formally identical to and a simple optimisation of

    <% switch (x % 4) {

    case 0:%>
        </tr><tr><% 

    default: 

    } %>

    (modulo any syntax glitches.  warning, untested code!)



  • @amischiefr said:

    @DaveK said:

     I don't speak PHP, but I presume you can replace all that hideous inline HTML with predefined named string constants of some kind, can't you?  And then you can refactor it into a real loop-and-case construct.

     

    Good thing too since this is JavaScript.

    :-) I don't speak any web-programming languages.  I merely laugh at and mock them from the heights of my fortress of hand-coded assembler solitude!

    : 0400 a0 00 2e 20 04 90 07 20 d2 ff a8 4c 02 04 60 60

    : 0420 48 45 4c 4c 4f 20 55 4f 52 4c 44 21 00

    (hmmm... will have to google that to see if I remembered it right.... it's been a few years, I'll bet I got the X and Y registers mixed up and the bne is probably wrong too...) 



  • @DaveK said:

    : 0400 a0 00 2e 20 04 90 07 20 d2 ff a8 4c 02 04 60 60

    : 0420 48 45 4c 4c 4f 20 55 4f 52 4c 44 21 00

    (hmmm... will have to google that to see if I remembered it right.... it's been a few years, I'll bet I got the X and Y registers mixed up and the bne is probably wrong too...) 

    I looked it up.  2e should be b9, 90 should be f0, and a8 should have been c8.

    Still, I think that's not a bad showing, coming at it cold and after a twenty year break.



  • @DaveK said:

    @DaveK said:

    : 0400 a0 00 2e 20 04 90 07 20 d2 ff a8 4c 02 04 60 60

    : 0420 48 45 4c 4c 4f 20 55 4f 52 4c 44 21 00

    (hmmm... will have to google that to see if I remembered it right.... it's been a few years, I'll bet I got the X and Y registers mixed up and the bne is probably wrong too...) 

    I looked it up.  2e should be b9, 90 should be f0, and a8 should have been c8.

    Still, I think that's not a bad showing, coming at it cold and after a twenty year break.

    Riveting.



  • @DaveK said:

    (hmmm... will have to google that to see if I remembered it right.... it's been a few years, I'll bet I got the X and Y registers mixed up and the bne is probably wrong too...) 

     I wonder if you could just type ASM into Google input box and it would spew the hex codes for you...


  • @DaveK said:

    @DaveK said:

    : 0400 a0 00 2e 20 04 90 07 20 d2 ff a8 4c 02 04 60 60
    : 0420 48 45 4c 4c 4f 20 55 4f 52 4c 44 21 00
    (hmmm... will have to google that to see if I remembered it right.... it's been a few years, I'll bet I got the X and Y registers mixed up and the bne is probably wrong too...)
    I looked it up.  2e should be b9, 90 should be f0, and a8 should have been c8. Still, I think that's not a bad showing, coming at it cold and after a twenty year break.
    MOV     AL,[B900]
    AND [SI],AL
    LOCK
    POP ES
    AND DL,DL
    DEC AX
    DEC SP
    ADD AL,[SI]
    DB 60, 60
    DB 10 DUP (00)
    DB "HELLO UORLD!", 00
    Which crashes my NTVDM. Good job.


  • @TwelveBaud said:

    @DaveK said:

    @DaveK said:

    : 0400 a0 00 2e 20 04 90 07 20 d2 ff a8 4c 02 04 60 60
    : 0420 48 45 4c 4c 4f 20 55 4f 52 4c 44 21 00
    (hmmm... will have to google that to see if I remembered it right.... it's been a few years, I'll bet I got the X and Y registers mixed up and the bne is probably wrong too...)
    I looked it up.  2e should be b9, 90 should be f0, and a8 should have been c8. Still, I think that's not a bad showing, coming at it cold and after a twenty year break.
    MOV     AL,[B900]
    AND [SI],AL
    LOCK
    POP ES
    AND DL,DL
    DEC AX
    DEC SP
    ADD AL,[SI]
    DB 60, 60
    DB 10 DUP (00)
    DB "HELLO UORLD!", 00

    Which crashes my NTVDM. Good job.

    Your x86-architecture CPU has X and Y registers?  Where can I get one?



  • @morbiuswilters said:

    @DaveK said:

    @DaveK said:

    : 0400 a0 00 2e 20 04 90 07 20 d2 ff a8 4c 02 04 60 60

    : 0420 48 45 4c 4c 4f 20 55 4f 52 4c 44 21 00

    (hmmm... will have to google that to see if I remembered it right.... it's been a few years, I'll bet I got the X and Y registers mixed up and the bne is probably wrong too...) 

    I looked it up.  2e should be b9, 90 should be f0, and a8 should have been c8.

    Still, I think that's not a bad showing, coming at it cold and after a twenty year break.

    Riveting.

    The door's right there if you're bored.  Don't let it hit you on the way out. 



  • It's a Commodore 64 program. For those lacking a 6502 disassembler:

    0400: A0 00        LDY #$00
    0402: B9 20 04     LDA $0420,Y
    0405: F0 07        BEQ $040E
    0407: 20 D2 FF     JSR $FFD2
    040A: C8           INY
    040B: 4C 02 04     JMP $0402
    040E: 60           RTS
    040F: 60           RTS

    $FFD2 is the address of the routine inside Commodore BASIC to display a character.



  • @Goplat said:

    It's a Commodore 64 program. For those lacking a 6502 disassembler:
    0400: A0 00        LDY #$00
    0402: B9 20 04 LDA $0420,Y
    0405: F0 07 BEQ $040E
    0407: 20 D2 FF JSR $FFD2
    040A: C8 INY
    040B: 4C 02 04 JMP $0402
    040E: 60 RTS
    040F: 60 RTS
    $FFD2 is the address of the routine inside Commodore BASIC to display a character.
    Ohhhh, nice. Thanks DaveK, for flexing your leet skillz, and Goplat, for translating the Motorola assembler. Here's the x86 program that does the same thing, minus the typo in "UORLD":
    :0100  31 D2 81 C2 16 01 8C C8-8E D8 31 C0 80 C4 09 CD
    :0110 21 80 C4 43 CD 21 48 45-4C 4C 4F 20 57 4F 52 4C
    :0120 44 21 24


  • BINNED

    @DaveK said:

    @DaveK said:

    : 0400 a0 00 2e 20 04 90 07 20 d2 ff a8 4c 02 04 60 60

    : 0420 48 45 4c 4c 4f 20 55 4f 52 4c 44 21 00

    (hmmm... will have to google that to see if I remembered it right.... it's been a few years, I'll bet I got the X and Y registers mixed up and the bne is probably wrong too...) 

    I looked it up.  2e should be b9, 90 should be f0, and a8 should have been c8.

     

     WTF, why did you remember the actual hex codes instead of the mnemonics?

     @DaveK said:

    Still, I think that's not a bad showing, coming at it cold and after a twenty year break.


     Definitely.



  • @TwelveBaud said:

    Goplat, for translating the Motorola assembler.

    Except that 6502 is MOS Technologies not Motorola.


    6809 .. now there was a real Motorola 8 bit micro. Color Computer rocked!



  • @topspin said:

    @DaveK said:

    @DaveK said:

    : 0400 a0 00 2e 20 04 90 07 20 d2 ff a8 4c 02 04 60 60

    : 0420 48 45 4c 4c 4f 20 55 4f 52 4c 44 21 00

    (hmmm... will have to google that to see if I remembered it right.... it's been a few years, I'll bet I got the X and Y registers mixed up and the bne is probably wrong too...) 

    I looked it up.  2e should be b9, 90 should be f0, and a8 should have been c8.

     

     WTF, why did you remember the actual hex codes instead of the mnemonics?

    Couldn't afford an assembler on my pocket money at the time I learned it, so I had to make do with the PET's built-in rom monitor.

    @topspin said:

    @DaveK said:

    Still, I think that's not a bad showing, coming at it cold and after a twenty year break.


     Definitely.

    Ta! >*bows*< 


  • Garbage Person

    You know, I've always found JSP irritatingly hard to look at when it's been coded by IDIOTS. 

     

    <%if (x==11){%><tr><td><img src="/00/keno.gif" width="1" height="15"></td></tr><tr><%}%>

     

    REALLY, GUYS? Just because you need to have the braces in the JSP tags doesn't mean you have permission to disregard everything you've ever learned about formatting.

     

     The only reason this should ever be ANY sort of issue is when the Java indenting conflicts with the HTML indenting (which it actually does here) - and then you just have to figure out which one is more important to you.... And looking at this code, they DEFINITELY don't give a shit about the HTML indenting

     

     

    There's a very good reason I prefer maintaining servlets that print the fucking HTML over JSP. JSP feels like I'm writing PHP with a sane library - and I fucking HATE PHP.

    To ASP.net's credit, I've never seen anyone butcher it (mostly because of a total lack of inline executable code) - if Oracle can turn JSP into that, I might actually change my opinion of them as a company.


  • Garbage Person

    @amischiefr said:

    @DaveK said:

     I don't speak PHP, but I presume you can replace all that hideous inline HTML with predefined named string constants of some kind, can't you?  And then you can refactor it into a real loop-and-case construct.

     

    Good thing too since this is JavaScript.

    This is actually a common construct among those who a.) are new to JavaScript and J2EE in general, and b.) aren't using any frameworks / JSTL / jQuery / JSF (or something that has built in tag libraries for such routines).


     A common misconception among those who are new to Java is that it has any bloody fucking thing to do with Javascript.



  • @Goplat said:

    It's a Commodore 64 program. For those lacking a 6502 disassembler:

    0400: A0 00        LDY #$00
    0402: B9 20 04     LDA $0420,Y
    0405: F0 07        BEQ $040E
    0407: 20 D2 FF     JSR $FFD2
    040A: C8           INY
    040B: 4C 02 04     JMP $0402
    040E: 60           RTS
    040F: 60           RTS

    $FFD2 is the address of the routine inside Commodore BASIC to display a character.

     

     

    Actually that would be inside the kernal. BASIC resides in the $A000 - $C000 block.

    In addition you REALLY don't want to put that code at 0400, unless you've remapped the screen. Cause that FFD2 might start overrighting your code. Talk about self modifying code...



  • @chrismcb said:

    In addition you REALLY don't want to put that code at 0400, unless you've remapped the screen. Cause that FFD2 might start overrighting your code. Talk about self modifying code...

    I wrote it for a PET rather than a C64.  $0400 is the beginning of the basic program area.

    It would still work on a C64, but a) you'd need to clear the screen and cursor down a couple of lines, and be very careful not to scroll it, and b) it would be a bit pointless writing the string "Hello world" to the screen memory in order for it to serve as constant data to be printed to the screen memory!




  • @DaveK said:

    @DOA said:

    This is a project I have to maintain. And by maintain I mean delete parts of it and rewrite them essentially from scratch.

    To be fair, that's pretty much what "maintain" means everywhere.

    Yeah, that's true. But this doesn't happen because existing code tends to actually be bad or unmaintainable. I think the reason so much maintenance takes the form of re-writing is that most programmers are unattached, young males with strong, unfulfilled urges to create something and establish territory. Mucking around in some other frustrated dorklet's Javascript raises a programmer's ire at a Freudian level. It's like having to care for someone else's screaming kid. Of course you would like to leave the stupid kid out in the snow and then go make your own kid, whose screaming you will consider "cute." That's the nature of the human mind and the DNA that comprises it. Fortunately, society enforces the rule that kids are kids, and must be afforded some basic respect regardless of parentage. And I as a decision-maker have similar expectations concerning existing code... maybe your boss will let you throw away your predecessor's "strange" code if you whine about it enough, but that's definitely not my style. I know what makes you tick and I know that many things that tick eventually explode.

    This is one of the many reasons that when I look for a good programmer, I look for someone as well-adjusted and straight-laced as possible. If you know Eric Cartman's middle name or how Ruby resolves the "diamond problem," we're probably not going to get along. And I definitely look for programmers with other, um, outlets for their creative frustrations.

     



  • @OzPeter said:

    6809 .. now there was a real Motorola 8 bit micro. Color Computer rocked!

    Amen... I still play with my Color Computer 2. Probably the coolest thing I've done with it recently is to code optimal line-drawing algorithms using the four-pixel graphics characters that are available in text mode. Developing the algorithms was hard. It's basically Bresenham's algorithm re-written to consider the availability of various 4-pixel move instructions. But the 6809 assembly code is remarkably easy to write and hand-assemble (my program takes the form of BASIC DATA statements with ML expressed in decimal.)

    Even more recently, I wrote something like a terminal emulator for the CoCo 2, again in 6809 ML. It's not a full emulator because it only receives data, but the cool thing is that I made up a protocol for cursor positioning (similar to the "ANSI" color terminal protocol). This happens at 9600 baud, so I can blast out mixed low-res graphics and text with surprising speed.  And it doesn't have to be a PC or even a microcomputer blasting out the data feed... a $2.50 PIC chip will work fine, and can modify (i.e. animate) the feed in response to all sorts of inputs, e.g. a Tandy deluxe joystick wired to its analog inputs.

    People say they learned nothing in college, but having spent 5 years elbow-deep in 68000 assembly does seem to have helped my CoCo skills considerably. It's not like I was doing this kind of thing as a kid (and nobody at Radio Shack knew how to do it either.)

    And every time I think about how IBM decided not to use the 68000 for their PC, it makes me want to cry. What in the hell were they thinking? Back then I wanted a PC or an Apple II just to have access to the associated software. Now, I don't touch anything PC-based unless I'm being well-paid to do it. The CoCo seems like a superior machine to my mind. I mean, at least the few games it did run weren't renderd in cyan, magenta, black, and white.

     



  • @DaveK said:

    @DaveK said:

    : 0400 a0 00 2e 20 04 90 07 20 d2 ff a8 4c 02 04 60 60

    : 0420 48 45 4c 4c 4f 20 55 4f 52 4c 44 21 00

    (hmmm... will have to google that to see if I remembered it right.... it's been a few years, I'll bet I got the X and Y registers mixed up and the bne is probably wrong too...) 

    I looked it up.  2e should be b9, 90 should be f0, and a8 should have been c8.

    Still, I think that's not a bad showing, coming at it cold and after a twenty year break.

    Damn it, why do I look at that and think you've wasted a couple bytes?  And that second line's all caps.  Ever hear of the shift key?



  • @Goplat said:

    It's a Commodore 64 program. For those lacking a 6502 disassembler:

    0400: A0 00        LDY #$00
    0402: B9 20 04     LDA $0420,Y
    0405: F0 07        BEQ $040E
    0407: 20 D2 FF     JSR $FFD2
    040A: C8           INY
    040B: 4C 02 04     JMP $0402
    040E: 60           RTS
    040F: 60           RTS

    $FFD2 is the address of the routine inside Commodore BASIC to display a character.

    As said, CHROUT ($FFD2) is in KERNAL ROM, not BASIC ROM.

    Does PET have a null-terminated-string-output routine in BASIC ROM? Commodore 64 has one in $AB1E (sorry, non-l33t symbolic assembly):

    MAIN: LDA #<TXT
        LDY #>TXT
        JSR $AB1E
        RTS
        TXT: .asc "HELLO WORLD!",13,00



  • @tgape said:

    @DaveK said:

    @DaveK said:

    : 0400 a0 00 2e 20 04 90 07 20 d2 ff a8 4c 02 04 60 60

    : 0420 48 45 4c 4c 4f 20 55 4f 52 4c 44 21 00

    (hmmm... will have to google that to see if I remembered it right.... it's been a few years, I'll bet I got the X and Y registers mixed up and the bne is probably wrong too...) 

    I looked it up.  2e should be b9, 90 should be f0, and a8 should have been c8.

    Still, I think that's not a bad showing, coming at it cold and after a twenty year break.

    Damn it, why do I look at that and think you've wasted a couple bytes? 

    Hey, I probably did.  I was a bit limited in my opcode choice by what I though I had a chance of remembering... 

    @tgape said:

    And that second line's all caps.  Ever hear of the shift key?

    Uh, yeh.  That's that thing that gets you the graphic symbols, isn't it?

     

    What do you mean there are two cases?


  • @WWWWolf said:

    As said, CHROUT ($FFD2) is in KERNAL ROM, not BASIC ROM.

    Does PET have a null-terminated-string-output routine in BASIC ROM? Commodore 64 has one in $AB1E (sorry, non-l33t symbolic assembly):

    MAIN: LDA #<TXT
        LDY #>TXT
        JSR $AB1E
        RTS
        TXT: .asc "HELLO WORLD!",13,00

    There certainly was one, but I couldn't remember the address.  Also, there were enough different PETs with enough ROM variations that trying to call anything except the guaranteed vectors at the top-end of memory was bound to go badly wrong on some or many models.  IIRC the C-64 BASIC ROM stayed pretty much constant throughout its life.

     



  • @bridget99 said:

    @DaveK said:

    @DOA said:

    This is a project I have to maintain. And by maintain I mean delete parts of it and rewrite them essentially from scratch.

    To be fair, that's pretty much what "maintain" means everywhere.

    Yeah, that's true. But this doesn't happen because existing code tends to actually be bad or unmaintainable. I think the reason so much maintenance takes the form of re-writing is that most programmers are unattached, young males with strong, unfulfilled urges to create something and establish territory. Mucking around in some other frustrated dorklet's Javascript raises a programmer's ire at a Freudian level.

    Primate territoriality instinct is a terrible motive for anything that should be an engineering decision, agreed.  That's why my first suggestion was refactoring it a bit so it could be better assessed and a keep-and-fix/throw-and-rewrite decision made on a more informed basis.  But I think you're too kind, as well.  A lot of maintenance actually is because the existing code actually is completely bloody awful, illegible, semi-literate, and both designed and implemented without showing the least evidence of any knowledge whatsoever of the principles of algorithms, computer science and program structure.  And that's being diplomatic about it.




  • Wow, you guys all had assemblers for your C64?

    All I had was a basic loader and a bunch of 8-bit ints:

    10 for a = [startloc] to [endloc]
    20 read d : poke a, d
    30 next a
    40 sys a
    50 data 34, 255, 12, 61, 98, 87, 110, 205
    ...
    500 data 7, 43, 128

    saved on a cassette, with a sheet of paper as my IDE!

    Give me a break, I was 9 or 10 at the time, and had no knowledge of such advanced tools. Still, at least I stuck with it, and made an especially abysmal mario clone.


Log in to reply