Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should XExtensionItemMutableParameters be replaced with a builder class? #1

Closed
irace opened this issue Nov 19, 2014 · 13 comments
Closed

Comments

@irace
Copy link
Contributor

irace commented Nov 19, 2014

Mutability should be avoided. A great way to provide an easy-to-use object creation API is by using the builder pattern.

Unfortunately, many Objective-C developers seem adverse to using builders. As such, the first version of this library includes the XExtensionItemMutableParameters subclass, which allows one to create an XExtensionItemParameters instance without using its initializer (which takes a large number of arguments).

I'd prefer a builder but want this library to be as approachable as possible. The introduction of a builder class means XExtensionItemMutableParameters would be deleted altogether, and XExtensionItemParameters's large initializer would be made private. The only way to get an instance would be to use the builder object and then call build at the end.

@robb
Copy link

robb commented Dec 10, 2014

I'd prefer a builder over that monster of an initializer any day

@irace
Copy link
Contributor Author

irace commented Dec 10, 2014

I'd prefer a builder over that monster of an initializer any day

❤️ @robb

@mbbischoff
Copy link
Contributor

I'd prefer a builder over that monster of an initializer any day

Me too, but I prefer mutable subclasses ever so slightly to builders.

@khanlou
Copy link

khanlou commented Dec 10, 2014

Objective-C initializers are basically factory methods anyway (since they can return any object or even nil), so an initWithBuilder: isn't that scary of an idea, imo.

@irace
Copy link
Contributor Author

irace commented Dec 10, 2014

Me too, but I prefer mutable subclasses ever so slightly to builders.

@mattbischoff Mutable subclasses and monstrous initializers go hand in hand. You need the latter in order for the former to be of any use.

@lazerwalker
Copy link

I'm personally a +1 for using builders.

I wonder if it might be an acceptable compromise to offer a builder, but not deprecate the mega-initializer. People who are that scared of using a builder can just use the nasty verbose initializer.

@irace
Copy link
Contributor Author

irace commented Dec 10, 2014

Mutable subclasses and monstrous initializers go hand in hand. You need the latter in order for the former to be of any use.

I guess this is not technically true. The mutable subclass could be defined in the same file as the immutable base class, and the monstrous initializer would be made internal to the .m file. This is what I would do if I added a builder class anyway.

@robb
Copy link

robb commented Dec 10, 2014

Are there any default parameters?

@irace
Copy link
Contributor Author

irace commented Dec 10, 2014

Are there any default parameters?

No

@robb
Copy link

robb commented Dec 10, 2014

Unrelated to this one, but I've used a builder for requests before and enjoyed being able to have format strings along the way, e.g.:

[[[[XYRequestBuilder
    GET:@"/users/%@/", userID]
    params:@{ @"foo": @"bar" }]
    whatever:&amen]
    build];

@irace
Copy link
Contributor Author

irace commented Dec 10, 2014

I feel like a big part of why some iOS developers don't seem to be fans of builders is that they assume that most of the value comes from the method call chaining commonly associated with the pattern, which generally looks aesthetically terrible in Objective-C.

I've made plenty of Objective-C builder classes that don't expose a fluent interface yet still provide all of the other benefits of separating out object construction.

@irace
Copy link
Contributor Author

irace commented Apr 22, 2015

I think keeping the mutable subclass around but providing a convenience initializer that takes a block with an XExtensionItemMutableParameters instance as the argument is a good balance. It keeps the mutable class around for those who are allergic to builders, yet adds a convenient way to initialize that doesn’t require calling the (currently very long) designated initializer.

- (instancetype)initWithBlock:(void (^)(XExtensionItemMutableParameters *mutableParameters))configurationBlock;

@mbbischoff
Copy link
Contributor

Agreed. Great solution.

@irace irace added this to the 1.0 milestone Apr 23, 2015
@irace irace closed this as completed Apr 23, 2015
irace pushed a commit that referenced this issue May 13, 2015
Bunch of cleanup work on top of Ari’s `XExtensionItemSource` refactor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants