Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Refactor Your Code Over Time

Posted on October 6, 2012 by Pippin in Thoughts 5 Comments
Home» Thoughts » Refactor Your Code Over Time
Tweet
Love It - 4

One of the best feelings that one gets from being a developer is the sense of elation that comes with finishing a product. You make the last change or addition to the code and that particular key stroke designates the product as done (at least for that version). Once complete, the code sits there unchanged, often times for very long periods of time. If it works, why change it? No need to, right? No, wrong.

Nearly every time I work on a plugin, I get the sense that tells me it is some of the best code I’ve written. It’s better than the code I wrote yesterday, and it’s better than the code I wrote a year ago.

If you have that same sense about your own code, then that means that ALL code you have ever written can be improved. This is part of the reason that I would strongly encourage you to make a habit of refactoring your code over time, at least for products that are publicly available.

I spent about five hours today going line by line through my Restrict Content Pro plugin. The plugin has over 110 files and a many, many thousands of lines of code. It’s a really large plugin, and I went line by line through every single file making improvements. The primary improvement I was working on was adhering more closely to the WordPress coding standards. This is something I feel very strongly about, but, unfortunately, did not use to follow very closely.

Over the course of 88 commits to the plugin’s Github repository, I added 1,698 lines of code and removed 1,759 lines. Overall I shrunk the plugin by nearly 100 lines.

A lot of my changes were simply improved white space and code formatting, but through the process of going line by line, I also found a significant number of security vulnerabilities and poorly sanitized data. Many of these issues were present because when I first wrote the plugin, I didn’t know very much about data sanitization or SQL injection vulnerabilities.

Before I started on this code refactoring escapade, I was not aware of many of the issues I found, and I was only able to fix them because I chose to go line by line through the entire plugin.

I’m constantly amazed at how quickly our skills as developers progress. Yesterday I opened a plugin I wrote a few months ago and was pretty disappointed with some of the code I found in it. That plugin wasn’t even three months old.

By forcing ourselves to sit down and go line by line through projects we’ve written in the past, not only do we dramatically improve old code, but we also gain a huge sense of accomplishment by seeing first hand just how far we have progressed in our development skills.

Part of me was really disappointed in what I found today, but another part was highly elated because I realized just how much I have learned in the last year.

We make ourselves better by not just learning new techniques, but also by learning what bad techniques are, and I assure you, one of the best ways to remind yourself what bad techniques are is to look at your own old code.

Tweet Follow @pippinsplugins

5 comments on “Refactor Your Code Over Time”

  1. Keith says:
    October 7, 2012 at 11:33 am

    Would love to see the actual code before and after. Especially highlighting the vulnerabilities, so we can learn best practices for good PHP coding.

    Reply
    • Pippin says:
      October 7, 2012 at 9:02 pm

      Here’s a couple of examples. Previously, whenever a subscription level was updated, the code that performed the SQL query looked something like this:

      1
      
      $update = $wpdb->query( "UPDATE " . $rcp_db_name . " SET something here with $_POST data " );

      This query is susceptible to SQL injections, and to improve it, I simply did this:

      1
      
      $update = $wpdb->query( $wpdb->prepare( "UPDATE " . $rcp_db_name . " SET something here with $_POST data " ) );

      The $wpdb->prepare() member function is the one designed to protect against injections.

      Another example would be the input fields used throughout the plugin for settings and such. Most of the data displayed in these forms was simply raw data, which means that if a user entered something (such as improper HTML) it could cause issues, including breaking of layout. To fix it, esc_attr() and other validation functions were used:

      1
      
      <input type="text" id="rcp-price" name="price" value="<?php echo esc_attr( $level->price ); ?>" style="width: 40px;"/>
  2. Tom McFarlin says:
    October 8, 2012 at 6:10 am

    Writing code is funny thing, because I can definitely identify with this:

    I get the sense that tells me it is some of the best code I’ve written. It’s better than the code I wrote yesterday, and it’s better than the code I wrote a year ago.

    And that’s something that happens with every language and/or library and/or framework, you know? You get comfortable using it for a while and then you get into this groove of writing really good code in it.

    Then you look at the code a few months later and … it’s as great as you thought. I think that’s the nature of programming.

    But following your advice and refactoring it overtime is smart – it’s like investing in your code: Small deposits over time end up leading to a much higher quality product.

    Reply
    • Pippin says:
      October 8, 2012 at 8:58 am

      I’m always surprised at how quickly my code quality declines. overtime. I’d say it’s a great testament to how quickly we learn.

  3. DrewAPicture says:
    October 28, 2012 at 3:05 pm

    By forcing ourselves to sit down and go line by line through projects we’ve written in the past, not only do we dramatically improve old code, but we also gain a huge sense of accomplishment by seeing first hand just how far we have progressed in our development skills.

    I see this all the time, working with students. There is a constant struggle to refactor and refactor because many of them improve so quickly. And it’s not a bad thing, it’s good really, and it becomes a matter of pride for them to be able to go back and refactor something they wrote just a short time before.

    I view it as a great measure of improvement when you look at something you wrote 3 months or a year ago and go, “What was I thinking doing it that way?” or “Wow, this is really inefficient.”

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Submitting Your First Pull Request to a WordPress Plugin on Github (2)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

  • Plugin Development 101 – Intro to Loading Scripts and Styles (16)

    In this part of Plugin...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • @jaredatch @kimparsell :D
    May 23, 2013
  • @jaredatch there is, as long as there is at least one ticket
    May 23, 2013
  • RT @Astoundify: We are hiring p/t tech support rep for our support forum if your interested email contact [at] http://t.co/bcXNhcwZx5
    May 23, 2013

Topics

hook meta box Rémi Corson featured shortcodes campaign monitor add_options_page register_setting Sugar Event Calendar attachments add_shortcode wp_enqueue_script the_content image forms short codes Related posts login do_action authors mail chimp attachment plugin recent posts comments post types bbpress apply_filters short code taxonomies custom post type Ajax images gallery Stripe jquery taxonomy users widgets add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2011 Pippin's Plugins