Advertisement
ivandrofly

Why is suffixing my classes with Handler or Service a bad practice? (Naming thing)

Aug 10th, 2023 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. # Rider Ai Answer:
  2. Suffixing classes with "Handler" or "Service" is not inherently a bad practice. Naming conventions are highly dependent on team preferences, project context, and sometimes on company-wide guidelines. However, a few points can cause criticism:
  3. Vague meaning: Words like "Handler" and "Service" are quite generic and can be ambiguous. They could mean anything and often don't provide enough clues about what a class is doing.
  4.  
  5. Single Responsibility Principle: If a class is a "Handler" or "Service", it often indicates that the class might be doing too much. Ideally, a class name should reflect what it does. If it does so many different things that you can't come up with a specific name, this might be a symptom of a class having more than one responsibility, which violates the Single Responsibility Principle (SRP) of the SOLID principles.
  6.  
  7. Tends to promote an Anemic Domain Model: Anemic models separate data and behavior, which is an anti-pattern in domain-driven design. When you have a class like "OrderService", it might be doing all operations related to an order, while the Order class is left with just getters and setters.
  8.  
  9. Harder to find classes: In large projects, if most of your classes end with "Service" or "Handler", it might make it harder to find specific classes you're looking for when you navigate through your project.
  10.  
  11. That said, if your team believes in suffixing classes with "Handler" or "Service" and it helps in understanding the codebase, it's not a wrong decision. The key point is to ensure each class has a single responsibility and its name properly reflects it. Other qualities such as testability, maintainability, and readability of your code should be prioritized. Make sure to review your applications and refactor code when necessary to promote clean code principles.
  12.  
  13. # AI Naming suggestion
  14. Naming classes depends heavily on the specific responsibilities that the class takes on. A class name must reflect its purpose or functionality. Here are few suggestions:
  15. If a class is intended to perform a specific operation, consider naming it after the operation it executes. For example, if the class is responsible for processing payments, a possible name could be PaymentProcessor.
  16. If a class represents an entity in the domain, it can be named directly after the concept it represents, like Customer, Product, Order, etc.
  17. If a class represents a collection of utility functions that do not logically belong to any other class, you could name it Util or Helpers.
  18. In the case of a class that manages the lifecycle of another class or provides instances of it, you might consider the Factory or Builder suffix as appropriate.
  19. However, it's crucial to maintain consistency and follow the naming conventions considered standard in your project, team, or organization.
  20. When dealing with complex domain logic, it might be beneficial to use terminology from the Domain-Driven Design (DDD) approach, like Repository, Service, Entity, ValueObject, AggregateRoot, etc. Remember that these have very specific meanings in DDD and should only be used if you are following a DDD methodology.
  21. Finally, avoid too lengthy or very short (single letter) class names, and ensure that the name is descriptive enough to let other developers understand what the class does without reading the implementation.
  22.  
  23.  
  24.  
  25.  
  26.  
  27. # Useful resources (Naming thing)
  28.  
  29. https://twitter.com/anton_t0shik/status/1677414053675712513
  30. https://twitter.com/anton_t0shik/status/1689569085493751808
  31.  
  32. https://www.reddit.com/r/learnprogramming/comments/mv33hr/why_is_suffixing_my_classes_with_handler_or/
  33. https://www.youtube.com/watch?v=MqugiGzricQ
  34.  
  35. https://www.youtube.com/watch?v=ZDluHz-ybPE&t=360s
  36.  
  37.  
  38. Inspired from https://www.reddit.com/r/learnprogramming/comments/mv33hr/why_is_suffixing_my_classes_with_handler_or/
  39. Source AI Gen
  40.  
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement