Click here to load reader

Data Class - Bad smells

  • View

  • Download

Embed Size (px)

Text of Data Class - Bad smells

Bad smell Data class

Bad smellData classKeerati 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 (cont)Use @property for getting methods,and use @field.setter for setting methods

How about 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

Im Mature now

Now movie class is not a data class anymore.It has responsibility to calculate its charge.


Happiness is only real when shared

Thank you

Search related