10 Techniques for Cleaner Code

0
Language

Level

Beginner

Access

Paid

Certificate

Paid

We’ve all written code that misses the mark. Sure, it works, but, still, you’re left with the feeling that you’ve missed something. The difficult part, unfortunately, is recognizing what that “something” is.

In this series, we’ll review ten techniques, one per episode, to improve the clarity of your PHP code.

Add your review

Refactoring Insurance

Technique #1. You can’t improve your code if you’re terrified to change it. That’s where tests come into play. Think of them as refactoring insurance. Go ahead, make as many tweaks as you need to. Each step of the way, your tests will let you know if you’ve made a mistake.

Play With Confidence

Technique #2. Now that we’ve learned to back up our code with a series of tests, we can move on to the second technique. Clean code isn’t a straight line. Often, you’ll follow a variety of roundabouts, tunnels, and even u-turns to get there. The secret is to play with confidence. Make a small tweak, run the tests, and then decide: “Is this better than before?”

Sweat the Small Stuff

Technique #3. Often, the little things that “don’t make a difference” have a way of eventually defining the integrity of your entire codebase. So, yes, do sweat the small stuff (indentation, temporary variables, if statements, etc).

Be Strict With Your Controllers

Technique #4. If you’re not careful, it’s easy for a controller to quickly get out of hand. All of the sudden, you have countless endpoints and actions – each with its own set of private helper methods. An alternative approach is to strictly adhere to the seven restful actions. If you find yourself reaching for a different name, ask yourself if you can instead create a brand new controller.

Drop Down a Level

Technique #5. Often, you’ll find your controller conducting logic that might instead be better managed by the model. In this episode, we’ll review a TeamMembersController that suffers from this very issue.

An Alternative to Magic Numbers

Technique #6. A magic number is an important value in your system that isn’t instantly clear. If you ever find yourself thinking, “Now what does this number represent,” you likely have a magic number on your hands. Instead, consider giving it a name.

Avoid Flags

Technique #7. Particularly when it comes to your public interface, be careful about requiring flags to toggle various functionality. Instead, ask yourself if creating a new method will remove the need for the flag entirely.

The Strategy and Factory Patterns

Technique #8. If your class contains multiple method with the same prefix, this might be a sign that a few classes are begging to be extracted. In this episode, I’ll show you how to combine the strategy and factory patterns to improve a messy controller.

Encapsulated UseCases

Technique #9. Think of a use case as a class that represents an important user action in your system. A few examples might be RegisterTeam, PurchaseVideo, or BuildServer. These classes should encapsulate all required steps for completing that action. Let’s learn how to create one in this episode.

Choose Your Class Names Wisely

Technique #10. Last, but not least, pay special attention to the names you choose for your classes. You might find that the wrong choice will cascade down and affect all of your method names as well.

User Reviews

0.0 out of 5
0
0
0
0
0
Write a review

There are no reviews yet.

Be the first to review “10 Techniques for Cleaner Code”

×

    Your Email (required)

    Report this page
    10 Techniques for Cleaner Code
    10 Techniques for Cleaner Code
    LiveTalent.org
    Logo
    LiveTalent.org
    Privacy Overview

    This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.