data class - bad smells
TRANSCRIPT
BAD SMELL“DATA CLASS”
Keerati Limkulphong
What is “Data Class”
Data class is a stupid classwhich just contains the fields, getting and setting methodsand nothing else.
Data classes are like children.(no responsibilities) They are okat the beginning, but to participateas a grownup object, they need totake some responsibilities.
Encapsulation and Validation Problems
Because the fields are public, so the users can access to them directly.
So what should we do ?(If we are Pythonic)
Encapsulation
???
Encapsulation (cont)
Use @property for getting methods,and use @field.setter for setting methods
How about validation???
Validation“Errors should never pass silently.” ~ZEN of Python~
So what next???
Becoming Mature Data classes are like children. (no responsibilities) They are ok at the beginning, but to participate as a grownup object, they need to take some responsibilities. ~Martin Fowler~ (Author of Refactoring)
Now movie can be rented
Becoming Mature (cont)Rental object uses price_code in Movie object to calculate charge
Move Method
I’m Mature nowNow movie class is not a data class anymore.It has responsibility to calculate its charge.
“Happiness is only real when shared”
Thank you