What we have now:
@interface BGTest
-(void)someMethodWithName:(NSString *)methodName tag:(NSUInteger)tag path:(NSURL *)path;
@end
Why not to write:
@interface BGTest
-(void)someMethodWithName:(NSString *) tag:(NSUInteger) path:(NSURL *)
@end
It is stated that ObjC method names should be self documented, and they are. Why to write more if methods can be clearer with lesser code?
I mean, why do we need to name method arguments in interface part, but not just in implementation?
Named arguments enable you to specify an argument for a parameter by matching the argument with its name rather than with its position in the parameter list. Optional arguments enable you to omit arguments for some parameters. Both techniques can be used with methods, indexers, constructors, and delegates.
Using named parameters really simplifies things as we don't have to worry about the order of parameters and we can specify the values for only those parameters that we want. When named parameters are used with optional parameters, the usability and the function call become easy and are much enhanced.
Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order.
Keyword arguments (or named arguments) are values that, when passed into a function, are identifiable by specific parameter names. A keyword argument is preceded by a parameter and the assignment operator, = . Keyword arguments can be likened to dictionaries in that they map a value to a keyword.
You can't declare a method without a parameter name because in this case there is no way for the compiler to tell if the next token after the type is a parameter name or it is a part of the method name. Let me demonstrate it on this example:
-(void)testMethodWithParam:(int)a secondParam:(int)b;
This is a correct method declaration. Now let's edit it as you propose:
-(void)testMethodWithParam:(int) secondParam:(int);
It's pretty obvious for us what we mean. We can tell, that secondParam:
is clearly a part of a method name. But Objective-C allows us to write a column after a space like this:
-(void)testMethodWithParam:(int) secondParam :(int);
And this case (from my point of view) is not that clear anymore. Especially if we remove one more space:
-(void)testMethodWithParam:(int)secondParam :(int);
Now it looks like secondParam
is the name of the first parameter, and part of a method name is missing.
I guess, to make resolving such issues easier, Objective-C makes us write a complete method specification.
I think, they could've changed method declaration format a little bit, so that it allowed such declarations (I like your idea), but it is like this for now.
Still, you can use different variable names in @interface
and in @implementation
. So this:
@interface TestClass : NSObject
-(void)testMethodWithParam:(int)a secondParam:(int)b;
@end
@implementation TestClass
-(void)testMethodWithParam:(int)firstParam secondParam:(int)secondParam
{...}
@end
is legal.
P.S. By the way, @CodaFi's idea with using underscores as variablesNames could be nice in this case:
@interface TestClass : NSObject
-(void)testMethodWithParam:(int)_ secondParam:(int)_;
@end
The interesting thing is that it compiles, despite the fact that I use a single underscore for both variable names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With