What’s the limit for inline comments, and how can you minimize them? Let’s look at a bad and a good example of inline comments, and how they’re chances to improve your code.
Clean Coding
Does It Matter If a Hook’s Callback Function Comes Before or After its add_action? No and Yes.
Does it matter if a hook’s callback function comes before or after its add_action? My answer might surprise you. No, it does not matter to PHP or WordPress. Yes, it does matter for code readability. I’ll explain why in this article.
Why shouldn’t you repeat code? The power of DRY
I often talk about not repeating your code. A member wants to know why. Why should you repeat code? What is DRY? Why is it important to you?
Refactor FAQ Module
In this episode, you and I are going to refactor the FAQ module. We’ll have both shortcode versions and the archive template using the same container and FAQ view files. You’ll hear and see me walking through the thought and build process, thinking about different approaches, and then testing it out. It’s a good exercise of reducing the codebase and thinking about the build process. Come do this with me.
Planning the Refactor
As you were building this module, did you notice the redundancies within the view files? We are using separate view files that have virtually the same HTML structures to them. That’s not good. Why? Because if you need to make a change to the structure or one of the styling attributes, you have to remember to change it in three different places. That is an error waiting to happen. If you forget about one of them, then the change might get released and pushed out to your customers. That’s problematic. Instead, the better strategy is to combine the shortcode and […]
Theme Settings Defaults
StudioPress includes a file called lib/theme-defaults.php. This file sets the default theme settings which are found in Genesis > Theme Settings. In this episode, you will refactor the redundancies and then add these into the setup of your new theme.
Theme Customizer Files
Next, you need to move the theme customizer files from the Genesis Sample Theme to your new developer’s theme. You’ll start by copying the files into your theme. Then you’ll start refactoring by applying the PHP namespace and removing the prefixing. You’ll give the functions purposeful names so that each tells you specifically what it does, i.e. what the expected behavior is when you call the function. You’ll be reorganizing the helper functions into a helper file. Also, there are some functions embedded within a function, which makes the code less readable. Let’s refactor those and make them their own […]
Refactoring Image Sizes
Your homework in the last episode was to refactor the image sizes on your own. In this episode, we’ll walk through how to move the image size name and arguments into a configuration variable. Just like the theme supports, calling the image sizes over and over again is WET and unnecessary. Let’s refactor to clean up the code and remove the redundancies.
Refactoring Theme Supports
Did you notice that the function add_theme_support() is being called over and over and over again in the setup? And did you notice that this function requires a key and then a value? I mentioned in the last episode that the final version of this starter theme will load all of the theme supports from a configuration file. Calling this function repeatedly is redundant and unnecessary. Let’s refactor it to convert the theme to clean, quality coding standard. In this episode, you will refactor the WordPress theme supports to move the parameters into a local configuration array. Then you’ll loop […]
The Why of Clean Code
There are two main principles which will accelerate your career in this profession: fundamentals and clean, quality coding techniques. Mastering these, incrementally building yourself up and incorporating them into your workflow and code will make you more in demand and marketable. Why? Why is clean, quality coding so important? Clean code means quality. You are increasing the quality of the product you are producing. Why? By breaking up your code into modules, having a function do just one thing and behavior, purposefully naming variables and functions, and making your code more readable, you are reducing the maintainability costs of code. […]