So I have noticed that there is a lot of animosity towards Objective-C among programmers. What's your take? Is it a vendor lock-in thing against Apple? General antipathy towards Apple? The syntax? What's your view on this?
With the advent of the iPhone SDK, Obj-C has gotten a lot more attention lately, and I am curious what people on SO's opinions are.
I personally fought the syntax at first but have gotten more and more used to it now. I really like the named arguments. I have some pet peeves with how things are done in Obj-C vs other languages, but I will refrain from comment on them here.
Honestly, I think that most of the complaints are coming from Windows / Java / web programmers migrating to the platform who just plainly don't want to learn something new. Witness the number of "how can I do development on Windows" or "how can I program the iPhone using JavaScript" questions that have been asked around here. Many of the posts I've read bashing Cocoa and the related development tools come from long-time Windows programmers who are upset that Xcode is not Visual Studio and Cocoa is not .Net. Developers typically choose to write for the Mac because they want to and are willing to invest the time to do it right, but the gold rush on the iPhone is bringing in a lot of people who just want a quick buck.
I don't love Objective-C, nor do I love any other language (however, there are some that I hate). What I do love are the frameworks that make up Cocoa. They are (for the most part) consistent and well-architected. They have allowed me to create better quality applications in a much shorter period of time than any other platform I've worked on. For example, I keep discovering cool things in Core Animation that would be unbelievably painful to do elsewhere. Even simple Foundation classes like NSString and NSDictionary have made my life easier.
It really doesn't have to do with the language itself. It's more of a question of programming style and religion.
First off, as others have mentioned, is the syntax. C-style programmers are used to dotted syntax. Objective-C, on the other hand, uses brackets. (Though I believe Objective-C 2.0 allows you to use dot notation for getting/setting properties.)
More importantly, however, is the paradigm. It's the perennial debate between statically typed and dynamically typed languages. Programmers are biased between them, so they will choose their language accordingly.
Objective-C and C++ appeared roughly at the same time. Whereas C++ added object orientation to C by combining it with Simula, Objective-C combined C with Smalltalk (which in turn took inspiration from Lisp).
So, in my opinion, it's not a question of why programmers hate or love Objective-C, it's a question of what OOP school of thought they are aligned to.
Objective-C is kind of primitive compared to other languages in the same mental space. It's radically object-oriented, but only to a point and then all of a sudden it's just plain C. The way it combines a Smalltalk-style type system with a C-stye type system has a lot of seams showing. And the named arguments are great for their self-documenting nature, but other modern languages get the same benefit with greater flexibility. Languages like Ruby, Python and, heck, even Lisp manage to have named arguments (and often more per method) without having every method call read like a treatise on the human condition.
Now that I'm done bashing Objective-C: I'd still much rather program in Objective-C than, say, Java or C++. In its day, Objective-C was a pretty clever hack. But now there are other major languages that operate in the same space as Objective-C and are more powerful.
I've been using Objective-C for seven years. Others have been using it longer and still seem to be happy, and I don't hate it, but I enjoy working in Ruby more.
A good language helps you think in a different way. I came to Objective-C after getting an iPhone and fancied learning how to write for it. I liked what I found! I'm an experienced C++ developer, and the concept of categories struck me as an interesting way of constructing and extended class libraries, so in that regard, it was worth learning!
I think there are two things that put people off the most initially:
The association with Apple. While Objective-C was not invented by Apple language - nor is theirs the only implementation [1] - it's only really Apple who are using it in any mainstream way.
The syntax, which looks very alien at first to people from a C/C++/C#/Java background.
When you start using it for a while, other differences tend to build on prejudices from those initial reactions and reinforce them (e.g. the static vs dynamic thing).
Like many, I started using ObjC to write iPhone software (here's my plug cue again: www.vconqr.com [2]). It took me a few days to "get it", and a couple of weeks to really feel comfortable with it - but further a-ha moments where further down the line. The things I really like now are:
Beyond the initial "getting used to it" phase, the biggest moment for me was when I ported a big chunk of my original code into C++ (because I wanted to do a lot of small allocations into different types of containers efficiently, and C++ seemed to be a better fit). Although I used boost libraries - including BOOST_FOREACH (which handled a lot of boilerplate), I still found that the C++ code was vastly more verbose, full of boilerplate and less expressive (and I've been doing C++ for about 18 years).
I don't know if I'd say it's my favourite language - or if that even has any meaning these days - but it certainly has it's place and I'm glad I learnt it - even apart from the iPhone development.
[Edit] I originally had a plug here for my ACCU conference presentation on Objective-C [3], which took place last April. Since people still seem to be reading this (I still get votes up from time to time) I'll update the plug to my Stackoverflow DevDays (2009) presentation on iPhone Development [4] that's coming up soon in London. I'll be spending about half the material on pure Objective-C.
[Edit 2] I'll update my plug for the Stackoverflow conference to my blog entry covering what actually happened [5].
[1] http://gcc.gnu.org/Jonathan Wolf Rentzsch wrote the ultimate answer to this question way back in 2003:
Original blog post is dead, but here is a Wayback Machine link: 10 Things I Love about ObjC and 15 Things I Hate about ObjC [1]
[1] http://web.archive.org/web/20100105030238/http://rentzsch.com/papers/loveHateObjC1) Syntax - I never liked SmallTalk and I'm primarily a C# dev these days, so the syntax was quite a change. The named arguments didn't make that much sense for a bit.
2) Framework/Base Classes - When I first started trying to do things in Objective-C, it was extremely frustrating because there were only a handful of classes to use...which felt like a lot of power was lost (compared to using something like the .NET framework). Everything just felt more difficult than it should have been for basic tasks.
After having used the language quite a bit more (and having built an iPhone app now), neither of those have been a problem. I do like the syntax now - it doesn't feel odd at all now - and the framework that is there has tons of power, it was just hidden away in additional methods that I hadn't noticed. Until you go through and really read all of the documentation on the core objects, it's easy to overlook the advanced methods that are already in place.
I think a lot of this stems from people not getting past the syntax and C heritage.
They see the programming looks a lot like C, and so think programming in it must be like C. Only it's nothing like C. You are using whole different libraries, different method call syntax, not really using pointers, etc. etc.
They see you "have to manage your own memory" and assume that is like C. But reference counting is not really anything like using malloc, especially with Autorelease thrown in the mix. It's a lot more like working in a GC environment than dealing with Malloc.
They see braces around calls and named parameters, and think it looks way too cumbersome - even though as with any modern IDE the typing cost of a method is almost nil or at least constant for any degree of complexity. Also others have mentioned how they like named parameters being added to other languages now - well Objective-C has always had them, and furthermore has a very nice convention built up around how you do the naming. Also people cannot see the tremendous flexibility behind message passing for calls, they just think call() has been replaced by [self call] and think they are pretty much identical apart from syntax.
I've see the comment repeated a few times that programming in Objective-C is like going back 20 years. The remark is half-accurate - it's like going back in time 20 years and then going forward 20 again, only in a different direction. Objective-C is as modern as any language, it's just that it took a different path to get to where it is today rather than following the C++/Java/.Net evolution chain. Objective-C is a kind of alt-history (or even steampunk) variant of other languages, because it has come to where it is from different choices taken and once you understand those choices it makes a lot of sense. And it continues to evolve in well-thought out ways, which is really the most important thing.
init
method that takes the named parameters contentRect
, styleMask
, backing
, defer
and screen
— there's just an initWithContentRect:styleMask:backing:defer:screen:
method. You can't reorder them, can't omit ones you don't care about. The name is atomic, and the only difference from a regular function call is that Objective-C syntax puts the argument values after colons in the method name rather than all after the name. - Chuck
def init(contentRect, styleMask=NSCommonWindowMask, backing=NSBackingStoreRetained, defer=false, screen=NSScreen.mainScreen())
. And the fact that an IDE can help you work around a language's failings does not fully negate those failings, it just makes them more bearable. Otherwise, it's like saying x86 assembly is an awesome OO language because you can write Objective-C and compile it down to assembly. - Chuck
I just started learning Objective-C, and there are two things about Objective-C that I don't really like
1.
CGRect frame = imageView.frame;
frame.size.width = 100;
imageView.frame = frame;
I want imageView.frame.size.width=100;
in one line
2.
componentsSeparatedByString()
instead of split()
? :(
I want some short and common language function names that are easier to remember
========================
oh and a new one:
3 Concating two strings: "one" + "two"
[NSString stringWithFormat:@"%@%@", @"one", @"two"];
Very developer unfriendly.
"one" + "two"
is converted to new StringBuilder().append("one").append("two").toString()
, so the actual code is even worse. - CajunLuke
split
method on NSString
, feel free to add it yourself using a category. - Dave DeLong
The main thing I don't like about Objective-C is that when you want to send a message to the result of another message it can be a pain if you forget to put your two '[' at the start of the line. This happens to me fairly often where I think I want to send this message and then straight after writing it I think I want to send some other message to the return value. I then have to move back to the start of the call and add the extra '['. I imagine as I use the language more this will probably happen less, but at the moment it's a pain.
Another issue is that the XCODE IDE code sense just doesn't seem as good as what I can use in Visual Studio. It suggests lots of things that aren't relevant and sometimes doesn't even suggest things that are. This can be a bit of a pain when you're learning how to use the provided APIs as you can't just browse through all the methods and properties that are provided by an object.
I tried my hand at a bit of Objective-C programming (for an iPhone app) and didn't much like it, mainly for the following reasons:
I neither love nor hate Objective-C, this is just another language. But I do love Cocoa for its ability to let the developer focus on its application.
Whilst it has a far richer OO model than either Java or C++, it suffers all the shortcomings of traditional C. I think the C side leaves many people on the beach.
More seriously, it comes down to whether you think of OO as function calls or messaging. Objective-C emphasizes the Smalltalk model of messaging.
A much better language to use is Objective-C++ which you enable by using the .mm suffix on your files. It also allows you to use C++ classes but, more relevantly, the C++ language extensions over C.
string
and want to use a cocoa API which wants a NSString*
, or struggle with object ownership and reference counting. The way of doing things in a language is fairly different from the other one, and unless you have to you generally want to avoid mixing both. Not always having choices is the good thing, especially if you want to preserve consistency. - pqnet
Personally it wasn't the brackets that got me on the syntax, it was the use of colons in arguments. It was like remembering to use "$" in PHP. I am just not used to typing that character in programming.
I also recently had a "port some obj-c I had written to another language" moment where I realized how articulate some of the code I had written was and how much excess glue was required in other situations. My latest opinion is that the mistakes made while internalizing the syntax (which are easily caught in the IDE) are well worth the trade off in flexibility and power.
I've been learning Objective-C for a few days now, and trust me, I want to like it but I'm having great difficulty finding the beauty. This is not a case of fearing change and different - when I first saw Python I thought it was beautiful, heck I even quickly found Perl to be a nice language, in spite of being littered with $ and other syntactical irregularities coming from a C background. We just have to face it, a language that needs explicit, verbose methods for such primitive operations as STRING concatenations is lacking. Since I want to get into iphone development, I am stuck learning this, so I pray that in a few weeks I'll have a moment of clarity and be able to return to this thread and renounce this post.
I can't say I hate Objective-C, but I find it's syntax to be just different enough to keep me from diving into it.
I really ought to spend more time with it. Like anything else, everyone tends to prefer the programming language they use most.
1) The use of the minus and plus (- +) operator for something other than math. A keyword would've been just as good and much easier to read. From macrumors.com: "An instance method is marked with a hyphen/minus sign". That just seems like a terrible design choice. Other languages repurpose symbols like % but I can see reasons for that, % is just shorthand for "divide by 100."
2) All the brackets. Everywhere. The dot operator makes things much easier to read.
The more I use Objective-C, the more I appreciate it for being different. Personally, I like the message sending feel of the syntax. Not using the dot-operator gives me the feeling of sending a message to an object rather than calling something. Sounds stupid possibly, but it's a distinct difference for me.
I come from a C#/Java background and never learned C. Objective-C feels like a nice bridge from those higher-level object oriented languages to the lower-level C language.
I think it has to do with the message-based threading and function-calls. The whole idea seems foreign at first. There also aren't a lot of good books on Objective-C outside of what Apple has to offer. O'Reilly has a book on Obj-C & Cocoa, but it's woefully outdated last time I checked.
Platform. Up until the iPhone, how many Mac developers were there? I'm assuming a lot less than Windows developers. Not slamming Mac developers by the way, I was one of those minority developers.
On Windows though .NET and C++ are popular, well documented, and have tons of external literature and how to's and libraries.
Relatively speaking Apple was lacking in external literature until the iPhone really took off. Even now there is still a lack of solid external literature out there compared to what you can get for C++ or .NET.
Education too. Is Objective C taught in college? Java is, C++ is. Is there a college that teaches Objective C? Is there a class somewhere that goes beyond Cocoa and really gets into C and the memory management etc.?
I'm not sure if there is an Objective C port to Widows, but I know I've never been on a project that's used it, whereas I have been on one that used Mono, a .net port, on the Mac.
I wish that there was more around Objective C in external literature, training, and porting. But regardless, it does a good job. It's just hard to pick it as my mainstay when I work across platforms and need to get up to speed quickly.
I'm having a really hard time with the fact that I need to edit both the header and the implementation every time I want to add or change a method signature. I definitely have not found my groove with Obj-C yet.
I've been coding in Objective C about 3 months now doing iPhone development. I've been programming C++ for about 20 years and c, basic, and assembler before that. My feeling is mixed. I don't think I like Objective C. I know I don't like XCode. I do very much like Cocoa Touch.
I admit that I am in the camp that favors strong type safety. I just can't for the life of me understand why this should compile and further why it's a good thing it compiles.
UITapGestureRecognizer* tap = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
I wrote this code cutting and pasting to get a bunch of gestures in and missed one. It didn't even give a warning. Finding the bug was a nightmare.
I do like the named parameters, but they took a bit to get used to.
I don't like manual management of a ref count. It's bound to go wrong. If I don't have it like in C I am careful to manage my memory and have clear handoff functions. Factory functions clearly place cleanup responsibility on the caller. Objective C I find I have to learn which functions retain the object and which autorelease or if I alloc and hand the object off what retains it and what does not. It's memorization of too many things. In C++ if I have a raw pointer I should be responsible, if a shared pointer then I am not, and if a reference then I'm at the whim of the owning object.
There is no comparison when it comes to available libraries. STL, Boost, Loki, for example.
I don't like the inconsistent naming convention. The poor autocomplete just adds to the problem.
XCode is not fit for consumption. It has nice ideas half implemented. It in no way compares to what is out there for IDEs. No matter what anyone thinks of Objective C, there can be no defense of XCode compared to Eclipse or Visual Studio. When I work on game consoles like PS or Wii we develop in visual studio and plug in our own build chain.
Cocoa Touch is outstanding! Full access to a very good library and the ability to mix C++ when I want those features is very good, and I know is a feature of the language. I'm building almost every game using UIKit as the dominant technology. Complaints that it's too slow etc, are just wrong. I like UI builder a lot, but as with everything XCode it's largely unfinished. Often I set a value in UIBuilder and it doesn't come through. There's just no excuse for that.
Apple created the iPhone concept and drew a lot of us to Objective C. Android is allowing C++ and already Cocos2d-x is out and surpassing Cocos2d. Android is predicted to dominate the market. C++ is coming to an iPhone near you. It will be interesting to see if allowed to choose how many people will adopt Objective C over C++.
Regardless of what I ultimately think of Objective C, learning new languages help us become better programmers. Things we like in one language have a habit of creeping over and that is helpful to everyone.
Preference, familiarity, insecurity, and jealousy varies.
Every language has it's pros and cons or it would not have existed long enough.
I have used Objective-C for more than 20 years and I have always been surprised that it hadn't become more mainstream among programmers. It is decidedly mainstream in the Apple sphere now, but this is actually only a recent development. I remember a somewhat soft-headed Apple exec telling me in 1998 that all MacOS X developers would soon be coding in Java. And many current Apple APIs are staunchly C++ friendly.
Objective-C is far from "a clever hack" as described earlier; it is a deeply elegant one which balances the machine pragmatism of C with the flexibility of dynamic typed object orientation. If you have an opinion about Objective-C but no exposure to NeXTstep or Cocoa APIs you are probably missing something about this balance I speak of.
I learned Objective-C as a first language, so going into it I had no opinions or bias towards any other language. I enjoy writing in Objective-C, I personally prefer using the brackets instead of dot notation. Perhaps since I went into it with no prior experience in any other language, it made it easy to become accustomed to it and program myself to think in that way (no pun intended).
I'm now learning Java, although it seems to be a nice language so far, I like Objective-C more. Perhaps as I become more comfortable with Java I might begin to like it just as much, but for now I enjoy Objective-C. There isn't one definitive feature that makes me like Objective-C more, it's just as a whole the language "feels better" (for lack of a better phrase).
This is all coming from a person who programs as a hobby and not a job at the moment.
I'm an iPhone trainee, and all others in my team, with Java background are hating Objective-C
They don't even understand interface and implementation,
because they are relating it with Java's interface
and implements
.
Many others are trying to relate it with C++. Avoid learning it by relating it to some language, learn it as a new language. All will love it as I do.
The biggest thing for me has nothing to do with whether Objective C is a good language or not, it has to do with targeting multiple platforms. I can share very little code right now if I want to develop for Android (Java), Symbian (C++), iphone(Obj-C), or whatever. You can't even share libraries between these platforms. Writing for the iphone kinda locks you into their ecosystem the most, since Objective C is the least wide spread of the aforementioned languages.
It would be nice if these mobile platforms supported some high level environment where the developer could leverage more of the mature, understood technologies being use outside the mobile world.
It will be interesting to find out more about Palms new approach.
It's a strange mix of a statically typed language (c) and a dynamically typed language (Smalltalk). That provides something for every developer to both love and hate.
I don't hate Objective-C. Made a client app for Mac coming from a Java background and things were pretty slick. I believe people may dislike the language for being so MacOS centric.
Objective-C is not an Apple language -- it's been around for a long time and is usable on non-Apple operating systems. Cocoa, though, is all Apple. See also What’s the difference between objective-c and cocoa? [1]
[1] http://stackoverflow.com/questions/435928/whats-the-difference-between-objective-c-and-cocoaI don't have much of a problem with the language.
I at first didn't like the Frameworks I had to work with. They all use camel case [1]. Where as I use underscore separated [2] words for my variable names.
But the more I got used to Objective-C and Cocoa the more I liked the clash. It helped me understand what was part of the framework and what was not.
[1] http://en.wikipedia.org/wiki/CamelCase#Current_usage_in_computingSome of the hatred for Obj-C is time vs. money. When you've used a language like C++ for 15+ years you want to make money with a THAT.
You don't want to stop and go learn a whole new language that in two weeks will only have you doing basic buttons and simple tasks.
Then there's XCode and Interface Builder. These programs will have most developers pulling their hair out! Horrible!
All of this would be true for any language not just Obj-C. Java and ActionScript succeed because they are very close to C/C++ in the minds of developers.
I come from a Java Background and I write Android Apps. All my Customers want an Iphone App as well, so I decided to learn Obj-c and I'm having a lot of fun doing so. I think the language looks very clean and stylish very apple ( I know its not an apple lang:). The main difference for me is Xcode I'm loving it. It looks so nice and works alot better then. I can get my Motodev eclipse working especially the bugs are not existant. I haven't written my first Iphone app yet but I'm really looking forward to it. I am excited about learning obj-c as I never knew that, it is basically build abon C which I always wanted to learn, but never saw the reason to do so other than interest. I like the brackets - makes the code more organized, I think. And I really like the header Idea especially for coders wanting to extend my apps.