My Favorite Line(s) of Code

April 3, 2013

Onwards to Round 2 in our current blog battle here at SEP. This time I’ve been asked to write about my favorite line(s) of code.

Unfortunately, I don’t have some fancy Ruby, Haskell, or Clojure to dazzle you with. My recent tenure spent managing projects leaves me with little time to focus on the latest and greatest coding techniques. And most of the code I see these days is what has been written by others.

But bear with me, and join me in the way-back machine…

25 years ago

In 7th grade, my school started computer classes. These were using a couple Apple computers (I think they were Apple IIgs, but not sure) and luckily for me a dozen Commodore 64s. Why was that lucky? Because about a year before, my parents had gotten us a Commodore 64 for use at home. And in that year, I had devoured every BASIC programming book that our local library had on their shelves, and I had scoured their computer magazines as well, looking for coding examples. While I was definitely no world-class programmer by that point, I did know quite a few tricks. And it was great getting to show off a little to my friends who were just learning what to do, as well as helping then easily exceed what we were supposed to be doing. My favorite code from that time (a piece that I can remember even now)? A simple loop:

10 FOR X = 0 TO 15
20 POKE 53280, X
30 POKE 53281, X
50 GOTO 10

So what does that do? Well, it quickly cycles the screen border and background colors (for the time, a very cool visual affect given how simple the code is). I have a feeling though that if I’d have done something like that in today’s schools I might have gotten suspended or expelled for this code due to fear of triggering seizures.

Now let’s step forward a few years to…

20 years ago

Onwards to high school! Now my friends and I were dealing with some real machines: 386 and 486 towers. And with them, some newer computer languages as well. This was mostly Pascal and C, but I also got to pick up some Prolog! Two pieces of code stick out in my mind from this time period.

One of these was a simple drawing program that I wrote for a class. The best part about it was the little competition amongst my friends. We easily got the basic functionality done (drawing rectangles and circles and such). So we decided to go all out, adding color selection and creating silly logos and fake company names. While none of our programs even approached the complexity of MS Paint, we were all proud of what we had accomplished on our own.

The other that comes to mind from that time was a simple screen saver that I came across that had source code included. While the original code made a nice “falling rain” pattern, I found that with some changes I could make it look like dripping blood, flames shooting into the air, or a snowstorm blowing across the screen.

Unfortunately, it appears that my copies of this code never made the transition off the old old 5 1/4″ floppies that they were on at the time. So until I can find a way to get to them they are currently lost to the ages.

Let’s jump again…

15 years ago

Here we are with me well into college. Over the years I picked up more C as well as learning C++, Java, and a variety of assembly languages. As with high school, a couple of projects really stand out in my mind from this time frame.

One was my senior project where we simulated the effect of line noise on dial-up modems. We were not only simulating existing modem technology but also simulating some theoretical higher-speed configurations. At the time this felt like some cutting-edge work. Despite how much better cable modems and DSL connections are for usability reasons (and speed), a little part of me is still saddened that I never got to see faster dial-up modems come out as a result of that work.

The other piece of work that comes to mind is a project from a computer graphics class. The assignment was fairly straightforward, we simply had to show off some of what we had learned in class. So what did my group decide to do? We implemented a first-person “shooter” walk-through art gallery. Think of a simplified version of “Doom” without the combat aspects – instead just walking around looking at pieces of artwork. We even included the subtle bobbing motion as you walked around. I still remember the hardest part of this project – getting the calculations right for the wooden frames around the pictures (lots of compound angles and making small adjustments until things looked right).

Unfortunately, much as is the case with my high school coding, I never transfered either of these from the old 3 1/2″ disks that they were on. So they too seem to be lost to time.

Time for another step forward…

10 years ago

By this point I’ve been at SEP for a few years. I’ve worked on some VB, some embedded C++, and Perl. Ah yes, Perl. While frowned upon by many today, there were so many things it let me do simpler than before. In particular, with Perl came my introduction to regular expressions, which are still one of the most useful tools I’ve come across in computing.

For example:

$_[0] =~ m/(\d+)d(\d+) choose (\d+) \((\d+)\/(\d+)\)/i;

What’s that for? It takes an input in the form of “4d6 choose 3 (6/7)” and parses it out so that the program knows:

  • how many dice to roll (4)
  • how many sides the dice have (6)
  • how many of the dice to keep after rolling (3)
  • how many statistics to roll (7)
  • how many statistics to keep (6)

And that is the first step in a simulator to compare variations of dice rolling for an RPG character generator. At that point the simulator would run a thousand samples of the rolls and compare the results to the started character creation process of D&D 3.0 to see whether the results were more powerful, weaker, more consistent, or more extreme. This is something myself and several of my friends had been having debates about at the time this piece of code was written, until finally we decided to generate some data to end the debate (and try out some combinations).

Into the timestream again…

5 years ago

Whew, have I really been at SEP for 9 years at this point?
I’ve been dealing with Perl and SQL for many years, and have started on C# as well. Ah SQL, it seems like lately several people out there have a hatred for you as well. But I love how simple you make it to perform set manipulation of hierarchical data.

update tblThings set Value=pNewValue where oid in ( select oid from tblThings start with oid=pThingId connect by prior oid=ParentThingId );

There you go, change the Value of the Thing identified and all of it’s children to the new value. Things like this came up fairly often with a project that I spent a good portion of my professional coding career on, so I quickly came to appreciate how easy SQL could make this. And manipulating a million rows of data with a single statement just gives a great feeling of power.

Now for a final step forward in time…


As I mentioned at the top, most of the code I see these days is written by others. And while what I see often amazes me at the time, it rarely sticks with me. I do get asked to help debug some code from time to time (as once you’ve done it for a while you can apply it to just about any language). Occasionally I get to help write a particularly tricky bit of SQL. But the code I get most deeply involved with now is usually at SEP Startup Weekends. My favorite code from those? A little bit of image processing work:

var qrs = GetRectangleLocations(image).Select(r => InflateFromCenter(r, aroundRectangleBuffer, imageBounds))
.Select((r, i) => new { Cropped = Crop(image, r), Rect = r, Int = i })
.Select(c => new { Decoded = DecodeQr(c.Cropped), c.Rect, c.Int, c.Cropped })
.Where(c => c.Decoded != null)
.Where(q => q.Decoded.BarcodeFormat == BarcodeFormat.QR_CODE)
.Each(x => x.Cropped.Dispose())
.Select(q =>
QrID = q.Decoded.Text,
ActualX = q.Rect.Left + q.Rect.Width / 2,
ActualY = q.Rect.Top + q.Rect.Height / 2,
.Each(x => Console.WriteLine(x.QrID))

So, what is this code doing?

  • Start with an image and find all the rectangles on the image.
  • Crop out each of the rectangles into their own separate images (with a little bit of buffering around each side).
  • See which of those rectangles have QR codes on them and decode the QR code (getting rid of things that aren’t QR codes).
  • Create a list of new objects containing the decoded QR codes, their rectangle coordinates, and the “center” of their rectangle.

Sure, it isn’t the prettiest code, but keep in mind that Startup Weekend lasts about 48 hours. Not only that, but we had a technical issue about halfway through the 48 hours which caused us to lose all of our image processing code (as well as our team member that was working on that code). So there we were coming down to the wire, and we had to create the image processing code from scratch (this piece along with all the functions that this piece calls). And by the end it worked.

Final Thoughts

Maybe by now you’ve picked up on something about my favorite lines of code. The lines themselves aren’t the most important thing. My favorite lines of code are my favorites due to the stories that go with them. Think about your favorite lines of code and what stories go with them. And please share in the comments below.