Even though I use and like Genesis doesn’t mean there aren’t areas where it doesn’t fully comply with clean, quality coding techniques. In the last episode, I showed you the code for processing the post image. Did you notice that the code within the function genesis_do_post_image() will not run (none of it) if the conditional expression is not true? Ah, this is a perfect example of when to return early. Return early pattern: when you are done processing and nothing else in the function/method is going to be processed, then so bail out and return early. Why? Why would you […]
Clean Coding
Labs
Labs are hands-on coding projects that you build along with Tonya as she explains the code, concepts, and thought processes behind it. You can use the labs to further your code knowledge or to use right in your projects. Each lab ties into the Docx to ensure you have the information you need.
Each lab is designed to further your understanding and mastery of code. You learn more about how to think about its construction, quality, maintainability, programmatic and logical thought, and problem-solving. While you may be building a specific thing, Tonya presents the why of it to make it adaptable far beyond that specific implementation, thereby giving you the means to make it your own, in any context.
Quality Code Tip: Views are for HTML – Not Your Business Logic
In today’s quality code tip, you will learn about removing the HTML from your business logic and putting it into a view file. In programming, you want to separate out your code by its intent and purpose. Let’s discuss the what, how, and why of view files. (Side note: I’m showing you the actual code on this website which is powered by Genesis.
if/else Backwards Code Pattern – Code Smell
Code Smell Alert: A common code pattern that I see is where the if/else logic and the if conditional is backwards. I call this the “if/else backwards code pattern.” This code pattern makes the code less readable, which decreases its quality. There are two clues to let you know that you have stumbled into this pattern: The if conditional is set off of a false state. Typically you’ll see a not operator (!) as your clue. The if logic is setting a default state. But that is the job of the else. Smelly Version This is the smelling code pattern: […]
Smelly Code: the “if/return true/false” Code Pattern
When doing a conditional expression, it’s a smelly code pattern to then return true or false. Why? The conditional expression already returns a true or false state. Therefore, you do not need to be so verbose and hardcode a boolean return. Here is the smelly code pattern: Refactor this code down to one line to make it more readable. It’s less code too (skinny). Strive to reduce your code and make it more simple. If you see this pattern in your code, then refactor it. Remember, you want your code to be as skinny (as few lines) as possible.
Master Tip: Why Use a View?
Let’s talk about why you want to not intermingle your business logic with the HTML. Why do you use a view? It’s based upon the proven software principles such as Separation of Concerns and Single Responsibility. APIs, frameworks, tools, and apps come and go. But these principles remain. More importantly you will learn about intent. Intent is important in all of the code that you will ever build.
Building the HTML View File
It’s time to build out the HTML structure and put it into the view. What is a view? Why do you want to use it? Let’s talk about the qualities of clean, quality code. You will also learn about how to load the view as well as the variable scope. To learn more about variable scope and including files, go to the PHP Variables Bootcamp.
Refactor Getting the Posts Page
Let’s refactor how you are getting the posts page object by abstracting it into a separate, reusable function. Then you’ll add in protection to ensure an object is received back from the database before continuing the processing. You will be learning about clean, quality code in this episode too. You will learn about the Falsey Conditional Expression.
Code Tip: Purposeful Naming
Let’s talk about naming of variables, functions, etc. in your code. In this video, you will learn some handy guidelines on naming by intent to let your code express what it’s doing, why it exists, how to use it, and what to expect from it. This tip is valid for all programming regardless of the language or technology.