?

Log in

No account? Create an account

t3knomanser's Fustian Deposits

My Favorite Way to Fix Broken Code

How Random Babbling Becomes Corporate Policy

run the fuck away

Mad science gone horribly, horribly wrong(or right).

My Favorite Way to Fix Broken Code

Previous Entry Share Next Entry
Megaweapon
DON'T USE IT!

The app I'm working on has a "Paging" system. You know what I'm talking about- when results come back, it only displays x results per "page"? With all the links to click to go to the other pages? Right, well the application does this... interestingly. So interestingly, it's broken.

Without going into the details and boring the non-technical folks, it takes an interesting approach. Most applications would do something like this:

APP: Hi there database. It's me again.
DB: What do you want this time?
APP: Oh, nothing much- mind just giving me all the products that ship out of Cleveland?
DB: Is that all?
APP: Well, actually, now that I'm thinking about it- I don't really need all of them. I mean, I will at some point, but for now, could I just get the first ten?
DB: Look man, I'm an Oracle Database. We don't make that sorta shit easy, y'know. This isn't some SQL Server that lets you do just a top-n. You ready to do some PL/SQL magic to make this happen?
APP: Sure!

It's a little tricky, but entirely workable. It requires a few minutes on Google to learn how to trick Oracle into returning subsets of a query. The app I work on takes a very... special approach.

APP: Hi there database. It's me again.
DB: Oh shit, here we go.
APP: Yeah, great. Um, look- could you tell me what query I need to run? I dropped that into a table someplace.
DB: Okay. Are you just going to go and run that, then?
APP: Well, no. I need to append some stuff to the WHERE clause to filter- I only want the first ten records.
DB: I can do th-
APP: So, anyway- I'm going to skim through the data to figure out what where conditions can be used to break this result set up into 10 record chunks.
DB: Umm...
APP: Then, when the user pushes buttons to go to the next page of data, I'll run a query with those conditions added to the WHERE clause.
DB: How are you going to pass that back? Javascript and a POST form submission?
APP: Oh, no! That's a silly way to do it. No, I'm going to pass it back as a URL parameter. I'll just put it right up there in the address bar.
DB: Oh. That's... um, you are going to escape the characters, right? I'd just hate to see you confuse a browser by passing slashes or something.
APP: Oh, of course. For example, all of the vertical pipes "|" can become "____". That means the SQL string joining thingy becomes "________"
DB: Uh... I meant unicod-
APP: And parentheses can become "L_BRACKET" and "R_BRACKET".
DB: You do realize you can only pass a certain number of characters back as part of your URL?
APP: Oh, it won't get that long. I wouldn't have any paging criteria that complex. Tee-hee.
DB: Suuuuure.... Okay, but what happens when you have records straddling the condition? Like if they have the same supplier or something? Aren't they going to fall between the pages.
APP: Ummm...
DB: I mean, I wouldn't want you to lose any data or anything.
APP: ...


This is about where I entered the conversation. The way the paging system is set up is crazywackyfuntimeohmygodbearisdriving. I tried fixing it. I spent the better part of a day elbow deep in this steaming pile. Simple changes would fix the problem in question- but break on every other dataset that comes back. I couldn't just section the paging logic off and work on just that either- it was intimately tied with ALL SORTS of other functionality (modularity is for the weak willed).

I was pretty frustrated on Tuesday, trying to wrestle with this. And then it came to me- I didn't need to use paging! At least, I didn't need to use the app's paging system. Instead, I would let the app send back the whole data-set (from my inspection, this numbered hundreds of text-only records at most). Instead of working with these weird ass WHERE clause hacks, I'd just pull the whole dataset out of the DB, slap some CSS and Javascript together to do all the paging logic in a far more intuitive way. It's also modular and reusable, which means any other screen that breaks the paging system can be easily ported to work with my Javascript. If we so desired, we could abandon the current paging system entirely, or simply reserve it for places where the datasets would be too large to manage if we returned them all at once (I don't think that happens).

I'm really enjoying this, despite all the "What the hell were they thinking" bitching, this is really fun. At the end of the day, I can say that I accomplished something. There's a useful product from my labor- something "tangible". It's a nice feeling.
Powered by LiveJournal.com