Why Inline Comments Are Generally a Bad Idea

on October 8th, 2009 by Tom 3 Comments Coding Style/Conventions

Bellow is a single function commented in two different ways. Which one is better?

NSString* MD5StringOfString(NSString* inputStr)
{
	//UTF8 encoding is used so the hash can be compared with hashes of ASCII strings
	NSData* inputData = [inputStr dataUsingEncoding:NSUTF8StringEncoding];

	unsigned char outputData[CC_MD5_DIGEST_LENGTH];
	CC_MD5([inputData bytes], [inputData length], outputData);

	NSMutableString* hashStr = [NSMutableString string];
	int i = 0;
	for (i = 0; i < CC_MD5_DIGEST_LENGTH; ++i)
		[hashStr appendFormat:@"%02x", outputData[i]];
 
	return hashStr;
}
NSString* MD5StringOfString(NSString* inputStr)
{
	//convert the string to UTF8 encoded byte data
	NSData* inputData = [inputStr dataUsingEncoding:NSUTF8StringEncoding];

	//calculate the hash
	unsigned char outputData[CC_MD5_DIGEST_LENGTH];
	CC_MD5([inputData bytes], [inputData length], outputData);

	//convert hash to a hexadecimal string
	NSMutableString* hashStr = [NSMutableString string];
	int i = 0;
	for (i = 0; i < CC_MD5_DIGEST_LENGTH; ++i)
		[hashStr appendFormat:@"%02x", outputData[i]];
 
	//return the hexadecimal string
	return hashStr;
}

Read full post →

Coding Tip: Replace Complicated Conditions With Boolean Variables

on October 3rd, 2009 by Tom Coding Tips

Consider the following if statement:

if(dragOperation != NSDragOperationCopy && NSPointInRect(currentMouseLocation, self.bounds)){
    //do something
}

Even though you may have worked out what the condition represents, it probably took you a little longer than it should. It’s complicated, making it time consuming to read, and prone to bugs upon modification. Thankfully, there is an easy remedy:
Read full post →