4 ms·
As an Android developer who's required to learn iOS just to port one of my projects, I do find Objective-C's syntax to be strange--quite sloppy, in fact. I trie
by mattquiros 14y ago
As an Android developer who's required to learn iOS just to port one of my projects, I do find Objective-C's syntax to be strange--quite sloppy, in fact. I tried to list down the things I don't like about the language while going through my reference, and here are some of them so far:
- Defining the method signatures in a header file or an @interface section, then repeating the same method signatures just to implement them in @implementation. I just think it's an awful waste of effort.
- The idea of categories. The way I see it, there doesn't seem to be a reason not to subclass since categories won't make sense anyway if you don't include the original class in #import.
- @optional directive in protocols. I think of protocols as Java interfaces, and I assume that's the intended purpose for having them, so having an @optional directive seems quite pointless to me.
- Obscure variable scoping. I find myself having to memorize too many visibility rules--ones that apply to the objects themselves (what attributes they inherited, local/block scoping, etc.), ones that apply to files themselves (defining instance variables within @interface, global BUT in-file variables within @implementation, the extern keyword, etc.). This is much simpler and elegantly done in Java where you don't need to switch between thinking of your program as a bunch of interacting objects and as a bunch of files.
- Which makes me think that Objective-C just doesn't have good OOP in the first place.
- Conditional compilation. When is this ever useful? I just can't visualize having to write this (maybe for games?), but again, I never had to do this in Java.
- lnanek2 14y agoI've had to use conditional compilation quite a few times. Often due to strange things like target architecture and things like that. I noticed my native iOS stuff runs lightning fast, though. So think like you are writing souped up C code. You have to put up with header files and macros and conditional compilation sometimes for different versions of the app or to run on simulator architecture vs. real, etc. but it is very efficient building and running. Categories can be nice to avoid subclassing. Subclassing can become a nightmare when one controller extends another which extends another which extends the Google Analytics one. There's so much hidden behavior and you can't even easily disable the log spamming Google Analytics one easily. Not compiling in the init code gives you errors in the log all over.
- archangel_one 14y agoConditional compilation is pretty widely used in C, so I guess Obj-C inherits it from there. The most obvious case is when your code is running on different architectures - consider #ifdef OSX / #ifdef iOS or similar. In Java you're theoretically writing for only one architecture, so I guess it'd be less useful. I don't know Obj-C well at all, but no arguments on the rest :)
- kevinh 14y agoIt's amusing because the opposites of some of these are things that bother me about Java. Having everything in one file? Messy. Not having categories? Sad. @optional is nice if you want to be able to degrade to a default case. Scoping is primarily just C so YMMV. ObjC is a purer form of OOP than Java is. Not to mention Java has it's own quirks in my books: no unsigned variables, no typedefs, calling a function on a null pointer creates an exception instead of returning null (what's up with that?), etc. And don't get me started with how Android uses xml (poorly). Really with languages I think it's primarily what you're used to.
- cageface 14y agoI'm mainly an iOS guy but the way Android manages layouts and resources in XML beats the pants off of xibs + interface builder for non-trivial UIs.
- GoldenMonkey 14y agoYou've gotta be kidding. What user interface tool does android + eclipse provide? There is nothing there, you've gotta hand roll it in xml. The UI tool is limited and a pain to work with. I think android's ability to use folders to support different layouts and resources beats the file naming conventions necessary for supporting the same scenario in iOS... i.e. myLogo~ipad.png
- cageface 14y agoI leave the WYSIWYG for the designer - it quickly falls apart for actually programming complex UIs. My job is to translate PSDs into working UI. There's a good reason we abandoned WYSIWYG for web design years ago and a modern mobile app is no less dynamic. I can change the underlying grid size, padding and item spacing for every screen in my entire app in one line. Have fun doing that in iOS.
- yareally 14y agoTry the UI tool for Android on Intellij IDEA, much better.
- SoftwareMaven 14y agoThere is more to OO than Java. :) ObjC has some syntax warts, due to being an extension of C like C++, but it is far better thought out than C++, IMO. The method signature issue is a direct result of the C history. Far easier (at the time) to tell the compiler in advance about signatures than to force it to compile everything twice to find them (which is, roughly speaking, what Java does, except the second "compilation" happens at run time). ObjC is a truer OO, IMO, because it focuses more on objects and message passing rather than method calls. As Categories and optional protocols show, this provides a far more flexible approach. The conditional compilation comment confuses me and makes me think you don't really understand what's happening under the hood. When you compile Java, you get byte code tat requires a VM. When you compile ObjC, you get native code. Now imagine dealing with an architecture as different as PowerPC and x86. All those differences were conditionally compiled into your VM. In ObjC, you need to deal with them yourself (and all the other positives and negatives that implies).
- svachalek 14y agoObjective-C is much more object-oriented than Java, mainly due to the parts of the language you've chosen to ignore because they don't remind you of Java. See the section on "Blub" in http://paulgraham.com/avg.html http://paulgraham.com/avg.html Conditional compilation is a terrible, terrible thing but for the opposite reason you think; it's just too useful for too many things and almost inevitably leads to shipping code that's never/barely been tested. Java left it out to protect WORA, but the only reason you can really (sort of) live without it in performance-critical apps is due to the JIT doing the same sort of things for you invisibly.
- jimbokun 14y agoThe idea behind Categories is to add methods to an existing class. This means if you get back an NSString from an API call, for example, you can call a method from a category declared on NSString, without having to convert it to the instance of some subclass. This can be very convenient, and I can't think of anyway to do the equivalent in Java. It's similar in spirit to "monkey patching" in Ruby and similarly dynamic languages (but won't allow you to replace existing methods, so not as dangerous).
- MBCook 14y ago> It's similar in spirit to "monkey patching" in Ruby and similarly dynamic languages (but won't allow you to replace existing methods, so not as dangerous). My understanding is that you can create methods with the same signatures, but which one will end up getting called is completely undefined, making it a worse idea that it usually is. It can be very handy thing though, I really like it.