14
BAD SMELL DATA CLASSKeerati Limkulphong

Data Class - Bad smells

Embed Size (px)

Citation preview

Page 1: Data Class - Bad smells

BAD SMELL“DATA CLASS”

Keerati Limkulphong

Page 2: Data Class - Bad smells

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.

Page 3: Data Class - Bad smells

Encapsulation and Validation Problems

Because the fields are public, so the users can access to them directly.

Page 4: Data Class - Bad smells

So what should we do ?(If we are Pythonic)

Page 5: Data Class - Bad smells

Encapsulation

???

Page 6: Data Class - Bad smells

Encapsulation (cont)

Use @property for getting methods,and use @field.setter for setting methods

Page 7: Data Class - Bad smells

How about validation???

Page 8: Data Class - Bad smells

Validation“Errors should never pass silently.” ~ZEN of Python~

Page 9: Data Class - Bad smells

So what next???

Page 10: Data Class - Bad smells

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

Page 11: Data Class - Bad smells

Becoming Mature (cont)Rental object uses price_code in Movie object to calculate charge

Page 12: Data Class - Bad smells

Move Method

Page 13: Data Class - Bad smells

I’m Mature nowNow movie class is not a data class anymore.It has responsibility to calculate its charge.

Page 14: Data Class - Bad smells

“Happiness is only real when shared”

Thank you