Category: Coding Style/Conventions

Why camelCaps Are Superior To_Underscores

— Category: Coding Style/Conventions

I've always preferred camel caps to underscores when it comes to naming conventions, but I've never really known why. Recently I've been writing a lot of Python, where the standard is to use underscores for most names, and now I realise why I don't like underscores. I hope to make my argument for camel caps objective — or, failing that, at least better than the mouth-frothing, religious reactions you normally get in these “X is better than Y” type of discussions.

For Those Who Have Never Used Objective-C

— Category: Coding Style/Conventions

There is one feature of the Objective-C language that I really love: the method naming. Let me explain with an example.

Here is a nasty call to a C function from the Win32 API that has 12 arguments:

hwnd = CreateWindowEx(WS_EX_LAYERED,
                      TEXT("Hello"),
                      TEXT("World"),
                      WS_OVERLAPPEDWINDOW,
                      10, 
                      10,
                      400, 
                      400,
                      NULL, 
                      NULL,
                      hInstance, 
                      NULL);

Pick an argument, any argument. What does it do? You can probably guess a couple of them, but basically you're forced to look up the documentation. Sure, 12 arguments is a bit excessive, but even three or for argument functions can be ambiguous. What if you're trying to understand a function call that is being passed three number literals as arguments? Even if you know the function, you'll probably have to look up the documentation just to remember the order of the arguments.

Now for the equivalent in Objective C:

Why Inline Comments Are Generally a Bad Idea

— Category: 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;
}