Reputation: 44698
Let's say I have a statement which is several lines long:
NSString *urlString = @"http://www.example.com";
NSURL *url = [NSURL urlWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
Is there any benefit to condense it to one line like so?
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL urlWithString:@"http://www.example.com"]];
Upvotes: 3
Views: 353
Reputation: 95335
The benefit to the one-line version is that you don't have to create temporary intermediate variables. This is particularly beneficial if you have no other use for those intermediate variables. In some cases it looks neater to use the one-liner but in other situations it can look clunky. There's no hard-and-fast rule to follow, it is entirely up your own aesthetics. Personally I would choose the one-line version for your scenario but there are probably others that would disagree.
One time that I know I avoid one-liners is when an initialiser method takes multiple arguments, because it can get messy and hard to follow, especially if you go several layers deep. For a lite example:
id someObject = [MyClass myClassWithThing:[Thing thingWithX:5 andY:5] supportingThing:[SupportingThing supportingThingWithString:@"Tada!"] error:NULL];
Some people prefer to use Eastern Polish Christmas Tree notation, which would look like:
id someObject = [MyClass myClassWithThing:[Thing thingWithX:5 andY:5]
supportingThing:[SupportingThing supportingThingWithString:@"Tada!"]
error:NULL];
Again, there's no rule to follow here. Although Objective-C has conventions on how to name classes and methods, I've yet to encounter a convention for nested message sending.
First and foremost, code for readability and maintainability.
Upvotes: 4