Reader small image

You're reading from  Clean Code in PHP

Product typeBook
Published inOct 2022
PublisherPackt
ISBN-139781804613870
Edition1st Edition
Tools
Right arrow
Authors (2):
Carsten Windler
Carsten Windler
author image
Carsten Windler

Carsten Windler is a Lead PHP engineer at Plan A.
Read more about Carsten Windler

Alexandre Daubois
Alexandre Daubois
author image
Alexandre Daubois

Alexandre Daubois is a Symfony Developer at SensioLabs, the company that created Symfony.
Read more about Alexandre Daubois

View More author details
Right arrow

Working in a Team

The main goal of this book is to enable you to write code that can be understood, maintained, and extended by you and others. Most of the time, being a PHP developer means that you do not work alone on a project or a tool. And even if you started writing code alone, chances are high that at some point, another developer will join you – be it on a commercial product, or your open source package where other developers start adding new features or bug fixes.

There will always be multiple ways to carry out a task in software development. This is what makes working in a team more challenging when you want to write clean code together. In this chapter, you will find several tips and best practices on how to set up coding standards and coding guidelines. We will also talk about how code reviews will improve the code and ensure the guidelines are kept.

We will also explore the topic of design patterns in more detail at the end of this chapter. These patterns...

Technical requirements

If you followed along with the previous chapters, you do not require any additional setup.

The code samples for this chapter can be found in our GitHub repository: https://github.com/PacktPublishing/Clean-Code-in-PHP.

Coding standards

In the previous chapters, you learned a lot about writing high-quality code. Yet, it is not enough if you do it by yourself only. When you work in a team, you will most likely have the problem that other developers have a different understanding of quality and are on a different skill level than you are.

This is harmful to your code because it might lead to lazy compromises, where the involved parties agree on a way, just to have their peace. Therefore, if you want to work effectively in a team, you want to standardize your work as much as possible.

It makes sense to start with the low-hanging fruit: code formatting. This goes down to the very basics, such as agreeing on how many spaces should be used to indent lines, or where braces should be placed. But why is this even important?

We already shortly addressed this topic in Chapter 5, Optimizing Your Time and Separating Responsibilities. However, we want to expand on it at this point. The main advantage...

Coding guidelines

In the previous section, we talked about why you should introduce Coding Standards. Once this is accomplished, you should consider setting up coding guidelines. Both topics sound very familiar, and indeed, they are. Yet while Coding Standards usually focus on how to format code, coding guidelines define how to write code. This, of course, includes defining which Coding Standard to use, but covers a lot more, as you will learn in this section.

What does how to write code exactly mean? Usually, there is more than one way to achieve things when writing software. Take the widely known model-view-controller (MVC) pattern, for example. It is used to divide the application logic into three types of interconnected elements – the models, the views, and the controllers. It does not explicitly define where to place the business logic, though. Should it be located inside the controllers, or rather inside the models?

There is no clear right or wrong answer to this...

Code reviews

The process of manually checking the code of other developers is called a code review. This includes all changes, that is, not only new functionality but also bug fixes or even simple configuration changes.

A review is always done by at least one fellow developer, and it usually happens in the context of a pull request, shortly before the code of a feature or bug fix branch gets merged into the main branch; only if the reviewer approves the changes will they become part of the actual application.

In this section, we will discuss what you should look for in code reviews, why they are so important, and how they should be done to make them a successful tool in your toolkit.

Why you should do code reviews

It might sound a bit obvious because that is what this whole book is about. Yet, it cannot be stressed enough – code reviews will improve the quality of your code. Let us examine more closely why:

  • Easy to introduce: Introducing code reviews usually...

Design patterns

Design patterns are commonly used solutions to problems that occur regularly in software development. As a developer, you will sooner or later come across this term, if you have not done so already – and not without a reason, as these patterns are based on best practices and have proven their usefulness.

In this section, we will tell you more about the different types of design patterns and why they are so important that they became part of this book. Furthermore, we will introduce you to some common design patterns that are widely used in PHP.

Understanding design patterns

Let us have a closer look at design patterns now. They can be considered templates to solve particular problems and are named according to the solution they provide. For example, in this chapter, you will learn about the Observer pattern, which can help you to implement a way to observe changes in objects. This is very useful when you write code, but also when you design software...

Summary

In this chapter, we discussed the importance of standards and guidelines. Coding standards help you to align with fellow developers on how the code should be formatted, and you learned about existing standards worth adopting.

Coding guidelines help your team to align on how to write software. Although these guidelines are highly individual for each team, we provided you with a good set of examples and best practices to build your team’s guidelines. With code reviews, you also know how to keep the quality up.

Finally, we introduced you to the world of design patterns. We are confident that knowing at least a good part of these patterns will help you to design and write high-quality code together with your team members. There is much more to explore on this topic, and you will find links to some great sources at the end of this chapter.

This almost ends our exciting journey through the many aspects of clean code in PHP. We are sure you now want to use all your...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Clean Code in PHP
Published in: Oct 2022Publisher: PacktISBN-13: 9781804613870
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Authors (2)

author image
Carsten Windler

Carsten Windler is a Lead PHP engineer at Plan A.
Read more about Carsten Windler

author image
Alexandre Daubois

Alexandre Daubois is a Symfony Developer at SensioLabs, the company that created Symfony.
Read more about Alexandre Daubois