Introduction

Welcome to the second lesson of the "Clean Code Basics in PHP" course, focused on meaningful naming. In the previous lesson, we introduced clean code and its significance in developing maintainable and efficient software. Now, let's explore the importance of meaningful naming — an essential part of clean code. Selecting appropriate names is vital for creating code that is clear, understandable, and easy to maintain.

Good Naming at a Glance
Reveal Intent Through Names
Avoid Misleading Names
Choose Descriptive, Searchable Names
Name Interfaces and Implementations Wisely

In PHP, while interfaces can be named freely without enforcing strict conventions, it's important to select names that clearly indicate their functionality. For example, instead of using legacy conventions like IUserService, simply using UserService for an interface is preferable. For class implementations, names like InMemoryUserService or DbUserService effectively communicate their specific roles or strategies.

Consistent Naming Across the Codebase

Consider method names such as fetchAllUsers, retrieveTasks, loadUsers, and fetchEveryTodoItem. Is anything wrong with these names? They do convey intent and are descriptive, so they appear fine. However, using these varied names within the same codebase is problematic due to inconsistency. In the same codebase, it's beneficial to stick to a single naming pattern, like getAll, to avoid confusion and maintain clarity, e.g., getAllUsers, getAllTasks, getAllTodoItems.

Provide Sufficient Context in Names
Summary

Meaningful naming is a critical aspect of writing clean code. By choosing names that clearly express intent, avoiding misleading terms, and maintaining consistency and context, you create code that is easy to read, understand, and maintain. Up next, you'll have the opportunity to refactor code, applying these principles and honing your ability to write intuitive, clean code.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal