Block arguments should always be named. #41
Conversation
@@ -82,6 +82,8 @@ if (something == nil) { | |||
* Blocks should have a space between their return type and name. | |||
* Block definitions should omit their return type when possible. | |||
* Block definitions should omit their arguments if they are `void`. | |||
* Block definitions should always include argument names. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When would a block ever have an unnamed argument? Is this something you've actually seen before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm talking about the possibility of a method signature that looks like this:
- (void)methodThatTakesBlock:(void (^)(NSString *, NSData *, NSError *))block;
It's very frustrating when someone does that for two reasons:
- I have no idea what those arguments hold.
- When I'm typing the method name xcode autocompletes it and then immediately yells at me about unnamed block arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This actually brings up the point that I don't think you should ever omit an argument's name in a C function. For example, I much prefer
void GHAwesomeFunction(int descriptiveName1, int descriptiveName2, float descriptiveName3);
vs
void GHAwesomeFunction(int, int, float);
If you agree, can I add that rule too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, gotcha. How about phrasing it like this, then:
Parameters in block types should be named unless the block is initialized immediately.
This allows uses like:
void (^block)(NSString *) = ^(NSString *name) {
};
… where the purpose of the parameter is obvious from the literal, and doesn't need to be duplicated in the type.
This actually brings up the point that I don't think you should ever omit an argument's name in a C function.
We try to limit our conventions to things that might be contentious or not obvious – i.e., our conventions always start with an implicit "Use common sense." I don't think I've ever seen C declarations without parameter names in reputable code. It shouldn't be necessary to say that it's a bad idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Sounds good. I'll reword it but I think I'm going to use Arguments rather than Parameters since that's what's used in the bullet above and elsewhere (unless you have a different preference).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of the word "parameters" is intentional. Parameters are used in declarators, arguments are used in invocations.
Declarations are also different from definitions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification. For declarations vs definitions, I would say "declared property" because I'm telling the consumer/compiler that the property exists but I would say "class definition" because I'm describing the class. Am I on the right track?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Roughly, a declaration is an interface while a definition is the implementation.
This declares a constant which will be defined in another module:
extern NSString * const NotificationName;
This defines and initializes the constant:
NSString * const NotificationName = @"NotificationName";
Note that these concepts aren't always mutually exclusive. This both declares and defines a variable i
(since the definition marks its first appearance, it also functions as a declaration):
- (void)method {
int i;
}
Big 👍 from me to this. |
Yeah, definite 👍 from me. |
💎 |
Block arguments should always be named.
Block declaration arguments should always include variable names to follow the Objective-C style of named arguments.
Also what's your stance on typedef'ing blocks? I'm somewhat against it just because you can't easily determine the block arguments by command-clicking on a method or quickly looking at a header file.