5

Here is what I'm looking for:

I'd like to separate pieces of functionality into modules or components of some sort to limit visibility of other classes to prevent that each class has access to every other class which over time results in spaghetti code.

In Java & Eclipse, for example, I would use packages and put each package into a separate project with a clearly defined dependency structure.

Things I have considered:

  1. Using separate folders for source files and using Groups in Xcode:
    • Pros: simple to do, almost no Xcode configuration needed
    • Cons: no compile-time separation of functionality, i.e. access to everything is only one #import statement away
  2. Using Frameworks:
    • Pros: Framework code cannot access access classes outside of framework. This enforces encapsulation and keeps things separate
    • Cons: Code management is cumbersome if you work on multiple Frameworks at the same time. Each Framework is a separate Xcode project with a separate window
  3. Using Plugins:
    • Pros: Similar to Frameworks, Plugin code can't access code of other plugins. Clean separation at compile-time. Plugin source can be part of the same Xcode project.
    • Cons: Not sure. This may be the way to go...

Based on your experience, what would you choose to keep things separate while being able to edit all sources in the same project?

Edit:

  • I'm targeting Mac OS X
  • I'm really looking for a solution to enforce separation at compile time
  • By plugins I mean Cocoa bundles (http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/LoadingCode/Concepts/Plugins.html)
Mark
  • 1,447
  • 2
  • 14
  • 26
  • 2
    Major con to plugins: they're not valid under iOS. Hope you're targeting OS X. Under Xcode 4 managing multiple targets and projects in the same window - Frameworks included - is pretty easy, you just need to go File > New Workspace. – Morgan Harris Jul 08 '11 at 13:52

4 Answers4

9

I have worked on some good-sized Mac projects (>2M SLOC in my last one in 90 xcodeproj files) and here are my thoughts on managing them:

  • Avoid dynamic loads like Frameworks, Bundles, or dylibs unless you are actually sharing the binaries between groups. These tend to create more complexity than they solve in my experience. Plus they don't port easily to iOS, which means maintaining multiple approaches. Worst, having lots of dynamic libraries increases the likelihood of including the same symbols twice, leading to all kinds of crazy bugs. This happens when you directly include some "helper" class directly in more than one library. If it includes a global variable, the bugs are awesome as different threads use different instances of the global.

  • Static libraries are the best choice in many if not most cases. They resolve everything at build time, allowing code stripping in your C/C++ and other optimizations not possible in dynamic libraries. They get rid of "hey, it loads on my system but not the customer's" (when you use the wrong value for the framework path). No need to deal with slides when computing line numbers from crash stacks. They catch duplicate symbols at build time, saving many hours of debugging pain.

  • Separate major components into separate xcodeproj. Really think about what "major" means here, though. My 90-project product was way too many. Just doing dependency checking can become a very non-trivial exercise. (Xcode 4 can improve this, but I left the project before we ever were able to get Xcode 4 to reliably build it, so I don't know how well it did in the end.)

  • Separate public from private headers. You can do this with static libs just as well as you can with Frameworks. Put the public headers in a different directory. I recommend each component have its own public include directory for this purpose.

  • Do not copy headers. Include them directly from the public include directory for the component. Copying headers into a shared tree seems like a great idea until you do it. Then you find that you're editing the copy rather than the real one, or you're editing the real one, but not actually copying it. In any case, it makes development a headache.

  • Use xcconfig files, not the build pane. The build pane will drive you crazy in these kinds of big projects. Mine tend to have lines like this:


common="../../common"
foo="$(common)/foo"
HEADER_SEARCH_PATHS = $(inherited) $(foo)/include

  • Within your public header path, include your own bundle name. In the example above, the path to the main header would be common/foo/include/foo/foo.h. The extra level seems a pain, but it's a real win when you import. You then always import like this: #import <foo/foo.h>. Keeps everything very clean. Don't use double-quotes to import public headers. Only use double-quotes to import private headers in your own component.

  • I haven't decided the best way for Xcode 4, but in Xcode 3, you should always link your own static libraries by adding the project as a subproject and dragging the ".a" target into your link step. Doing it this way ensures that you'll link the one built for the current platform and configuration. My really huge projects haven't been able to convert to Xcode 4 yet, so I don't have a strong opinion yet on the best way there.

  • Avoid searching for custom libraries (the -L and -l flags at the link step). If you build the library as part of the project, then use the advice above. If you pre-build it, then add the full path in LD_FLAGS. Searching for libraries includes some surprising algorithms and makes the whole thing hard to understand. Never drop a pre-built library into your link step. If you drop a pre-built libssl.a into your link step, it actually adds a -L parameter for the path and then adds -lssl. Under default search rules, even though you show libssl.a in your build pane, you'll actually link to the system libssl.so. Deleting the library will remove the -l but not the -L so you can wind up with bizarre search paths. (I hate the build pane.) Do it this way instead in xcconfig:


LD_FLAGS = "$(openssl)/lib/libssl.a"

  • If you have stable code that is shared between several projects, and while developing those projects you're never going to mess with this code (and don't want the source code available), then a Framework can be a reasonable approach. If you need plugins to avoid loading large amounts of unnecessary code (and you really won't load that code in most cases), then bundles may be reasonable. But in the majority of cases for application developers, one large executable linked together from static libraries is the best approach IMO. Shared libraries and frameworks only make sense if they're actually shared at runtime.
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Very interesting. I've not thought of static libraries. This may very well be the way to go for me. I have code that is shared between two apps, but I don't mind linking each one separately into one binary. Thanks! – Mark Jul 08 '11 at 15:38
  • I've found an excellent tutorial showing how to achieve what you suggested above in Xcode: http://www.clintharris.net/2009/iphone-app-shared-libraries/ – Mark Jul 08 '11 at 20:01
  • To answer some of your points about Frameworks, naming conflicts can be easily avoided by using a "namespace" prefix as Apple does in its own Frameworks. That's why every class in `CoreAnimation` starts with "CA", and everything in `UIKit` starts with "UI". You can link statically against a Framework if you prefer. There is nothing that stipulates that Frameworks must use dynamic linking. In fact for iOS, they can only be linked statically. And they port [just fine](https://github.com/kstenerud/iOS-Universal-Framework) to iOS, thanks to the efforts of Karl Stenerud. – aroth Jul 09 '11 at 00:07
  • Regarding namespaces and dynamic linking, the problem is when you have a very large body of code with extensive reuse within the same team. They use the same prefix. So you wind up with MYHandyUtility() getting copied into multiple frameworks and things explode when you link dynamically. To avoid that you either make your frameworks very small (then there are too many) or very large (then you bloat your product). Apple avoids this somewhat by having entire teams devoted to the framework, but most orgs don't have a special team. Bloat in UIKit also doesn't matter as much because it's shared. – Rob Napier Jul 09 '11 at 21:49
  • Regarding static Frameworks, that's useful if you have someone to distribute them to, and I've considered exactly that when sharing code across many products, but for a single large team the overhead of packaging a Framework often doesn't give you a payback over just reading the include files from the source tree. The benefit of Frameworks is when you can isolate the Framework development from the App development to drive reuse (often by having different people do frameworks from apps, with their own testing, docs, schedules etc.) But many orgs don't staff that way. – Rob Napier Jul 09 '11 at 21:55
4

My suggestion would be:

  1. Use Frameworks. They're the most easily reusable build artifact of the options you list, and the way you describe the structure of what you are trying to achieve sounds very much like creating a set of Frameworks.

  2. Use a separate project for each Framework. You'll never be able to get the compiler to enforce the kind of access restrictions you want if everything is dumped into a single project. And if you can't get the compiler to enforce it, then good luck getting your developers to do so.

  3. Upgrade to XCode4 (if you haven't already). This will allow you to work on multiple projects in a single window (pretty much like how Eclipse does it), without intermingling the projects. This pretty much eliminates the cons you listed under the Frameworks option.

And if you are targeting iOS, I very strongly recommend that you build real frameworks as opposed to the fake ones that you get by using the bundle-hack method, if you aren't building real frameworks already.

aroth
  • 54,026
  • 20
  • 135
  • 176
  • I'm targeting Mac OS X. Maybe it really is time to upgrade to Xcode 4 and use frameworks... – Mark Jul 08 '11 at 14:18
  • Strangely I would really like to see this answer elaborated for 2015 for Xcode 7.x. I'm facing the same sorts of problems with scale and am finding it impossible to manage test app targets as well as unit test targets without splitting my app project into many smaller ones. The question is, how are you then supposed to share internal code, as well as managing external dependencies as well. – fatuhoku Nov 20 '15 at 10:02
2

I've managed to keep my sanity working on my project which has grown over the past months to fairly large (number of classes) by forcing myself to practice Model-View-Control (MVC) diligently, plus a healthy amount of comments, and the indispensable source control (subversion, then git).

In general, I observe the following:

"Model" Classes that serialize data (doesn't matter from where, and including app's 'state') in an Objective-C 1 class subclassed from NSObject or custom "model" classes that inherits from NSObject. I chose Objective-C 1.0 more for compatibility as it's the lowest common denominator and I didn't want to be stuck in the future writing "model" classes from scratch because of dependency of Objective-C 2.0 features.

View Classes are in XIB with the XIB version set to support the oldest toolchain I need to support (so I can use a previous version Xode 3 in addition to Xcode 4). I tend to start with Apple provided Cocoa Touch API and frameworks to benefit from any optimization/enhancement Apple may introduce as these APIs evolve.

Controller Classes contain usual code that manages display/animation of views (programmatically as well as from XIBs) and data serialization of data from "model" classes.

If I find myself reusing a class a few times, I'd explore refactoring the code and optimizing (measured using Instruments) into what I call "utility" classes, or as protocols.

Hope this helps, and good luck.

Global nomad
  • 1,037
  • 12
  • 25
0

This depends largely on your situation and your own specific preferences.

If you're coding "proper" object-oriented classes then you will have a class structure with methods and variables hidden from other classes where necessary. Unless your project is huge and built of hundreds of different distinguishable modules then its probably sufficient to just group classes and resources into folders/groups in XCode and work with it that way.

If you've really got a huuge project with easily distinguishable modules then by all means create a framework. I would suggest though that this would only really be necessary where you are using the same code in different applications, in which case creating a framework/extra project would be a good way to effectively copy code between projects. In practically all other cases it would probably just be overkill and much more complicated than needed.

Your last idea seems to be a mix of the first two. Plugins (as I understand you are describing - tell me if I'm wrong) are just separated classes in the same project? This is probably the best way, and should be done (to an extent) in any case. If you are creating functionality to draw graphs (for example) you should section off a new folder/group and start your classes and functionality within that, only including those classes into your main application where necessary.

Let me put it this way. There's no reason to go over the top... but, even if just for your own sanity - or the maintainability of your code - you should always endeavour to group everything up into descriptive groups/folders.

Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
  • Thanks for the suggestions. I'm using groups today as well as object oriented methods, but that relies on my own discipline to not include a header and introduce a dependency that shouldn't be there. By plugins I mean actual Cocoa bundles: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/LoadingCode/Concepts/Plugins.html – Mark Jul 08 '11 at 14:14