The Art of Cleanliness

I remember when I first looked at this code. There was so much repetition that I thought the previous developer’s Ctrl, C, and V keys would be worn to nubs. Method after method of the same scaffolding code, the same setup and breakdown, with only the core or “meat” of the functionality a mere couple of lines.

Code clutter is the same as work space clutter. It cuts into your ability to efficiently get the job done, be it visually tracing through a hunk of code or finding that “zen” moment when writing a paper. Clutter is a distraction, and the same applies to code. Bottom line is: the less time I spend hunting down the code I need to change, the quicker I can be done and have a beer.

My philosophy is: if you copy and paste it once, you’re doing it wrong*.

*Now, obviously I don’t mean copying and pasting a method name to re-use! No, no! In this context, I mean a method body, or Enum, or set of declarations.

A fellow developer that I worked with (who unfortunately was laid off, among others), and I had spent a lot of time re-factoring much of this code to reduce redundancies. We made great use of C# lambda methods to wrap the meat of our code into an anonymous method inside of the body of our calling method. Here’s an example.

delete old

This is an example of a piece of code that sits in the data layer and will invoke a SQL stored procedure. In this case, it’s to break up two accounts that are linked in our system, but that’s irrelevant. Now, the method by itself isn’t bad, and the setup and breakdown is justified. The problem is, EVERY method has the same set of scaffolding. What we want to do is simplify this to make just the code needed by the method visible, with everything else hidden, behind the scenes. Below is the result of this same method.

delete new

How we got here really isn’t difficult. We merely utilized an anonymous method (via a lambda) to sort of “expose” just what we need.

Below is the code that does the heavy lifting. This isn’t the exact method, as there’s a couple layers of overloads (ExecuteCommand is wrapped by ExecuteProcedure, as well), but it ends up here:

exec cmd

As you can see, it looks very similar to the original method. However, EVERY method that invokes a SQL stored procedure can now utilize this scaffolding code. Not only does it reduce the sheer amount of code visible to a tired developer, but eliminates errors caused by copy/paste errors and typos. Note that we also removed the nested try/catch, as it wasn’t needed, and removed some unneeded declarations.

Now, this is just one example of how some light housekeeping can bring down the level of headache when writing and debugging code. I find utilizing lambdas to be a very powerful method of removing clutter from your code base.

My first dynamic website

Yesterday I wrote about coming across an old website of mine from 2006. While I have yet to find the source code for it, I DID happen to come across my very first ASP website from 2004.

One feature is the Access database has an “admin” login with a blank password. Security be damned!

The site itself was a VERY basic message board site that would let users sign up and post topic on the page, as well as links, photos, and jokes. Even the hosting interface has not changed after over 6 years.

Finding this site gave me the opportunity to see how far I’ve come as a developer. In the last 6 years, I’ve moved from classic ASP, to PHP, to ASP.NET.

The source code (minus the database) is posted below. It’s embarrassing.

Download Messagebox V1

Web memories

I took a break from the slower-than-usual day at work to take a look at Internet Archive (web.archive.org), which, as the name implies, archives web content over time. This includes not only sites, but images, scripts, etc. It’s an awesome non-profit project that is perfect if you need to find an extremely old blog post.

For shits and giggles (more giggles), I punched in my old website, ryanpetersonline.com, and immediately was pulled back in time; back to 2006. Everything that was relevant at the time – everything from talking about my first home server to our new (now aging) couch – was there, spanning about 1 years time. After reading through the wide-range of topics I ranted and wrote about, I realized that I sort of miss blogging.

Maybe I’ll get back into it. I’ll start by adding these archived posts to THIS blog. It’ll be funny to pull these up 50 years from now and see what was relevant, assuming the internet is still here and we’re not all floating in cloud (web joke).

All things Connor

Wow, it’s been a LONG time since I posted here. There’s good reason too – busy job, busy house, and of course, our first son Connor was born on August 19th. I’ll be making a lot of updates, but not so much tech-related.

On that note, I encourage you to check out my newborn son’s website, connorpeters.com.

It’s an early build, but still in development

I took the day off (well, it was supposed to be the morning, but we ran into complications) to visit the OBGYN with my wife in order to get a checkup on our collaborative project. Here’s an early screenshot of the latest build.

In about nine weeks we should be able to find out the sex. At that point, I can begin registering his or hers domain name.

Quick module creation using TouchPointCMS

Back from vacation. I took a week off in OBX to get my eyes off computer screens for a while. We didn’t have WIFI down there, which turned out to be a good thing since I was able to relax and only answer a minimal amount of work-related email (they had 2 PCs on site).

Anyway, I’m very excited about TouchPointCMS development. It’s looking to be a very promising and solid CMS with endless add-on functionality. It’s also amazing how much faster this version runs over the old adhoc’d versions. One of the last updates to TouchPointCMS was improving the caching mechanisms used throughout the site in order to decrease load times and save on server resources.

touchpoint_modules

I’m also finding that writing add-on modules now is incredibly easy. For example, we’re deploying TouchPointCMS for smart-spice.com and they want to include a spice manager. We can do this pretty quickly, considering a basic module consists of roughly 3 core files (5 if using code-behind):

  • The user control that the end user sees (.ASCX and .CS)
  • The user control that the CMS user sees for management (.ASCX and .CS)
  • A meta.config file for SQL related to installation, deletion, generating instances, icon, etc.

Essentially, that’s all you need for building a module. For the spice manager, I combined functionality from the included Blog and Photo Gallery modules to allow for a simple spice list manager with images. The front end user control is nothing more than a ASP.NET ListView control bound to the spices for that page.

The modules that you create are automatically loaded with the web page. If you want to customize the placement of the module, such as having a Events module off to the left or right sizes of your page, you can deselect the Render Inline Module option from the CMS. This will disable the automatic rendering of the module, allowing you to use the <tpc:Event runat=”server” /> instance to create a list of events wherever you like on your template file. If you want to use events from another page, just include the PageID=”6″ attribute, where 6 is the page ID you wish to pull from.

Check back later for updated CMS development and release!

Learn more about TouchPointCMS.