Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've been programming professionally in C, and only C, for around 10 years in a variety of roles.

As would be normal to expect, I understand the idioms of the language fairly well and beyond that also some of the design nuances - which APIs to make public, who calls what, who does what, what is supposed to reentrant and so on. I grew up reading 'Writing Solid Code', it's early C edition, not the one based on C++.

However, I've never ever programmed in an OO language. Now, I want to migrate to writing applications for iPhone (maybe android), so want to learn to use Objective-C and use it with a degree of competence fitting a professional programmer.

How do I wrap my head around the OO stuff? What would be your smallest reading list suggestion to me.

Is there a book that carries some sort of relatively real world example OO design Objective-C?

Besides, the reading what source code would you recommend me to go through.

How to learn OO paradigm using Objective-C?

share|improve this question

closed as not constructive by gnat, Walter, maple_shaft Sep 10 '12 at 11:03

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

4  
"smallest reading list" may not be a good optimization. You are learning a new instrument, so you will need a lot of practice. –  user1249 Sep 9 '12 at 19:53
2  
The situation you are in at the moment is very hard - you want to grok a new paradigm ( OO ) and a new language. This alone makes makes the learning process much harder - 4 times than only one of the two. Also use your critical thinking on the OO stuff you learn. It has morphed beyond tool and into religion for some architecture astronauts. –  Daniel Iankov Sep 9 '12 at 20:17
2  
You've been writing in C, "for around 10 years in a variety of roles", and you've never applied OO design to any of your C code? –  Ambroz Bizjak Sep 9 '12 at 21:07
    
You may find this easier than you expect. Obj-C will reduce the language gap since it is quite similar to C -- the differences will naturally emphasise the key concepts you'll need to master. My suggestion is more around choosing some fruitful toy problems / exercises. I expect you'll already have a strong intuition for OO design because anyone coding solutions for as long as you will intrinsically appreciate the cleanliness and elegance of wrapping functions and data structures within their own package (class). [PS I went from C to OO first via Obj-C, then Ruby, now Python -- Good luck!] –  Assad Ebrahim Sep 10 '12 at 1:59
    
@ambroz, not that I know of –  Holysmoke Sep 10 '12 at 2:45

8 Answers 8

up vote 4 down vote accepted

I also learnt object-oriented programming coming from procedural programming (Pascal and C) so I can understand the difficulty of the switch: you have to start thinking differently, and you already have a lot of experience in another paradigm (BTW, I still enjoy programming in the procedural way from time to time).

Besides reading language-specific books, I think it would be useful to read a general introduction to object-orientation and object-oriented design.

For example, I found Object-Oriented Modelling and Design very useful. The book is not very recent, but the basic concepts of object orientation have not changed that much. Also, I like this book because it concentrates on design concepts and only in the later chapters it explains how to map these concepts to programming language constructs. Furthermore, it shows that an object-oriented design can (also) be implemented in non-object oriented languages (e.g. C) if you need to.

So, in addition to books specific to Objective-C, I would recommend such a language-agnostic reading to get general ideas that are not biased by a particular object-oriented language.

share|improve this answer

Step 1: classes are structs with functions. Mixing data with functions is useful.

Step 2: In a class you limit the visibility of functions and variables. Usually you will have a few public functions that are the "interface" of the class. The advantage of hiding things is you can change them without the user's of your class needing to change. As long as you preserve the public interface you are good to go. It also makes it easier for people to use your class as they won't get confused trying to understand the private details and can focus on the public interface.

Step 3: You can inherit fields and functions from a parent class. This can help prevent some duplication between different sub-types. You can also override the functionality of inherited functions if you choose. Inheritance is one of the least useful features of OOP but there it is.

Step 4: Polymorphism lets you create "pluggable" software. To add functionality you make a new class that satisfies an interface with it's functions. You create the class and it sort of plugs in. You do not make any changes to the work-flow code. Without OOP you would have lots of "if-elseif-elseif" statements in your work-flow to perform different actions for different things. With OOP those if statements disappear because you call the method of your object. Your object's function does what it needs to do polymorphically.

For example you have a class "Engine". It has children "DirectXEngine", "OpenGLEngine". Your game works fine for years. Then 5 years later someone events a new engine and you want to support it in your game. You create a class "SomeNewEngine" and it plugs into your video game. You fully support the new engine without any changes to the workflow code. All you had to do was make a new class. All you have to test (in theory) is that your new class's functions satisfy the interface. (but in reality it is not always feasible to conform things to the same interface...)

Step 5: Understanding polymorphism is not limited to OOP. You can get polymorphic behavior from generic programming. Consider an "add" function

T add(T val1, T val2) {
    return val1 + val2;
}

It works whether you pass in floats, ints, doubles, strings, etc. Even though the implementation of adding these types is very different. This is polymorphism on the source code level. If you think of the "+" operator as a behavior that all the types support, then they all conform the the "+" interface.

You can conceptualize all behavior as functions. Whether it's an actual function or a syntax of the language like +, -, %, etc.

Take it a step further and you remove all syntax from the language. You are now at the point every function you create "is" the language. You create your own language as you program in your language. You support all programming paradigms but are no paradigm. OOP becomes an irrelevant definition of a few techniques that are part of the natural world.

share|improve this answer

There's nothing inherently special about one having C(or any other procedural programming language) background when learning a new programming paradigm. Except for the fact that it's very easy to stick to the old ways for whatever reasons; be that misunderstanding of the new concepts, finding them tiresome to stick to and so on.

The only real, valuable way is to practice. Of course, first you need to build some understanding about what to practice. As for a book, I can't recommend one apart from advising you to take a look what Amazon has to offer(read the table of contents and some reviews at least). Any decent Objective-C book should explain the basic concepts regarding object oriented programming to get you well started.

However, a single book is often not enough. There are books dedicated for object oriented programming and design patterns, such as Design Patterns: Elements of Reusable Object-Oriented Software (The Gang Of Four book). There are many others too, so choose your favourite among catalogue you are most familiar with.

share|improve this answer

I'd suggest you start with exploring the new options that the language provides. At first they seemed a bit useless for me, but with the time everything goes to its place. For this purpose I used cplusplus.com and cprogramming.com Having covered (or refreshed) the basics you need to introduce yourself with some C++ philosophies, which are very different from C. For example:

Beyond this point I think it's best to find a C++ project to work on. With the real work you'll solve real problems and gain experience, which as you already know is invaluable. Good luck :)

share|improve this answer
1  
+1, this is definitely some stuff I forgot to include in my own answer, providing the OP does choose to go with C++. –  about blank Sep 10 '12 at 7:23

Object oriented programming is different but not that different. The main thing is that you can stick together state and functions. This can either be done on a class of object level (one per type of object) or on the object level. I'd take a look at some implementations of object oriented programming for C. Like this for example:

http://c.learncodethehardway.org/book/learn-c-the-hard-waych20.html#x25-9700020

The difference with an natively object oriented programming language would be calling Object_method(struct Object *object, args..) and object.method(args..), basically just that you can add methods to a struct that you can call on the object itself.

Python is a simple language to learn object oriented programming in, python is also quite explicit on the "self" variable that points to the object itself. http://net.tutsplus.com/tutorials/python-tutorials/python-from-scratch-object-oriented-programming/

OTOH, I find that my small experience with SmallTalk was the thing that really made it easy to get started with Objective-C. Looking at some examples of SmallTalk is a great way to understand why things are as they are in Objective-C. Python is easier for grasping the fundamentals of OO though as SmallTalk has more magic feel to it. http://en.wikipedia.org/wiki/Smalltalk

EDIT: (once you have wrapped your head around what OO programming is about)

I'd take a look at the Stanford class on iOS development available for free on itunes. Really good stuff and a solid academic style introduction to iOS programming. The first episode goes through the basics of Objective-C. http://itunes.apple.com/us/itunes-u/ipad-iphone-application-development/id473757255

Then head over to: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html

share|improve this answer

Copy Apple as much as you can. Apple's Objective-C APIs for iOS are clearly written, very consistent, and make good use of a number of OO design patterns. Apple also has some great sample code that usually demonstrates the idiomatic iOS way of doing things. Just be cautious and check the dates on any sample code you download. Some of the older sample projects use outdated paradigms.

Doing this isn't going to give you a grand overview of writing OO code, but if your goal is to port your projects to iOS, this will give you a great start in writing good, clean iOS code.

share|improve this answer

Objective-C is a cleanly written language with a good API. It also has an innovative design and implementation methodology, but it suffers from many C limitations that C++ vastly improved on or added completely out of thin air.

Notable examples of this are:

  • Generics/Template meta programming
  • Safer Casts
  • Both stack and heap allocation - NOTE: this isn't something that C lacks technically, but Objective-c does - where as C++ has this feature. In a nutshell, it only allows heap allocation for objects, where as primitive types are usually stack allocated. You can do stack allocation with C-style structs, but that's your limitation (and you forgo the main benefits of OOP, for the most part - so, you may as well just write in C if you take that route - however, you can embed your Objective-C code into C style code)
  • Differentiation between public, private, and protected method inheritance/virtualization.
  • The ability to pass purely by and receive by references.
  • Smart Pointers to assist with memory management.
  • Built in constructors and destructors which are enforced by more than just standards.
  • Many more APIs and libraries than what exist in the Objective-C/iOS world.

If your focus is more on learning iOS as opposed to just OOP, then Objective-C is the best choice you could really make. Granted, keep in mind that this will have you focusing on more GUI-centric things (such as event-driven programming), which may or may not be what you want to begin with. There's also the down side of the fact that iOS and Objective-C, while going hand in hand, really are only good for each other and not meant for anything else outside of that (in terms of an application building perspective).

If you want to learn about OOP from a more general perspective, and apply what you've learned to something more generic and well-rounded (in the sense that you can take it pretty much anywhere you go), I'd recommend learning either C++ or Java. Java is entirely different, semantically, than C, however it does have many features within it that C++ has.

You're not going to have as much control over your program, but if you need to you can use the JNI (Java Native Interface) to write Java code that can be sent to both C and C++ code.

Either way, regardless of what language/platform/environment you choose, someone with your skills shouldn't have a tough time at least getting the basics down and running with that. It may take a bit for everything to really sink in though, due to the fact that you've been writing procedurally for so long.

My two cents.

share|improve this answer

Oddly I would suggest "Object Oriented Perl: A Comprehensive Guide to Concepts and Programming Techniques" by Damion Conway.

I would then use Ruby to write some OO programs.

The SICP also has a section of OO programming.

Learning object oriented programming is is different than learning C++. You can write very nice functional programming in C++ and you could write an OO program in C. So you have 2 things you need to learn C++ and OO programming.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.