Creative Coding

Practical Tips for Improving Your Code


On this site, we spend a lot of time sharing information and discussing how to accomplish certain things with WordPress. After all, the purpose of the site is to provide tutorials - that is, we attempt to give practical advice on how to build certain things using the platform.

But development isn't strictly about writing code and building things. It's also about writing quality, maintainable code, refactoring and improving the state of our projects, and generally trying to leave a codebase in a better state than it was when we found it (or when we began creating it).

So rather than focus on how to build something or review any particular code, let's review a few practical tips for writing quality WordPress code.

WordPress Coding Standards

First and foremost - and although we discuss this a lot - it's worth repeating over and over:

Following the WordPress Coding Standards is one of the most significant things that you can do when writing your themes, plugins, or applications.

For those who are unfamiliar, the WordPress Coding Standards provide rules for how we should be formatting our WordPress-based PHP. Of course, there's nothing to actually enforce the rules - you can ignore them (and many do), but it's considered a best practice for those who are serious about developing WordPress-based projects, and it's respected by those who are active in the community.

Furthermore, following coding standards means that we - along with all other developers who also do it - will have similar looking code. In fact, one of the goals of defined coding standards is so that the code looks like it has been written by a single developer.

Another advantage to doing this is that it also makes it easy for others to contribute to our codebase. After all, since WordPress and its derivative works are open source, others may want to come along and fork or contribute ultimately giving them the ability to easily read our code.

Lastly, this isn't necessarily a call to action to go back and refactor everything you've every done. Now's as good a time as any to begin following the standards. Good developers are improving all the time, so it's totally acceptable to start now (even if it's in the middle of a project).

Code Comments

One of the most helpful things that you can do when writing code is to leave helpful comments that explain exactly what it is you're attempting to do.

Naturally, comments can live at the class level, function level, and the line level. They are permissible in PHP, HTML, JavaScript, and CSS so there's really no excuse not to include them somewhere.

Sure, writing comments takes a little bit of extra time, but remember that if it were easy to read, it wouldn't be called code.

Think about it this way: Programmers are notorious for going back to their previous projects and recognizing how bad their code is, or how we would do something different if we were working on that project now.

If we say that about our own code, what must others think when they see our code especially if they're coming from a more experienced background?

For more in-depth thoughts on commenting both server-side and client-side code, be sure to review this series.

Simplified Functions

Another thing that we developers can do is to simplify our functions. Though I realize that this is somewhat of a subjective area, I think that aiming for smaller, more focused functions makes the code more readable and can ultimately help with testability (if you're interested in going that route).

First, it's not at all uncommon to see functions that exceed 30, 40, or 50 lines. The problem is that these functions are often doing more than one thing.

This is problematic because:

  • It can make diagnosing the problem more difficult to trace when a bug arises
  • It can decrease the readability of the code by making it more difficult to give the function a unique, descriptive name
  • It can make it difficult to write specific unit tests because the functions aren't doing specific things.

So, with that said, there are some practical tips that we can follow to improve the quality of our functions.

Stay DRY

If you find yourself writing the same thing over and over among different functions, then that's a case for when you need to extract that particular part of the code and move it to its own function so that it can be called by all of the places that it currently exists.


If you find your code being either complicated to describe with comments or  difficult to trace while reading through it then it may be worth taking a step back and refactoring your code into something a bit simpler.

This looks different in each situation, but striving for readability over complexity is often times a better goal to strive for rather than simply getting something working.

Get in Line

This is arguably the most subjective point, I think it's worth bringing up and that's simply that we should strive to keep our functions small with 20 lines long being a relatively solid length for which to aim. Sure, this can be somewhat of a challenge with the way that WordPress often requires us to create large arrays to pass as arguments, but you get the idea: keep them small, focused, and maintainable.

Yes, this will result in more functions, but the code will be easier to read and maintain as each function has a single specific purpose. This means you can name them with greater clarity and provide unit tests around them more easily.


Obviously, none of the above are hard rules that have to be followed - they're simply suggestions for how to improve the quality of the code that we write, that we maintain, and that we contribute to others' work.

Above else, I believe that we should be striving for readability and testability. Keeping those two goals are the forefront of our work will help drive everything else into its proper place.

Of course, this isn't a complete list - it's far from it! So please offer up your suggestions in the comments!

Related Posts
  • Code
    Object-Oriented Programming in WordPress: ScopeWordpress oop
    In continuing our discussion of object-oriented programming in WordPress, we need to begin talking about the idea of scope. In short, this refers to the idea as to how classes can control how their attributes and functions are accessed (or whether or not they can even be accessed). This is yet another core idea of object-oriented programming after which we should be in good shape to begin working on an actual WordPress plugin.Read More…
  • Code
    Mobile Development
    In the Spotlight: Brian LeRouxPreview image@2x
    The explosive growth of the mobile space has accelerated the search for a robust and viable cross-platform solution. In 2008, shortly after the introduction of the iPhone SDK and after fiddling with Cocoa and Objective-C, Brian LeRoux and his colleagues at Nitobi decided that their time was better spent building a cross-platform solution than building native mobile applications.Read More…
  • Code
    Web Development
    Testing Your Ruby Code With Guard, RSpec & PryRuby wideretina preview
    Learn the basics of test-driven development in Ruby along with many common web development tools such as Guard, RSpec, and Pry, among others. Read More…
  • Code
    Creative Coding
    Using WordPress for Web Application Development: A ReviewApplication foundation 400
    Over the past few months, we've been taking a look at all of the features and aspects that make WordPress a potential foundation for application development. In fact, we've spent roughly 15 articles talking about all that WordPress offers. And though we'll be reviewing each of the points in this email, perhaps the biggest thing to take away that building web applications using WordPress is different than using many of the popular frameworks that are currently available namely because WordPress isn't a framework.Read More…
  • Code
    Theme Development
    Making the Perfect WordPress Theme: How to Code WellWp white 400
    In the previous part of this series; we went through various WordPress APIs that we should learn about, talked about the importance of making a theme translatable (or even better, releasing them already translated into other languages) and understood the concept of licensing themes and using licensed products with the themes. In this article, we're going to focus on code: We'll see how to code with WordPress coding standards, how to properly comment our code and how to validate and test the theme.Read More…
  • Code
    Cheat Sheets
    The WordPress Coding Standards: Naming Conventions and Function ArgumentsThe wordpress coding standards
    In this series, we're taking a deep dive into the WordPress Coding Standards - specifically, the PHP coding standards - in order to evangelize and understand how quality WordPress code should be written. Despite the fact that this is documented within the WordPress Developer Handbook, I think there's something to be said for understanding the rationale behind why some things are the way that they are. Remember: Our ultimate goal is to make sure that we're writing code that conforms to the coding standards so that we, along with other developers, are able to more easily read, understand, and maintain code for themes, plugins, and applications built on top of WordPress. In this post, we're going to be taking a look at how to handle naming conventions and function arguments.Read More…