Avoid Short Cuts

Development Tips

Short cuts sometimes look like they save you time but when it comes time to change that code it's the shortcuts that take the most time to fix.


Date : 2007-05-15
A good rule of thumb is that if it saves time to write it the first time it's going to take longer to maintain. What happens is the shortcut assumes several details and we think, “well, everything I need this for fits within those parameters”. So we go ahead and use the shortcut. Then the client wants one small features added. Suddenly that shortcut no longer works and we have to rewrite that whole section without the shortcut. The most maintainable code is often the most simply worded code.

This isn't to say that you shouldn't use syntactical shortcuts such as PHP's echo shortcut: as a replacement for . The shortcuts that we need to avoid are the ones that make sweeping assumptions about what we are accomplishing. They are usually “Higher Level” instructions that do a lot of work all at once and seem like such a time saver. One example of that is in ColdFusion there is a Tag (command) called “” it “Assumes” that all the form input variables are named after your database fields and does a quick easy insert command filling in the field names with the form variable names and the values with the form field values. Of course this is a process we have all done a million times and it does save time to have it all wrapped up in one command. But think of the consequences: First, we are skipping the vital step of validation, opening ourselves and our users up to hacking attempts. Second, we are giving away the names of our database fields in our form. Anyone could look at a simple form on a ColdFusion page and assume that those are the database field names. This would make Insertion attempts much more likely to be successful. Third, what happens when we want to add a “Verify Email” field? Things suddenly get more complicated. If I remember correctly the and it's brother do allow you to submit a list of fields to use so fields that were not necessary to the SQL command could be filtered out, but still it shows how things get more complicated with the Shortcut as you add features.

Rather than looking for the fastest way to do each process with your code, look for the cleanest way to do it. Clean code is easy to read, easy to maintain, and usually easier to write the first time. When you have found the cleanest way to write a process you have probably also found a coding “best practice”. Thousands of programmers have been working on the same problems for many years. Chances are if there is a standard way of doing something there is good reason for it.

Comments :

No comments yet
  • Search For Articles