Monday, January 18, 2016

Refactoring in Swift - move field

Imagine classes as families. They should hold inside only what makes sense.

One does not invite neighbours to live with them, does he? Invite them to visit, maybe, but move in, no way!

So are classes. They should hold only the fields that make sense.

Think of this absurd example:

class Address {
    var street = ""
    var city = ""
    var country = ""

class User {
    var address = Address()
    var postalCode = ""


It is obvious that postalCode should not be in the User class but in Address.

I'll not insist on this, as it is too obvious. 

Next time you write a class, ask about each and every field: 

Should this be here? 

Does this class need to know about it? 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.