Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to name iOS-related elements in code?

I've been doing a lot of iPhone development lately, and I have a naming issue for which I can't think of a good solution. The problem is that somethimes I have to refer to "iOS" in variable, namespace, or class names. How should I do it? Suppose I have a version of "MyClass" that is designed for iOS. Should I call it:

iOSMyClass?

This is bad. Class names are supposed to start with a capital letter.

IOSMyClass?

This is bad. Now my class looks like an interface.

AppleMyClass?

This is better, but what if I create a version of the class for Macs?

AppleMobileMyClass?

This is better, but it's starting to get pretty verbose.

Any thoughts? I'm developing desktop software using C#.

like image 465
Peter Ruderman Avatar asked Feb 02 '23 20:02

Peter Ruderman


2 Answers

Suggest going with your second choice, but a slight mod: IosMyClass. Consider the convention of 3 letter acronyms being Pascal cased (i.e. MVC in System.Web.Mvc). Of course, Apple flips that around in its implemention with iOS. However it sounds as if this is for the .NET space, and the desire is to follow its conventions.

There are handfuls of other classes (in the .NET Framework even), that start with the letter I. Admittedly, it's not ideal, as it's so close to the convention of prefixing interfaces with I. However, interfaces start with two upper case letters (I[A-Z]). That identifies them as an interface. So IosMyClass, by convention, is not an interface, but IIosFoo would be.

like image 196
p.campbell Avatar answered Feb 05 '23 10:02

p.campbell


I know you specify C# as your language here, but I thought I should point out the standard Objective-C naming conventions, which you'll see as you look at other iOS code.

Objective-C lacks the concept of namespaces, so it is generally recommended that you prefix your class names with 2-3 capital letters that signify your company or framework to avoid naming collisions. Apple has been recommending 3 characters lately, because frameworks like Core Plot that use 2 letters have started having collisions with private classes in Apple's frameworks (like CPImage, in this case). For another take on the naming guidelines, see here.

For that reason, I would recommend against the use of a generic iOS prefix, because Apple might use that internally for something and cause your application to break. Admittedly, I don't know how MonoTouch C# classes interact with the Cocoa framework classes, but I'd want to avoid any chance of a problem like this.

Again, if I may point to the Core Plot framework, there is some code within that framework that is specific to Mac and some to iOS, yet separate classes are not used for each platform. Platform-specific methods are extracted from the main class files and placed in categories. These categories have the same name, but the Mac target compiles using one file and the iOS target uses another. Similarly, entire classes that are specific to each platform are given the same name and interface, but put in different files. Which file is used at compilation is determined by the platform target.

Therefore, I'd recommend against giving iOS-specific classes a unique name unless they have no analogue on your other target platforms.

like image 42
Brad Larson Avatar answered Feb 05 '23 10:02

Brad Larson