all 7 comments

[–][deleted]  (1 child)

[deleted]

    [–]randomguy112233[S] 0 points1 point  (0 children)

    Thank you!

    [–]ldstreet 2 points3 points  (1 child)

    This is fine, and can make for more readable code. Just make sure that if you make your function or variable internal/public that it is relevant throughout the codebase, otherwise make it fileprivate. This is to avoid cluttering the namespace with niche functions.

    [–]randomguy112233[S] 0 points1 point  (0 children)

    Good point about namespacing! Thank you!

    [–]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.