Showing posts with label Bullshit Code. Show all posts
Showing posts with label Bullshit Code. Show all posts

Monday, March 28, 2011

More Bullshit Code!

This code is actually from another project. I'll eventually use it in the troll project. It's kind of simple but it's just so damn awesome to me from a geek perspective:

Click the picture above to see it better.
What does it do?

Answer: Anyone who has been around long enough to have suffered through writing a report in COBOL may remember the ancient concept of a "control break" - simply the point where upon reading a record you test if the condition has been met to print a sub-total, accumulate a grand total, etc. In this code, I've got a bunch of files in a directory, I select the files only (excluding anything else like subdirectories) and sort those files by their date/time stamp. Then I produce a report of a tally of files by the day they were placed in this directory. The "control-break" happens right in the middle of the code snippet above - the "if" clause. Could you ever "push" something in COBOL? The end result of this code is an array of hashes. Any number of table formatters (like the one I use for the troll project) can spit out something halfway readable. Like so:


The geek value is in how, with so few lines of code these days, you can express something that required so much more sweat in the bad old days. What is even funnier is that you could have done something like this even back then - with Lisp. I wonder if some shop somewhere in those days used Lisp for mundane data processing. Probably only a handful - memory cost a huge amount of money in those days.

I was watching a talk by Ruby superstar Aaron Patterson and he said this is much faster:

Click on the above to see it better.
Inject is expressive but slow apparently. I haven't had a chance to benchmark this particular code.

Inevitable humbling update: I was watching another videocast and what I'm doing above has a simple name: a histogram. The vidcaster of course had a butt simple way of doing the above. No fancy array of hashes, pushes, maps or injects - just one hash:

Click above to see it better.
Hashes are ordered in Ruby 1.9.2. so the end result comes out ok. Older rubies take a bit more work. You learn something new every day.

Tuesday, January 18, 2011

More Bullshit Code!

In the last installment we looked at some code that implements the "added view column" (ac) feature of my column search program. I demoed it in the comment threads here.

The column search (hacols) is the program whose output I often post in the comment threads. One day I needed a convenient way to assemble a url to a specific comment from parts I had already been collecting and thus the "added view column" feature to hacols was born. The feature allows me to develop a short one-liner program on the command line and then once it's working, store it to be recalled for later use. It becomes just another column (albeit unsearchable) for views of the database. So far I've used added view columns to generate comment urls and to downcase and strip out html from comment bodies. Check it out:

Click on the graphic above to see it better.

Blogging this code led me to thinking. Counting the results of a search and executing view code on those results is at present an either/or. One bit of code that executes later is the "uniq" feature which filters out redundant results. What if I wanted to count the unique results of a search like all the variations on Puddybud's handle in the comments file? Uh.. I can't do that with this code. And yikes! What's with copying tr to a results array? Why not update tr in place? What a waste! And sigh, it makes sense to do that inject only when adding columns at the command line not every time we convert the vetted code strings to procs for view execution. Ditto for storing. How humbling...

So the big if statement drops to the cutting room floor and the following refactor is born. Here:

 and here:


What a difference a little time and blogging makes. Brand new code next time.

Update: Found yet more optimizations for larger result sets. I won't discuss them. I promise.

Sunday, January 9, 2011

Bullshit Code!

During the back and forth in the comment threads over the tragic shooting of Congresswoman Gabrielle Giffords (among others), Puddybud went off on yours truly:
You wasted all those unemployed months telling the unemployment office you were looking for a yob while writing BULLSHIT code!
Bullshit code??? Hey those are fighting words!


Can Puddybud or anyone else tell "the world" what this does?

This will be the first of another occasional series. Not once a week like the "Bird's Eye View" but...  you get the idea.

Update: no takers yet.. (Puddybud dumped on it, of course.) In case you didn't notice, that if statement above is dangling. So I'll complete it and we'll see what happens:


Hint: I actually demoed what this code does some time back - in the comment threads.

Yet another update: Wow! I'm glad I blogged this code. Kind of forced me to look at it more closely and I've already re-factored it some to yield just a bit more functionality. Details to come in the next BS Code post.