jaspercreel's avatar

Do you suffix your filenames with their path/type?

I tend to suffix my filenames with their path or type. For example, if I have an enum folder, everything in the enum folder is suffixed with Enum (e.g. UserStatusEnum.php). I just can't get away from it. It makes searching files so much easier.

Anyone else do this?

0 likes
5 replies
LaryAI's avatar
Level 58

Suffixing filenames with their type or path is a common practice among developers to improve file organization and searchability. While it's not a strict requirement, it can be beneficial in certain contexts, especially in larger projects. Here's a solution that outlines the pros and cons of this approach and provides a recommendation:

Pros of Suffixing Filenames

  1. Improved Searchability: As you mentioned, suffixing filenames can make it easier to search for specific types of files, especially in large codebases.
  2. Clarity: It provides immediate context about the file's purpose or type, which can be helpful for new developers joining the project.
  3. Consistency: If applied consistently, it can lead to a more organized and predictable file structure.

Cons of Suffixing Filenames

  1. Redundancy: If the file is already located in a directory that clearly indicates its type, the suffix might be redundant.
  2. Longer Filenames: Adding suffixes can lead to unnecessarily long filenames, which might be cumbersome to work with.
  3. Potential for Inconsistency: If not applied consistently, it can lead to confusion rather than clarity.

Recommendation

  • Use Suffixes Judiciously: Consider using suffixes for files that are frequently searched or when the file type is not immediately clear from the directory structure.
  • Leverage IDE Features: Modern IDEs and text editors often have powerful search and navigation features that can mitigate the need for suffixes.
  • Adopt a Naming Convention: If you decide to use suffixes, establish a clear naming convention and document it in your project's guidelines to ensure consistency.

Example

If you decide to suffix your filenames, here's how you might structure your files:

/Enums
  - UserStatusEnum.php
  - OrderTypeEnum.php
/Models
  - UserModel.php
  - OrderModel.php
/Controllers
  - UserController.php
  - OrderController.php

In this example, each file is suffixed with its type, providing clarity and aiding in searchability. However, ensure that this approach aligns with your team's workflow and project requirements.

chiefguru's avatar

Hi @jaspercreel, I'm in the habit of doing so particularly for services/events/listeners/jobs etc because I can quickly see at a glance that if I instanciate a new ZoomConnectionService() or a SimpleEmailTemplateJob somewhere in the bowels of my code what I'm dealing with.

1 like
Talinon's avatar

I generally try to avoid adding redundant suffixes. Especially if I have a dedicated directory for a large service. I wouldn't have a path such as \App\Services\Zoom\ZoomConnectionService.php. I'd opt for something like \App\Services\Zoom\Connector.php, removing the redundant "Zoom" and "Service" from the class name.

If I needed to find it quickly within my editor, I'd just use fuzzy search to find it quickly.. searching for "szc" Services/Zoom/Connector would bring it up immediately. If you follow the same patterns, this becomes second nature.

As for instantiating it within the bowels of code, I'd likely name a temporary variable (or via method injection) as $service, or if I needed to be more explicit because the class has many dependencies I might opt for $zoomService.

public function myMethod(Connector $zoomService)
{}

That's pretty clear to me, while having the benefit of chopping off redundantly long class/filenames.

In the case of more abstract programming, I'd do the same thing with interfaces. I've recently dropped using the "Interface" suffix for more descriptive name of what the implementation does. I know this goes against the PSR naming conventions, but it makes code so much more human readable. In this case, I'd might make an interface such as "ConnectsVirtualMeetings", and then the method above would become:

public function myMethod(ConnectsVirtualMeetings $service)
{}

And then the Zoom Connector class would implement ConnectsVirtualMeetings.

1 like
jaspercreel's avatar

@Talinon Part of why I asked this question is because it feels a little bad to suffix like I do. I just do it to help find things faster in my tools, but maybe that is a tooling problem, and I need to figure out my tools better

Please or to participate in this conversation.