As you might recall, if you're a regular reader of this blog or are married to me (Hi, Mrs! :-) ), I am playing with MacRuby. A decidedly fun [if you call writing code 'fun'] language and environment. But there's a problem... What do you call methods, class and variables? More specifically - how do you format those names?
The problem comes from the underlying foundation - something called "Cocoa" and the language that is written in, Objective-C. The standard nomenclature for which is best described as "verbose". There's very little room within the recommendations for abbreviated naming. "Application" is written as "Application". Most mortally inclined programmers would, I think, prefer the shorter "app". Interestingly, the main file for a MacRuby app is "AppDelegate.rb"; that, however, seems to be acceptable! Anyway, it contains a class "AppDelegate" and a method "applicationDidFinishLoading".
(No, I'm not breaking any NDA; this is open source code.)## AppDelegate.rb# Sandbox## Created by Carolyn Ann on 11/27/12.# Copyright 2012 Carolyn Ann. All rights reserved.#class AppDelegatedef applicationDidFinishLaunching(a_notification)# Insert code here to initialize your applicationendend
If this conformed to Ruby standards, it would be:
Some subtle differences, right? (Okay, if you're into waging religious wars over braces "{ ... }", then the differences are about as subtle as an oak tree to the back of your head. Most people don't get that worked up about such things. So: work with me, here. :-) )## app_delegate.rb# Sandbox## Created by Carolyn Ann on 11/27/12.# Copyright 2012 Carolyn Ann. All rights reserved.#class AppDelegatedef app_finished_launching(a_notification)# Insert code here to initialize your applicationendend
Now, there are two schools of thought on this: go with the Cocoa 'camelCase' standard, and go with the Ruby 'snake_case' standard. (Duh...) As is often the case, if I'm not sure about something and try to explain whatever it is to someone else, my own thoughts are clarified. So... I'd say the Cocoa standard is the one to use.
Here's why: there's very little that can be written in a true cross-platform way. While it is possible to use RubyGems - and you should! - that code can be a different style. Your own code, however, should be adhere to a style that is known and accepted for that platform. OS X and iOS (that's behind the iPad and iPhone) have a particular style - it is known and it is generally accepted. If you stick to the Apple supplied standard, you avoid strange looking code:
That just looks odd. Although it does have the advantage that you can tell where a bit of code is located. (I just declarified* the issue for myself...) Also, note the snake_cased option. Those are camelCased in Objective-C/Cocoa.class AppDelegatedef applicationDidFinishLaunching(a_notification)call_this_bit_of_ruby_codeandNowCallhisBitOfOSXendend
*No, that's not a word. ... However, it is now. :-)
Knowing I'm calling Ruby code can be useful. What of my own code? What if I want to write a wrapper function?
class SomeClassdef someMethodWithAReallyLongName(and_a_lot_of_options)theWrappedMethod not_set_options, preset_optionsenddef some_method_with_a_really_long_name(lots_of_options)theWrappedMethod not_set_options, preset_optionsenddef long_named_method(lots_of_options)theWrappedMethod not_set_options, preset_optionsendend
I gotta tell you - that first method looks "cleaner", more consistent. Now, I know I'll be calling third-party code, and I won't be wrapping those; I'll call them as Ruby code. Besides being the indicator it's not my code, I'm not inclined to change other people's code to satisfy (or have) some religious lust for naming consistency!
I took a look around, and there's no agreement on naming conventions; mind you, there's not a lot of MacRuby open source code on GitHub. 85 pages of mostly forked MacRuby code. The application code I could find had the same problem - some coders used Ruby's snake_case, and some used Cocoa/Obj-C's camelCase. I am inclined to play with the conventions and see what's best. Like I said, there's not much chance of writing cross-platform code (heck, you need to use cross-platform coding between iOS and OS X!), so the biggest issue seems to be which style should win the religious wars that always inhabit these topics. I think I'll stick to camelCase; mostly because I've never really liked snake_case. Your mileage, as the saying goes, may vary. :-)
Carolyn Ann
I took a look around, and there's no agreement on naming conventions; mind you, there's not a lot of MacRuby open source code on GitHub. 85 pages of mostly forked MacRuby code. The application code I could find had the same problem - some coders used Ruby's snake_case, and some used Cocoa/Obj-C's camelCase. I am inclined to play with the conventions and see what's best. Like I said, there's not much chance of writing cross-platform code (heck, you need to use cross-platform coding between iOS and OS X!), so the biggest issue seems to be which style should win the religious wars that always inhabit these topics. I think I'll stick to camelCase; mostly because I've never really liked snake_case. Your mileage, as the saying goes, may vary. :-)
Carolyn Ann
I like the comment of: it doesn't matter what standard you use as long as you use one.
ReplyDeleteOne being the most important point :)
About the wrapping of third party code though. I wrap third party code for one reason above all. I never know when I am going to change the third party tool, and I would rather just change my wrapper, than my whole application where it calls the third party code :)
The only thing that I don't like is:
applicationDidFinishLaunching
I'm not sure if this is supposed to be a boolean saying that loading is complete, or an event that fires when it's complete (the name says the first, the rest of the code says the second ;p)
Sorry, my inner reviewer has come out to play :) But I'll admit I know nothing about Ruby, I'm looking at this as a C# developer :)
I didn't think of that reason for wrapping third party code! Thanks! :-)
DeleteI agree with you on the "pick one and stick with it!" thing; unfortunately my dilemma (dilemna in the rest of the world...) is that I have to handle two competing standards. Which argues for handling the Ruby standard as a third party and simply sticking with the Obj-C/Cocoa standard.
The applicationDidFinishLaunching thing is a method provided by RubyMac code and Cocoa; it's been around for about 5 years, as far as I can tell. I agree with you about the naming of it; that caused me some confusion when I first came across it!. Unfortunately, it's something I'm stuck with. Basically, it's called just after the application finished its startup process. (I've just tried to find a decent example... This is the best one I saw in a very quick Google: http://www.cimgf.com/2008/03/26/cocoa-tutorial-awakefromnib-vs-applicationdidfinishlaunching/ A "nib" is a window file; Apple wraps all its window files into a specialized XML file that it calls a NIB; the compiled version is a XIB. Please don't ask me why... I don't know! :-\ (It's because Steve Jobs ran NextStep - there's a lot of stuff with "NS" as its prefix because of that. That was in the late 80's and early 90's! So it was awhile ago.
But yeah, I'm trying to standardize on one particular style, but only for MacRuby work. I actually don't care what style I should use - I'm not wedded to where braces should go, unlike one or two programmers I've known - I'd like to be consistent. It just makes reading code easier! (Esp. mine...) As we often said in networking: that's the great thing about standards... There's so many of them!