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 spent the past 2 years teaching myself and taking classes on object oriented programming. I have done quite a bit of Java programming, and lately I've been spending a lot of time doing objective-c. I really enjoy programming, but one thing that I have noticed (with some frustration) is that I'm not amazing at finding elegant solutions to problems.

I'm beginning to notice that it isn't enough to just solve a problem, but that the way you approach and implement a solution says a lot about you as a developer and depth of your knowledge and understanding.

Being that I am looking to make a career out of iOS development (I'm still in college), I want to be sure that I am doing everything I can to learn elegance, as opposed to just getting it done.

Case in point: For one of my first programming projects for school (this was a long time ago!), we needed to populate arrays with tons of information. I went through and added each element line by line just hard coding the index! When the TA came by he laughed at me and was like: "Why didn't you just use a for-loop?" and I thought to myself, well who cares? It works doesn't it?

My Question to iOS devs out there: What are some things that I can study/try to implement to increase the elegancy of my design habits? What are examples of things that are often done in the app development world by rookies that just shouldn't be done? Instead of just being able to throw a quick and dirty hack job of a solution together, I want to be able to consistently come up with awesome ways to solve problems! And I've realized that those types of solutions don't always come quickly to me.

Is this skill something that can only be acquired through experience and doing it wrong tons of times before you realize that there is a better way? Are there any areas that you can recommend I try to cover and make a point of understanding?

While I am happy that I can keep up with many of the q's posted to sites like this, I get frustrated when I see some that are just in a whole different universe! I want to close the gap, and so I am throwing this out there to any who may be interested/able to help.

Thanks!!

share|improve this question
1  
It is nice to know that you like iPhone programming but this is no place to discuss it. This is a place for programmers to solve technical issues. Please Read What kind of questions should I not ask here? in Stackoverflows FAQs. stackoverflow.com/faq I think you just found a wrong place to discuss this. –  PARTH Mar 26 '11 at 4:30
add comment

migrated from stackoverflow.com Mar 26 '11 at 10:39

This question came from our site for professional and enthusiast programmers.

11 Answers

Mike Rundle runs a great website called Flyocity, and he's recently started something called Design Then Code, which is a set of tutorials that describe both the design side and then the implementation of an iPhone app. I get the idea that he's going to release a bunch of them, but the first one is out and it's pretty nice. It's $25 for the design side, $25 for the code side, or $40 for the whole shebang, so you'll be out of pocket a bit, but the quality of the writing and the ideas presented are worth the cost of admission.

Having said that, a lot comes with experience. The more you write code, the better you'll get at it. Read lots of code, as well. The Apple Dev site has tons of sample code for all kinds of things, so pick something you're interested in and try to figure it out.

share|improve this answer
    
So obviously I'm the only person that thinks that the DTC guide is a bit overpriced. Btw, within the next 2 days and 20 hours you can get the $40 DTC pack and much more useful stuff in the valio bundle for $49. To be honest, I would have been quite disappointed if I would have paid $40 for the DTC Guide alone. –  Matthias Bauch Mar 26 '11 at 9:29
add comment

FWIW IMO the answer is:

Keep completely away from Java, Perl, Basic and the like.

If possible, start coding in machine code, assembler, or at least just plain C. (Or may be punch cards if you have access to that!)

Once you master that: think in algorithms

Code and implementation is crap. It's nothing. Idiotic trends like "object!" or whatever else come and go every few decades, and don't mean much.

Try to program like an alien would, purely in terms of algorithms. Focus on the very epistemology of computing, and lesser concerns will follow. Your focus is your reality!

share|improve this answer
add comment

The key to elegant solutions is to think outside of your current problem. For example, in your class assignment with the array it worked, but if the requirements change just a little bit you would have to rewrite the code.

A starting point to elegant code is to look for a way to accomplish it in the least number of lines possible. Make the computer do the work for you.

At the end of the day it just takes experience. Keep working at it and it will start to come naturally.

share|improve this answer
add comment

Stephen, this is a fantastic question! I've been, like you, trying to improve my chops as an iOS developer for the past few years, and I think that I've improved a lot (I still have a ways to go!).

When searching for elegance, you come up against the old problem of whether something that makes your code shorter is simply too scary to be elegant. Case in point: I wrote a category on NSObject to release all retained properties of an object automatically (to simplify -dealloc implementations). This was certainly a very elegant thing, but it was also super scary: it caused some strange bugs (which I suspect were affected by subtleties in the Objective-C runtime implementation, as well as the compiler), and eventually had to be pulled out. I suppose my point is that there's got to be a balance between elegance and learning to not make things too clever.

So, my rule of thumb for elegance:

  1. Usually, elegance means less code.
  2. Usually, elegance means you can understand it quickly.
  3. Sometimes, elegance means more code (but less scary magic).

The most important thing is that you get really confident with Objective-C so that you can learn to ignore the people who say that X is not possible (or useful), or so that you can listen to them. If you're not confident with your tools, you'll be cowed by people who are less inventive than you are.

Find a role-model who writes code like you imagine Jesus would write code! Try to copy them. Eventually, you'll get sick of their style and maybe even go in the opposite direction. The point is that you experience different, valid styles of doing things. In the end, even if you come out somewhat agnostic, you'll possess the tools to make elegant solutions, no matter the constraints put upon you by your environment (depending on the team you're working with, you may change the type of solution you propose).

I think that my answer has been a bit rambly, but I hope that at least some of it was helpful. Best of luck!

share|improve this answer
add comment

I'd suggest you start learning more about Design Patterns instead of focusing on specific implementation solutions.

With a bit of a macro background on the different patterns and where they can/should be applied, you will be able to look at your coding with different eyes, make more informed decisions and therefore write code that is elegant, flexible, extensible and reusable.

Here is an awesome book on the subject that's just been released: http://apress.com/book/view/9781430233305.

For a non iOs specific read on design patterns, have a look here http://en.wikipedia.org/wiki/Design_Patterns

share|improve this answer
add comment

You can't learn 'elegant ways'. It just comes with experience and ofcourse knowledges. The more you know, the better things are. Do not focus only on one programming language, technology, domain. Try to learn different languages and different technologies (cause programming patterns in Java and for example C++ or Objective C are quite different and they all have pros and cons). Analyze everything, try to understand why things are working exactly that way.

It is also a very good thing is to read code. Download different sources, try to understand their architecture, try to think how you can improve that code. As an iOS dev you can start here.

And every time, when you're looking at code, that you've written a year ago and think "WTF is this?" =) it means that you're progressing.

And ofcourse PRACTICE everyday =)

share|improve this answer
    
You forgot the link. –  BJ Homer Mar 26 '11 at 5:14
    
@BJ Homer Thanks. It was some bug on server. –  Max Mar 26 '11 at 5:24
add comment

I would suggest to look at other developers code including code for Mac OS X. For example projects like Cocoa2d, AsyncSocket, InAppSettingsKit, HJCache, Ingredients etc or even the Apple sample code.

That said eventually you need to figure out for yourself how to write code. Everyone has a different style and there are many ways to accomplish the same thing. Personally I am not a big fan of design patterns because they are just recipes but the art of coding is how to use a recipe to write your code. Great bread is not based on a recipe but on the experience as well as the intuition of the backer. But then again it doesn't hurt either.

share|improve this answer
    
Just don't look at Apple's sample code. It's inconsistent and often doesn't follow their standards and guidelines. They don't even seem to maintain it for goodness sakes. –  Paul Feb 24 '12 at 14:06
add comment

As DA said, make some apps. The best way to learn is doing an actual project.

Know someone who wants a simple app, then see if they will let you try and build it for them for free. Although classes and tutorials can be good, it is more important to be able to know how to solve a real world problem with the code.

I think the basic requirements of most apps these days are a selection of the following:

  • Network activity
  • Mapkit
  • Gamekit
  • Connect with Facebook
  • Post to Tiwtter/Facebook
  • Control the Camera
  • Access the GPS/Compass
  • Use CoreData & SQLlite
  • In App Purchases

Of course other members will say there are others, but for most of the apps we have built these are the core requirements that most clients seem to want.

Hope this helps.

share|improve this answer
add comment

While I am happy that I can keep up with many of the q's posted to sites like this

Are you sure? You haven't answered a single question. You don't get feedback that way.

If you have a solution for a question you should post it. If you don't know a solution and the problem is interesting for you try to find one.

If it's wrong somebody will downvote your answer. With a bit of luck there will be a comment that tells you what's wrong.

If it's ugly somebody might post a comment and tell you what you can do to make it better.

Come back after some days and check the other answers. Try to figure out what's better or worse in the other answers.

Answering questions improves your skill much faster than only reading. I can't count the new things I learned because I've answered a question.

You have to show your code to become a better programmer. Show it in stackoverflow answers or (kind of) show it in a product that is actually used.
If you are into open source create a project.

[rant] Judging on the code quality of many iOS open source projects you can do that right now, no need to write well designed, elegant, usable or even code without obvious bugs (that show you a nice yellow warning bubble) [/rant]


But you should definitely have an app in the app store. Every iOS developer that can write code that gets things done without crashes should have one. Nobody can see your ugly code anyway.
After I published my first app I learned more about good code design in the first month than I learned in the year I spent with reading tutorials, blogs, books and documentation.

But I learned it the hard way. At the beginning each improvement I added needed a huge refactoring (aka delete 90%) of the code. Because I used so many "it works, so what?" solutions.

It's one thing to read about Model-View-Controller. It's another thing to realize that your current implementation is crap because it violates MVC everywhere and you can't do this and that because of your bad design.

And only after you've changed 30 methods that are invoked by NSNotifications and post other NSNotifications you will realize that the "NSNotification is awesome!!!!11" thought you had a year ago was kind of wrong.


Learning by doing, and if doing means writing bad code then do it.
I doubt that anybody (besides Jon Skeet) can write elegant code without writing hundreds of thousands of lines of ugly code first.

share|improve this answer
add comment

Great apps are designed, not "just" coded.

But it sounds like your programming education also needs much added reading in basic algorithms and design patterns.

share|improve this answer
add comment

The fact that you know you are creating unelegant code means you are already progressing just fine. It's the developers that think their code is perfect that are the ones you need to watch out for. ;)

I don't think there is anything particular to iOS dev compared to any other dev in terms of what makes for elegant code. You'll learn elegant solutions with practice. For iOS dev, start making some apps. Have a hobby? Make an app for it.

share|improve this answer
    
If he knows he's doing ugly code, but does nothing, then he is not doing fine. But! He is doing fine because he is making an attempt to learn better ways. He's looking for mentoring. –  Paul Feb 24 '12 at 14:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.