hyperoslo / ios-foundation

πŸ…An established foundation within iOS team
5 stars 0 forks source link

Extension method names #43

Open onmyway133 opened 8 years ago

onmyway133 commented 8 years ago

When we add method, property (compute, associated object) to extension on UIKit class, there might be conflict even if we make internal/private extension in the framework. Like with Fashion, users can't add property names styles to UIView classes?

There are some posts about this

To avoid that, I think we need to add a prefix. Then what prefix should we use?

zenangst commented 8 years ago

For the case of Fashion maybe we can find a better name instead of adding a prefix? I do see the problem here, could lead to some unexpected behavior. styles would be renamed to fashion to be more tied to the repository. That was an initial thought that I had at least.

What are your thoughts on this @vadymmarkov ?

JohnSundell commented 7 years ago

How about adding a little bit extra wording to the APIs, to distinguish them from system APIs? A prefix works, but it smells of Objective-C πŸ˜„. One thing I've done in the past is to try to use the name of the library as part of API naming. That way you don't only avoid conflicts, but also makes it easy to see what API belongs to what library. For example, given the fashion case that you mention, I'd suggest simply prefixing it with the name fashion so that styles -> fashionStyles. May be a bit longer, but IMO it makes things a lot more clear πŸ˜„

onmyway133 commented 7 years ago

That sounds much nicer. For method names in extension, it will be more tricky I think, like appleStyles(), ...

Like in Gallery, I tend to add some properties to AVAsset extensions https://github.com/hyperoslo/Gallery/blob/master/Sources/Utils/Extensions/AVAsset%2BExtensions.swift. The g_ does not certainly sound good, but the point of using extension properties (instead of composition) make the code more pleasant to read

JohnSundell commented 7 years ago

@onmyway133 One option for the AVAsset case you linked would be to wrap it in another struct/class and then add our APIs on that. Like:

class GalleryAsset {
     private let asset: AVAsset

     fileprivate var naturalSize: CGSize {
         return asset.tracks(withMediaType: AVMediaTypeVideo).first?.naturalSize ?? .zero
     }
}

That way we don't have to worry about collisions at all πŸ˜„ What do you think @onmyway133?

onmyway133 commented 7 years ago

@JohnSundell It works πŸš€ I can refactor this way