Best Practices in Development

Over the last few days I've been working on a relatively large change to my image linking logic. This change involved some reorganization on the server side, adding name-spacing, changing the url structure, and placing 301 redirects to all 1200+ of my photos. I made this update both in hopes of keeping some relative SEO content on the photos linked in my blog and because I wasn't happy with the original implementation (especially the data storage). This post isn't about the update, though. I wanted to talk a bit about some of the principles I used on this and other projects I work on.

Incremental Changes

This update was a rather large one to my engine. I needed to change more than just a few image links - I upgraded my entire image storage system to a relational setup. Eventually this will turn into a dedicated photography site (photos.jacobemerick.com, mayhaps). Trying to change everything and push it live at once would have been asking for disaster.

Instead I broke down this change into five steps. Each step could be tested ...

read more »

Abstract Form Handling

I've already gone over some basic form handling and form best practices with my last few posts, but building with an object-orientated MVC starts to throw forms into a different light. It's very easy to abstract out forms with their repetitive logic patterns, something that I've recently done on one of my side projects. While I don't want to explain the code line-by-line, this post will go over some of the basic thought processes and steps I took to make my abstracted form handler.

Form Wrapper

The first logic I worked out was the definition of a form. To create each new form, I create a new class that is abstracting off a base pattern. Each form class defines form elements within a standard 'get' method. This way I can call on individual form elements easily or call the entire class to pull the whole form. Example...

read more »

How Many Tiers for a Clean View Layer?

An interesting possibility cropped up for my MVC a few weeks ago. I've been working with microformats on and off the last few months (you can read about one of my first adventures with them when I created a microformatted resume). I started to wonder where else my site could use this new feature. An easy place to add microformat tags are links - you can add a 'rel' attribute that describes the relationship of the link to your site. Also, if I added a layer of abstraction to my links site wide, then I could easy add behavior rules (target="_blank" and whatnot) that would affect an individual link across all of my pages and sites.

This idea would involve creating a table of all my links, internal and external, that could be mapped to meta information about the links. There would be a helper class on my site that would pull a link based on an alias, give it attributes based off of the meta data (including microformatting), and spit out a final link. No where on ...

read more »

Thoughts on Data Abstraction

Something that I've been working on a lot with both my work and personal web programming is data abstraction. The opportunity to work with several different handmade php frameworks has given me different ideas on what an effective data abstraction system should have. Without formal education, though, this post will be only based on my hands-on experience with the functionality I find myself using without true design patterns.

Data abstraction is a fancy word for treating the data as a separate entity from your web application logic. Web sites usually store their data in databases, using customized queries to pull information for their code to output and manipulate information. It doesn't matter what type of database or language you're working with; data abstraction can be employed whenever there is interaction with data (this post will be approaching this from a rough PHP - MySQL angle). Complex sites often have numerous queries scattered throughout the web site, and one needs to have intimate knowledge of the database structure during the ...

read more »

The Problem with Assuming

In the days of old (PHP 4) web programmers sometimes resorted to small shortcuts in their code. One of these was register_globals, which extracts the global arrays ($_REQUEST, $_POST, etc) into individual variables, allowing a programmer to write $name instead of $_POST['name']. This is an obvious security flaw - users can easily pass in custom parameters to overwrite variables in the code and cause unforeseen problems. In the below example, if register_globals was turned on in the PHP configuration, a tricky user can pass in a 'access' parameter and do some horrible things.

if($level == 'admin')
{
  $user = 'admin';
  $access = 'everything';
  $restrictions = 'none';
}

if($access == 'everything')
{
  // do some horrible things here
}

This is a good example of a bad programming practice - assuming that things will work the way you expect them to. That is, you assume that a level of 'admin' is the only way the 'access' variable could be set to 'everything'. The security-orientated approach would be to ...

read more »

Cliffs of Mulligan Plains

The -15ºF air hit me like a brick wall as I stepped outside of my car at Mulligan Plains. Unaccustomed to the cold Upper Peninsula mornings I quickly covered my nose and mouth with a warm scarf before heading down the plowed two-track. The sun's early rays had only just begun showing over the eastern cliffs and I didn't have time to wait for the valley to warm up; there was a long, hard hike before me.

Last summer marked the first time I visited Mulligan Plains without stopping by Mulligan Falls. I had always known that there were other attractions in the narrow valley but had never made a direct effort to see them until then. The main goal of today's trip was to revisit one of these sights: Rocking Chair Lakes. My route wouldn't be an easy one but did a promise some awesome views of the plains. I planned to start near the bridge over lower Mulligan Creek, follow the top of eastern cliffs to Rocking Chair Lakes before looping back to the car, resulting in a 6 mile hike with plenty of vertical distance change.

The first climb of ...

read more »

What's With the Big Pause?

When I first decided to create an online blog I wanted to avoid a common pitfall of many other blogs: lack updates. While there were a few empty months in 2009, it feels like a did a good job with regular posts every week or so... Until the New Year rolled around. This is the first blog post in over six months.

Starting in mid-November a few things changed that really impacted my regular blogging. The first, and most obvious one, was winter. While I do enjoy winter adventuring, driving to the Upper Peninsula through the snow to come up with fresh content for the hiking side of my blog was something I decided to avoid. That, coupled with decrease daylight and other seasonal blues, dampened my motivation to write new hiking blogs.

Another problem that I ran into was a decrease in time. Not only did I start working later hours in November at my full-time job, but I started a large contract project that ate up any free time I normally reserved for my personal site development. My site was half rewritten at this time - I was starting to ...

read more »

Intro to SQL Part D

For the last post in this short series on SQL I wanted to explain some more in-depth usage. A technique that I wish I would have started using in my early websites is a centralized script to handle all of my database calls. PHP does have some great functions built in to connect, read, and write using SQL statements (check out mysqli), but it's easy to start copying the same eight to ten lines of code every time you want a piece of data from your tables. A better approach is to handle the queries with a centralized class.

Before anything can be read or written to a table with a SQL statement, you need to create a connection. Your website will be signing as a user with specific privileges. A good technique is to make two users, one that can read and one that can read and write. By using the former user as much as possible and taking extra care with the latter one, you will minimize the possibility of hackers uploading malicious information or dropping whole tables.

Once a connection is made you can start having fun with the database ...

read more »

MVC CSS Stylesheets

While working on redoing my website using a custom model-view-controller framework I decided that it was a good time to do some extra cleanup. After all, cleaning up the backend only changes the content portion of the site. There's much more to websites - styles, behaviors, cache control - then the content. In the interest of cleaning up my website I decided to take my MVC framework to the next level and use it to create my stylesheets and Javascript in real-time.

The primary benefit of MVC frameworks is to simplify code logic and remove duplicate code. With all user requests being transferred through the same routes and controllers, it's easy to whip out extra html pages or make site-wide changes by editing a few lines. Common SQL functions, AJAX calls, and classes can be used across different pages and sites easily without duplication.

There's a lot of duplicated code in my assets. I use Erik Meyer's reset stylesheet, a global set of style rules, jQuery, similar lightboxes, etc across my different subdomains. I could include each ...

read more »

Website Overhaul

It may be no surprise to any of my long-term visitors, but I've decided to perform a deep overhaul of my website. Based partly on my recent introduction to model-view-control frameworks and partly on research into web caching and optimization, I've spent a good chunk of time analyzing my current and future website functions in the hopes of streamlining the backend code and optimizing the frontend. There are several goals that I have in mind for this overhaul, most of which will stretch my current programming skills.

Model-View-Controller Setup

The idea behind this sort of programming is breaking everything up into separate, reusable objects. All requests to the website are directed to a primary router, which interprets the request and pulls a controller to handle the page. A controller is a set of instructions based on the page type (example: home page, blog post pages, etc) that sets variables for the final page. The controller may talk to a model, which is the 'logic' for the site (usually a database object), and pull additional ...

read more »