Ho cercato di spiegare a un collega la gravità di avere un codice duplicato in un progetto, su questo pezzo di codice:
+ (void)createIapInParse:(SKPaymentTransaction *)transaction {
Reachability *reach = [Reachability reachabilityWithHostname:@"www.facebook.com"];
if ([Social getFBUser]) {
NSString *iapId = [Util getBundleNameFromIdentifier:transaction.payment.productIdentifier];
PFObject *iap = [PFObject objectWithClassName:@"Iap"];
iap[@"iapId"] = iapId == nil ? [NSNull null] : iapId;
iap[@"userId"] = [Social getFBUser].objectID == nil ? [NSNull null] : [Social getFBUser].objectID;
iap[@"email"] = [Social getFBUser][@"email"] == nil ? [NSNull null] : [Social getFBUser][@"email"];
iap[@"country"] = [Util getDeviceCountry];
iap[@"installationId"] = [Util getInstallationId];
iap[@"score"] = [NSNumber numberWithLong:[GameScore getTotalScore]];
NSTimeInterval interval = [[NSUserDefaults standardUserDefaults] doubleForKey:@"timeSpentInApp"];
iap[@"timeSpentInApp"] = [NSNumber numberWithDouble:interval];
iap[@"retries"] = @0;
iap[@"transactionIdentifier"] = transaction.transactionIdentifier;
iap[@"transactionDate"] = transaction.transactionDate;
iap[@"transactionSource3G"] = [NSNumber numberWithBool:[reach isReachableViaWWAN]];
[iap saveInBackgroundWithBlock:^(BOOL succeded, NSError *error) {
NSString *query =[NSString stringWithFormat: @"...", iap.objectId, transaction.payment.productIdentifier];
[ZeeSQLiteHelper executeQuery:query];
}];
NSLog(@"Save in parse: %@", iap);
} else {
NSString *iapId = [Util getBundleNameFromIdentifier:transaction.payment.productIdentifier];
PFObject *iap = [PFObject objectWithClassName:@"Iap"];
iap[@"iapId"] = iapId == nil ? [NSNull null] : iapId;
iap[@"userId"] = @"-";
iap[@"email"] = @"-";
iap[@"country"] = [Util getDeviceCountry];
iap[@"installationId"] = [Util getInstallationId];
iap[@"score"] = [NSNumber numberWithLong:[GameScore getTotalScore]];
NSTimeInterval interval = [[NSUserDefaults standardUserDefaults] doubleForKey:@"timeSpentInApp"];
iap[@"timeSpentInApp"] = [NSNumber numberWithDouble:interval];
iap[@"retries"] = @0;
iap[@"transactionIdentifier"] = transaction.transactionIdentifier;
iap[@"transactionDate"] = transaction.transactionDate;
iap[@"transactionSource3G"] = [NSNumber numberWithBool:[reach isReachableViaWWAN]];
[iap saveInBackgroundWithBlock:^(BOOL succeded, NSError *error) {
NSString *query =[NSString stringWithFormat: @"...", iap.objectId, transaction.payment.productIdentifier];
[ZeeSQLiteHelper executeQuery:query];
}];
NSLog(@"Save in parse: %@", iap);
}
}
L'ho refactored per:
+ (void)createIapInParse:(SKPaymentTransaction *)transaction {
Reachability *reach = [Reachability reachabilityWithHostname:@"www.facebook.com"];
NSString *iapId = [Util getBundleNameFromIdentifier:transaction.payment.productIdentifier];
PFObject *iap = [PFObject objectWithClassName:@"Iap"];
NSTimeInterval interval = [[NSUserDefaults standardUserDefaults] doubleForKey:@"timeSpentInApp"];
iap[@"iapId"] = iapId == nil ? [NSNull null] : iapId;
iap[@"country"] = [Util getDeviceCountry];
iap[@"installationId"] = [Util getInstallationId];
iap[@"score"] = [NSNumber numberWithLong:[GameScore getTotalScore]];
iap[@"timeSpentInApp"] = [NSNumber numberWithDouble:interval];
iap[@"retries"] = @0;
iap[@"transactionIdentifier"] = transaction.transactionIdentifier;
iap[@"transactionDate"] = transaction.transactionDate;
iap[@"transactionSource3G"] = [NSNumber numberWithBool:[reach isReachableViaWWAN]];
if ([Social getFBUser]) {
iap[@"userId"] = [Social getFBUser].objectID == nil ? [NSNull null] : [Social getFBUser].objectID;
iap[@"email"] = [Social getFBUser][@"email"] == nil ? [NSNull null] : [Social getFBUser][@"email"];
} else {
iap[@"userId"] = @"-";
iap[@"email"] = @"-";
}
[iap saveInBackgroundWithBlock:^(BOOL succeded, NSError *error) {
NSString *query =[NSString stringWithFormat: @"...", iap.objectId, transaction.payment.productIdentifier];
[ZeeSQLiteHelper executeQuery:query];
}];
NSLog(@"Saved in parse: %@", iap);
}
e ha continuato a discutere con me dicendo che è la stessa cosa.
Il suo argomento principale era che "è un buon programmatore e può capire e leggere anche la prima versione abbastanza velocemente", così non gli importa se è scritto così o meno.
La domanda è: ho davvero torto? Non è così importante per tutti? È una questione soggettiva o non capisce che non è la stessa cosa?