Reputation: 4923
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?
Upvotes: 9
Views: 207
Reputation: 7764
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.
Upvotes: 7
Reputation: 2551
One of the reason I can think of is to disambiguate from:
// A method named someMethodWithName:::
- (void)someMethodWithName:(NSString *) methodName :(NSUInteger) tag :(NSURL *)path;
which would look pretty close to what you suggest:
- (void)someMethodWithName:(NSString *) tag:(NSUInteger) path:(NSURL *);
Upvotes: 1
Reputation: 4805
Answering your question with a question, how does the compiler know that the name of the variable in your example is methodName
? I personally think it would be name
. How did the compiler magically omit the "with"? Is your hypothetical Objective-C compiler equipped with some language parsing technology that allows it to understand english? What if my native language is Spanish?
Moreover, what about this method:
-(void)someMethodWithName: (NSString*) anotherMethodWithName: (NSString*);
What's the name of the second variable? anotherMethodName
? How does the compiler know to omit the with
but save the another
?
What if you have an ivar
named "methodName"? Now you have to choose between an intuitively named method and an intuitively named ivar
.
Now, beginners to Objective-C have to learn a whole set of rules for figuring out the variable name from the message name. Worse still, the set of rules for generating argument names is probably too complex to put in the spec, and has to be up to the implementer of the language. Now, there might be several different sets of naming conventions depending on your compiler.
Upvotes: 0
Reputation: 7634
Because then you could not access the arguments' values. The methodName
, tag
, and path
names are the variables that the arguments are stored in. Your first example is similar to a C function like someMethodWithNametagpath(NSString *name, int tag, NSURL *path)
, while your second would be someMethodWithNametagpath(NSString*, int, NSUrl*)
.
Upvotes: 1