Now that I’ve actually started coding my first WordPress plugin, I’ve realized that I used to be a fairly messy programmer. In the past, I would write some code, test it, fix it, and then be done. Now I find myself taking a look at my “finished code” and finding ways to refine it. I’m not happy with it just working. I want it to be concise and beautiful. I want it to make sense when I look at it a year from now. Below are some things that I’m doing to accomplish that.
Every line, every function, every end bracket gets a comment. Even if it’s obvious what the code is doing, I write it out in plain English just to make sure. It may not be as obvious to me in the future or to another individual looking at the code.
It’s almost always unnecessary to have repeating blocks of code throughout your project. If you find yourself repeating or reusing code, turn it into a function. You can then just call it when you need it. It will make your code cleaner and you’ll thank yourself down the road when you decide to change something and don’t have to update your code in half a dozen different places.
There’s a hierarchy to every bit of code you write. Use indents and white space to reflect that hierarchy. It will make it easier to find things and easier to know where things start and end.
When naming classes, functions, and variables, use names that make sense. You want reading your code to be like reading a sentence.
These are just a few steps that make for better coding practices. There are plenty more and I’m sure that mine are far from perfect. What are some coding practices that you use?