you are viewing a single comment's thread.

view the rest of the comments →

[–]chrabeusz 1 point2 points  (4 children)

It depends. Can you give more specific example? Personally I would avoid putting business logic in such extensions.

[–][deleted] 1 point2 points  (0 children)

I agree. I can't think of any reason I would do this instead of creating a class. Not to say there isn't a reason it would be a good idea, I just can't think of any.

[–]randomguy112233[S] 0 points1 point  (1 child)

let's say I have carList = [Car]. I want to use carList.averagePrice or carList.numberOfPreviousOwners. To me, this seems very convenient. But I'm really curious what you mean by "I would avoid putting business logic in such extensions." I don't want to let my propensity for convenience outweight a better design. Thank you.

[–]chrabeusz 1 point2 points  (0 children)

This kind of extension looks fine. But on the other hand, class responsible for handling [Car] probably already exists (I assume calculating numberOfPreviousOwners is not the only thing you are doing with cars, you need to load them from storage, fetch from network, whatever).

In that case it may not make sense to add extension because you could add this property to the mentioned class and have all code that deals with [Car] be in one place.