Don’t be stubborn! Choose the right tool for the job

Posted by Jack Altiere on August 3rd, 2006

I’m sure everyone knows a developer like this. No matter what the task or goal is, they always use the same toolset. The question I’m going to pose is why? It’s my contention that programming languages should be like tools in a toolbox. You can have the nicest hammer in the entire world, but it’s not going to do much for you if what you need to do is to put a hole in a piece of wood. This is one reason that I like learning new programming languages, some languages are just better suited at specific tasks.

I’ll give you an example. I was involved in a software project that went something like this. We downloaded a third party library in PHP, but it didn’t have all the features that our team needed, so we decided to engineer our own. It was going to be a web application, so we stuck with PHP. The project also was a great candidate for object oriented design, so that is how we developed our package. Now lets fast forward a little bit. Our library worked great, but we were starting to see some performance issues because of large data sets, so we decided to port this to a compiled language. We had some people suggest C, because….well they know C. Don’t get me wrong, C is a great language, but C++ would be a much better choice. We could keep the object oriented design and just port the existing code for the most part. The argument I got back was that we could just use C and use function pointers and structures. Sure. That’s a great idea. We could also try to dig a ditch with a spoon. Why not use the tool best suited for the job? This whole “fit the square peg into the round hole” philosophy is enough to drive me insane.

My favorite programming language is C#, but if I had to write a web application on a Linux server I sure wouldn’t install Mono on it to do it, I could very easily use PHP instead, because in my opinion it’s a better tool for the job. Some of you are probably saying to yourselves, “you’re just stating the obvious here”. I wish that was the case…I’ve seen this phenomenon way too many times. We all have our favorite languages and tools, but I won’t blindly defend my choices without at least considering the task at hand, and whether or not something else makes sense in this situation.

Another problem I see is taking an existing piece of software, and using it again in another application. Before you say it, I’m not going crazy……let me finish. Generally this is the GOAL of a piece of software, but don’t reuse stuff for the sake of reusing it. Make sure if you are going to reuse something that it actually accomplishes the goal, and does it well. Don’t just bolt on a crappy library because it kind of does something that you think that you need. Just because one of the things that library you wrote back in 2001 did was dynamic image manipulation doesn’t mean it’s a good fit for your web application today.

But it’s not standard!!


This one is harder for me, because standards are a good thing. BUT, if you are going to have standards they need to be kept up to date, and a full range of tools should be available. I’ve run into people who are working on projects using languages that they know aren’t the best choices. They are forced to do this because the companies they work for won’t allow any other tool to be used. Your just putting yourself in a position to fail in this case.

So change the way you design your application. In fact, don’t even choose a language until you have all of the requirements. Figure out your use cases and requirements, then talk about the pros and cons of different languages. Also, if you have some down time, try a new tool or a new language. Heard a lot about Ruby on Rails? Try it out! Reading the pros and cons of PHP vs. ASP or ASP.NET? Try both and form your own opinion. It never hurts to add to tool to the toolbox.

Recursion isn’t so bad!

Posted by Jack Altiere on July 25th, 2006

In my experience, recursion is a topic that some developers fear and I have no idea why. It’s not that complicated……really. I’ve seen otherwise competent developers look like a deer in headlights when asked to solve something recursively. I’m going to hopefully shed some light on this topic, and give a simple real world example of something that might be done recursively.

The most simple definition of a recursive function is that it’s a function that calls itself. See, that wasn’t so hard! The most important thing to remember when writing a recursive function is to clearly define a base case. When you don’t have a clearly defined base case specified you can run into the dreaded infinite loop problem, which isn’t a good thing. After you have a base case, your function just needs to get you closer to the base case every time you call it.

The easiest way to figure out what I’m talking about is through example. I chose an example for this article that everyone can relate to. We’ve all been to a website that gives you a “breadcrumb” view of where you are in the site…this is especially true in sites where you are buying something. You might see a set of links on the page that looks something like this:

Home -> Category1 -> Sub Category1 – > Sub Category2

Lets say our site has a database on the back end, and this database has a Category table. This table has a few fields, CategoryID, ParentCategoryID, and CategoryName. A top level category in our database will have a ParentCategoryID of 0. Are you with me so far? Good. The base case in our example is clear. We start from the category that the user is at, and we keep adding categories to our list until we find one that doesn’t have a parent category above it. Every time we call this function we will be closer to the top of the tree, so this looks like a good candidate for recursion.

I’m going to tackle this example in PHP, it’s a solid language used often in website development. FYI: I’m skipping any sort of error checking in the example code for the sake of keeping the things simple.

<?php
// This string will eventually contain our list of categories.
//I’m going to return a string delimited with a | in this example.
$navString = “”;

// This is our recursive function definition.
//The $id parameter refers to the categoryID in our database table.
//The $data parameter is our current category list.
function loadNavTrail($id, $data)
{
  // I’m assuming that we have our database info set up somewhere else.
// A global variable probably isn’t the best idea but that’s not the point
// of this example.
  global $DB;
 
// We are going to query our database to get the name of our category.
// This example assumes we are using a MySQL database.
  $sql = “SELECT CategoryName FROM Category WHERE CategoryID = $id”;
  if (!$result = mysql_query($sql, $DB))
    die(“Database error!!”);
 
$row = mysql_fetch_array($result);
  mysql_free_result($result);
 
// Add the category name to our string.
// Don’t add a | to the result on the first pass.
  if (strlen(trim($data)) > 0)
    $data = $data . “|”;
  $data = $data . $row['CategoryName'];
 
// Figure out if our category has a parent category or not.
// This is our base case.
  $sql = “SELECT ParentCategoryID FROM Category WHERE CategoryID = $id”;
  if (!$result = mysql_query($sql, $DB))
    die(“Database error!!”);
 
$row = mysql_fetch_array($result);
  mysql_free_result($result);
 
// If this value is 0, we are at the top of the tree…otherwise call the function again.
  $parentCat = $row['CategoryID'];
 
if ($parentCat == 0)
  {
    // This is our base case, there is no parent category.
    return $data;
  }
  else
  {
    // This is our recursive function call.
    return loadNavTrail($parentCat, $data);
  }
}

// This is our function call.
// $currentID here represents the ID of the current category.
$navString = loadNavTrail($currentID, $navString);

// At this point, you could split the $navString variable
// on the ‘|’ character we chose as a delimiter.
// Realize that we got them from the bottom to the top, so you would have to
// reverse the order of the elements to get a “breadcrumb” view.
?>


See, I told you recursion wasn’t bad! Recursion is a very powerful technique to have in your bag of tricks as a developer, I recommend that if you don’t already have experience with it that you come up with an example and give it a shot!

Downloads

Twitter Updates

    Top Commentators

    • No commentators.

    Copyright © 2007 Jack Altiere. All rights reserved.