Real Refactoring Example
Lab: The Loop Part 2 – Developer’s Guide to Customizing Genesis
Video Runtime: 10:46
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 do this? Because it unwraps your code, expresses your intent, and makes it easier to read. Remember that readable is a HUGE thing in programming. It’s the difference between maintainable, reusable code and costly, hard to maintain code. When you see a return, you know you are done and that’s it. This is the point of using a return early pattern.
In this episode, I’m going to walk you through the code, talk about the reasons why and how to spot it, and show you how to refactor it. And guess, what? There are two (2) return early pattern opportunities in this one function. It’s an excellent opportunity for me to teach you about the software principles of clean, quality coding.
-
Real code example that needs to be refactored
-
Same code after refactoring
Jeffrey Way at Laracast has an excellent video on refactoring multiple levels in your code: Simple Rules for Simpler Code: One Level of Indentation. Check it out.
Once upon a time, there was a developer... You! This is going to be a good story...
Episodes
Total Lab Runtime: 02:28:53
- 1 Before Entry Content Hook - genesis_before_entry_contentpro 07:13
- 2 Entry Content Overviewpro 05:49
- 3 Entry Content - Post Imagepro 10:43
- 4 Real Refactoring Examplefree 10:46
- 5 Entry Content - Post Contentpro 12:16
- 6 Entry Content - Post Content Navigationpro 13:31
- 7 Entry Content - Post Permalinkpro 09:35
- 8 After Entry Contentpro 04:16
- 9 Entry Footer - Overviewpro 07:14
- 10 Entry Footer - Post Metapro 14:07
- 11 After Entry - Overviewpro 06:01
- 12 Reorder Author Box & After Entry Widget Areafree 08:25
- 13 After Entry Widget Areapro 11:30
- 14 Turning on the Adjacent Entry Nav - Previous and Nextpro 10:09
- 15 Customize the Previous and Nextpro 06:25
- 16 After the Loop - Archive Posts Navigationpro 07:26
- 17 The Loop Else - No Posts Foundpro 01:31
- 18 Wrap it Upfree 01:56