GoogleSignIn-iOS icon indicating copy to clipboard operation
GoogleSignIn-iOS copied to clipboard

Reconsider our usage of dereference and access arrow operator (`->`)

Open mdmathias opened this issue 3 years ago • 1 comments

Is your feature request related to a problem you're having? Please describe.

We should consider replacing the usage of the arrow operator in our codebase. Doing so would, in many cases, make our code easier to read and in some cases make for less code in general. See the "Additional Context" below.

Describe the solution you'd like

Replace the use of -> with property access through self.

Describe alternatives you've considered

We could leave things as they are, but this would push the "problem" into the future.

Additional context

Here is an example of the enhancement I am thinking of, which arose in conversation during the review of #230. The suggestion below replaces the use of self->_delegate with weakSelf.delegate. Note that in cases where we are confident that there is no strong reference cycle in the block, we could simply use self.delegate as we do in self->_delegate. The example below also obviates the need for GTMAppAuthFetcherAuthorizationEMMChainedDelegate *_retained_self;, which is used within the preexisting code moved in #230.

#pragma mark - GTMAppAuthFetcherAuthorizationEMMChainedDelegate

@interface GTMAppAuthFetcherAuthorizationEMMChainedDelegate ()

@property (weak, nonatomic) id delegate;
@property (nonatomic) SEL selector;

@end

@implementation GTMAppAuthFetcherAuthorizationEMMChainedDelegate 

- (instancetype)initWithDelegate:(id)delegate selector:(SEL)selector {
  self = [super init];
  if (self) {
    _delegate = delegate;
    _selector = selector;
  }
  return self;
}

- (void)authentication:(GTMAppAuthFetcherAuthorization *)auth
               request:(NSMutableURLRequest *)request
     finishedWithError:(nullable NSError *)error {
  __weak GTMAppAuthFetcherAuthorizationEMMChainedDelegate *weakSelf = self;
  [GIDAppAuthFetcherAuthorizationWithEMMSupport handleTokenFetchEMMError:error
                                                              completion:^(NSError *_Nullable error) {
    if (weakSelf.delegate || weakSelf.selector) {
      return;
    }
    NSMethodSignature *signature = [weakSelf.delegate methodSignatureForSelector:weakSelf.selector];
    if (!signature) {
      return;
    }
    id argument1 = auth;
    id argument2 = request;
    id argument3 = error;
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    [invocation setTarget:weakSelf.delegate];  // index 0
    [invocation setSelector:weakSelf.selector];  // index 1
    [invocation setArgument:&argument1 atIndex:2];
    [invocation setArgument:&argument2 atIndex:3];
    [invocation setArgument:&argument3 atIndex:4];
    [invocation invoke];
  }];
}

@end

mdmathias avatar Sep 29 '22 20:09 mdmathias

see also if this helps issues

dav3yblaz3 avatar Nov 30 '24 17:11 dav3yblaz3