Why NOT to use getters and setters | Tell don't ask

Do you use getters and setters? Are you familiar with anemic domain models? The Tell don't ask principle? In this video, we will learn more about these concepts! We have already touched on the application layer a bit through the screencast about the single responsibility principle, and the relationship with the infrastructure layer through the screencast about the dependency inversion principle. In this video, we will focus on the layer of our domain, specifically, the domain models.

Video Index

  • 0:19 Question: In PHP, public class attributes are accessed; should I use getters and setters?
  • 1:17 Programming principle applicable to all languages
  • 2:00 User model with public class attributes
  • 3:19 Difference between Data Transfer Object (DTO) and domain model. What is an anemic domain model?
  • 4:14 What problems arise from using public class attributes?
  • 4:50 User model as an associative array/hashmap
  • 6:00 What problems arise from using associative arrays for domain modeling?
  • 6:50 User model with private class attributes, getters, and setters (anemic model)
  • 7:17 Benefits of using getters and setters
  • 8:00 Why NOT to use getters and setters
  • 9:12 Domain model User with behavior
  • 10:47 What benefits come from using rich domain models (with behavior)?
  • 13:09 Conclusion: Neither public class attributes nor getters and setters. The ideal: Business logic where appropriate (high cohesion)

Video

Related Material

  • Tell don't ask: Article by Martin Fowler about the Tell don't ask principle
  • Tell, Don't Ask (thoughtbot): Examples of violations of the Tell don't ask principle in Ruby
  • Data Transfer Object: Article by Martin Fowler on the DTO design pattern
  • Anemic Domain Model: Article also by Martin Fowler about the anti-pattern of anemic domain models

Pay according to your needs

lite (only monthly)

19 €
per month
  • Access to a subset of courses to lay the foundation for maintainable, scalable, and testable code
  • Company invoice
Popular

standard

24,92 €
Save 121
Annual payment of 299
per month
  • Full course catalog
  • Design and architecture challenges
  • Highlighted solutions videos for challenges
  • Receive job offers verified by Codely
  • Company invoice

premium

41,58 €
Save 89
Annual payment of 499
per month
  • All previous benefits
  • Early access to new courses
  • More benefits coming soon

We won’t increase the price while your subscription is active