Tuesday, April 10, 2012

Objective-C needs Java's Annotations

TL;DR: Java's annotations feature allow a developer to annotate code. These annotations can be used either at run-time or compile-time to do awesome stuff. Objective-C needs this as well so that we can let mogenerator and other cool tools do its work more efficiently.

I am addicted to Objective-C. In my opinion Objective-C is a very nice and pragmatic language with a neat syntax and I have used it a lot during the last eight years. Lately I had to use Java a little bit. It was fun but I still like Objective-C more – although there is one thing about Java that I fell in love with and would like to see in Objective-C as well: Annotations.
First I will try to explain what Java annotations are and then I will describe which problems could be solved by having something like Java's annotations available in Objective-C.

Java Annotations

Java annotations allow a developer to add metadata to the code (e.g. to classes, methods, fields or packages). Every annotation has a name and the actual metadata in the form of key-value pairs. An annotation can be processed either at run-time (by using Java's reflection capabilities) or at compile-time (by providing a compiler plug-in – a so called annotation processor)). When an annotation is being processed a developer can do all sorts of things like executing existing code (at run-time) or by creating new code (at compile-time).

Annotations could be useful in Objective-C as well

Now it's time to talk about how annotations could be used to solve Objective-C related problems.


Eliminate the need to write boilerplate code
Writing code makes fun as long the code is not trivial. When working with Core Data (an object graph management framework used to persist data) a developer usually has to write a lot of boilerplate code. This (boilerplate) code is used to avoid compiler warnings, improve the performance, make your application code more read/writable and make it easier to debug. So usually a developer wants to have the benefits of the boilerplate code without writing it. This is why there are tools like mogenerator which generate the boilerplate code automatically. This is cool but manually generated code is a bad practice when developing an application: You have to manually regenerate the code when the model changes and new team members have to get familiar with the generator. It would be ideal to let the compiler generate the code that is usually generated by mogenerator. This would be possible if we had annotations. An annotation processor is basically a compiler plugin that can generate code, as already mentioned. You still get all the benefits of manually generated code (code competition, higher performance, ...) but without the drawbacks.

Remark: Core Data can also be used without the generated or handwritten boilerplate code. The framework generates the code at runtime but in order to avoid compiler warnings one usually writes/generates the code.

Typos are found by the compiler
Java has a few built in annotations. One annotation I found quite useful is the @Override annotation. You attach it to a method you intent to override. Why is this useful? Imagine you have a typo in the name of the method you intent to override: Your implementation never would get called. This is a problem that is also acknowledge by Apple in the form of a technical Q&A: "Common mistakes with delegation in Cocoa":
Capitalization errors are often the hardest to catch. Any of the methods of the NSTableDataSource informal protocol are likely candidates for subtle miscapitalization. For example, numberOfRowsInTableView: is often mistakenly entered as numberOfRowsInTableview:.
Apple goes even further and suggest to copy & paste code in order to prevent these errors:
Perhaps the safest habit to develop is to simply always cut and paste the method selector.
Wait. Using copy and paste to prevent errors in code? Questionable… That's where a @Override-annoation would come into play. The annotation could check at compile time if one is actually overriding a method or not. My friend Stefan Reuter told me about another benefit of @Override: Imagine a developer is overriding a method of a class provided by a third party. A few months later the third party decides to remove this method. If the developer annotated the method with @Override then the compiler will complain about a method that is overridden but which is no longer present. Sweet isn't it?

Remark: I think that in the case of Objective-C protocols we already have something similar. Starting with Objective-C 2.0 a message defines in a @protocol definition can be marked as required (@required) or as optional (@optional). If a class declares that it conforms to a protocol the developer is warned if the class does not respond to the messages marked as required.

We already have annotations!

Objective-C already had an "annotation-like" feature: properties:
@property (key1=value1, key2=value2, ...) Class *name;
With @property we tell the compiler how @synthesize generates code. So what I want to see in Objective-C is a generalized way to do the same thing @property/@synthesize does.

61 comments:

  1. Totally agree. With something like Override and a word for the opposite it would be much safer to add categories to existing classes. Right now you you can easily screw up with categories which is a shame because it is great feature if you ask me.

    ReplyDelete
  2. Agree ,It is really a pity that there is no java-like annotation support in Objective-C. But there is a possible workaround to achieve this goal. Use formatted comments as annotation, then write a parser to parse those comment annotations and save them as a file from which you can read the annotations at runtime.

    I have implemented a project named ROAnnotation which use this method to provides runtime java-like key-value pairing annotation at class/method/property for Objective-C. What do you think about it?

    ROAnnotation on GitHub
    https://github.com/jibeex/ROAnnotation

    ReplyDelete
  3. Hi this is Kathiresan i am having 3 years of experience as a dot net developer and i am certified. i have knowledge on OOPS concepts in .NET but dont know indepth. After learning android will be enough to get a good career in IT with good package? and i crossed Android Training in Chennai website where someone please help me to identity the syllabus covers everything or not??

    Thanks,
    kathiresan

    ReplyDelete

  4. Hi this is Kathiresan i am having 3 years of experience as a dot net developer and i am certified. i have knowledge on OOPS concepts in .NET but dont know indepth. After learning android will be enough to get a good career in IT with good package? and i crossed Android Training in Chennai website where someone please help me to identity the syllabus covers everything or not??

    Thanks,
    kathiresan

    ReplyDelete
  5. Hi, I am Martina from Chennai. I am technology freak. I did Android mobile application development course at Fita academy, this is very useful for me to make a bright career in IT industry. So If you looking for best Android Training Institute in Chennai please visit fita academy.

    ReplyDelete
  6. It was really a wonderful article and I was really impressed by reading this blog. We are giving all software and Database Course Online Training.
    Oracle Training in Chennai is one of the reputed Training institute in Chennai. They give professional and real time training for all students.
    Oracle Training in chennai

    ReplyDelete
  7. Oracle Training in chennai
    It’s too informative blog and I am getting conglomerations of info’s about Oracle interview questions and answer .Thanks for sharing, I would like to see your updates regularly so keep blogging.

    ReplyDelete
  8. Informatica Training in chennai
    This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..

    ReplyDelete
  9. Pega Training in Chennai
    Brilliant article. The information I have been searching precisely. It helped me a lot, thanks. Keep coming with more such informative article. Would love to follow them.

    ReplyDelete
  10. QTP Training in Chennai,
    Thank you for the informative post. It was thoroughly helpful to me. Keep posting more such articles and enlighten us.

    ReplyDelete
  11. There are lots of information about latest technology and how to get trained in them, like Hadoop Training in Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies Hadoop Training in Chennai By the way you are running a great blog. Thanks for sharing this..

    ReplyDelete
  12. Nice article i was really impressed by seeing this article, it was very interesting and it is very useful for me.I get a lot of great information from this blog. Thank you for your sharing this informative blog.
    SAS Training in Chennai

    ReplyDelete
  13. I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    Greens Technologies In Chennai

    ReplyDelete
  14. I was looking about the Oracle Training in Chennai for something like this ,
    Thank you for posting the great content..I found it quiet interesting, hopefully you will keep posting such blogs…
    Greens Technologies In Chennai

    ReplyDelete
  15. Thanks for sharing amazing information about pega Gain the knowledge and hands-on experience you need to successfully design, build and deploy applications with pega. Pega Training in Chennai

    ReplyDelete
  16. Who wants to learn Informatica with real-time corporate professionals. We are providing practical oriented best Informatica training institute in Chennai. Informatica Training in chennai

    ReplyDelete
  17. QTP is a software Testing Tool which helps in Functional and Regression testing of an application. If you are interested in QTP training, our real time working. QTP Training in Chennai

    ReplyDelete
  18. Looking for real-time training institue.Get details now may if share this link visit Oracle Training in chennai

    ReplyDelete
  19. Hey, nice site you have here!We provide world-class Oracle certification and placement training course as i wondered Keep up the excellent work experience!Please visit Greens Technologies located at Chennai Adyar Oracle Training in chennai

    ReplyDelete
  20. Awesome blog if our training additional way as an SQL and PL/SQL trained as individual, you will be able to understand other applications more quickly and continue to build your skill set which will assist you in getting hi-tech industry jobs as possible in future courese of action..visit this blog Green Technologies In Chennai

    ReplyDelete
  21. Nice site....Please refer this site also Our vision succes!Training are focused on perfect improvement of technical skills for Freshers and working professional. Our Training classes are sure to help the trainee with COMPLETE PRACTICAL TRAINING and Realtime methodologies. Green Technologies In Chennai

    ReplyDelete
  22. Job oriented Hadoop training in Chennai is offered by our institute. Our training is mainly focused on real time and industry oriented. We provide training from beginner’s level to advanced level techniques thought by our experts. Hadoop Training in Chennai

    ReplyDelete
  23. let's Jump Start Your Career & Get Ahead. Choose sas training method that works for you. This course is designed for professionals looking to move to a role as a business analyst, and students looking to pursue business analytics as a career. SAS Training in Chennai

    ReplyDelete
  24. It is really very helpful for us and I have gathered some important information from this blog.
    Oracle Training In Chennai

    ReplyDelete
  25. Oracle Training in Chennai is one of the best oracle training institute in Chennai which offers complete Oracle training in Chennai by well experienced Oracle Consultants having more than 12+ years of IT experience.

    ReplyDelete
  26. There are lots of information about latest technology and how to get trained in them, like Hadoop Training Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies(Hadoop Training in Chennai). By the way you are running a great blog. Thanks for sharing this.

    ReplyDelete
  27. Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.Informatica Training In Chennai

    ReplyDelete
  28. A Best Pega Training course that is exclusively designed with Basics through Advanced Pega Concepts.With our Pega Training in Chennai you’ll learn concepts in expert level with practical manner.We help the trainees with guidance for Pega System Architect Certification and also provide guidance to get placed in Pega jobs in the industry.

    ReplyDelete
  29. Our HP Quick Test Professional course includes basic to advanced level and our QTP course is designed to get the placement in good MNC companies in chennai as quickly as once you complete the QTP certification training course.

    ReplyDelete
  30. Thanks for sharing this nice useful informative post to our knowledge, Actually SAS used in many companies for their day to day business activities it has great scope in future.

    ReplyDelete
  31. Greens Technologies Training In Chennai Excellent information with unique content and it is very useful to know about the information based on blogs.

    ReplyDelete
  32. GREENS TECHNOLOGIES, ONE OF THE BEST IT INSTITUTES FOR ORACLE SQL TRAINING IN CHENNAI OFFERS TRAINING WITH PRACTICAL GUIDANCE. OUR TRAINING ACADEMY IS FULLY EQUIPPED WITH SUPERIOR INFRASTRUCTURE AND LAB FACILITIES. WE ARE PROVIDING THE BEST ORACLE PLSQL TRAINING IN CHENNAI.

    ReplyDelete
  33. if i share this blog weblogic Server Training in Chennai aims to teach professionals and beginners to have perfect solution of their learning needs in server technologies.Weblogic server training In Chennai

    ReplyDelete
  34. if learned in this site.what are the tools using in sql server environment and in warehousing have the solution thank ..Msbi training In Chennai

    ReplyDelete
  35. i wondered keep share this sites .if anyone wants realtime training Greens technolog chennai in Adyar visit this blog..performance tuning training In Chennai

    ReplyDelete
  36. As your information sybase very nice its more informative and gather new ideas implemnted thanks for sharing this blogsybase training In Chennai

    ReplyDelete
  37. i gain the knowledge of Java programs easy to add functionalities play online games, chating with others and industry oriented coaching available from greens technology chennai in Adyar may visit.Core java training In Chennai

    ReplyDelete
  38. I have read your blog and I got very useful and knowledgeable information from your blog. It’s really a very nice article Spring training In Chennai

    ReplyDelete
  39. fantastic presentation .We are charging very competitive in the market which helps to bring more oracle professionals into this market. may update this blog . Oracle training In Chennai which No1:Greens Technologies In Chennai

    ReplyDelete

  40. hai you have to learned to lot of information about c# .net Gain the knowledge and hands-on experience you need to successfully design, build and deploy applications with c#.net.
    C-Net-training-in-chennai

    ReplyDelete

  41. hai If you are interested in asp.net training, our real time working.
    asp.net Training in Chennai.
    Asp-Net-training-in-chennai.html

    ReplyDelete

  42. Amazing blog if our training additional way as an silverlight training trained as individual, you will be able to understand other applications more quickly and continue to build your skill set which will assist you in getting hi-tech industry jobs as possible in future courese of action..visit this blog
    silverlight-training.html
    greenstechnologies.in:

    ReplyDelete

  43. awesome Job oriented sharepoint training in Chennai is offered by our institue is mainly focused on real time and industry oriented. We provide training from beginner’s level to advanced level techniques thought by our experts.
    if you have more details visit this blog.
    SharePoint-training-in-chennai.html

    ReplyDelete


  44. if share valuable information about cloud computing training courses, certification, online resources, and private training for Developers, Administrators, and Data Analysts may visit
    Cloud-Computing-course-content.html

    ReplyDelete

  45. I have read your blog, it was good to read & I am getting some useful info's through your blog keep sharing... Informatica is an ETL tools helps to transform your old business leads into new vision. Learn Informatica training in chennaifrom corporate professionals with very good experience in informatica tool.
    Regards,
    Best Informatica Training In Chennai|Informatica training center in Chennai|Informatica training chennai

    ReplyDelete
  46. In recent days Informatica & SAS plays vital role to reach your career in a faster way, so it’s a required skill for everyone, thanks for sharing this useful information to our vision keep blogging.
    Regards,
    sas course in Chennai|Informatica training in chennai

    ReplyDelete
  47. Pretty Post! It is really interesting to read from the beginning & I would like to share your blog to my circles for getting awesome knowledge, keep your blog as updated.
    Regards,
    sas training in Chennai|sas training chennai|sas institutes in Chennai

    ReplyDelete
  48. Excellent information with unique content and it is very useful to know about the information based on blogs.
    Informatica Training In Chennai
    Hadoop Training In Chennai
    Oracle Training In Chennai
    SAS Training In Chennai

    ReplyDelete
  49. Excellent information with unique content and it is very useful to know about the information based on blogs.
    Informatica Training In Chennai
    Hadoop Training In Chennai
    Oracle Training In Chennai
    SAS Training In Chennai

    ReplyDelete
  50. Hi, It's really Nice Post its very informative and way of describing is Good keep it up.

    Android training in chennai
    ios training in chennai

    ReplyDelete
  51. We at Colan Infotech Private Limited a Mobile application development company in chennai,
    is Situated in US and India, will provide you best service in enterprise mobile app development company and Colan Infotech has a group of exceedingly dedicated,
    inventive and creative experts with an energy for delivering exciting , helpful and stylish Web and Mobile Applications ,
    We work with customers in a wide variety of sectors.
    We design all of our websites and applications using the responsive web design approach.
    Our talented team can handle all the aspects of mobility so we are rated as
    best service provider in Mobile apps development companies in chennai.

    We solidly trust that our customers start things out and there is not a viable alternative for quality of service.
    We offer custom services to a wide range of industries by exceeding our client’s expectations. You can even interact directly with the team regarding your project,
    just as you would with your in-house team. we always desire to solicit our customer's fruitful experience with us,
    we are the top notch Mobile App Development Company in chennai and mobile app development companies in Bangalore.
    We can provide best mobile app development chennai . We can provide cutting edge technology services in Mobile application development in chennai.
    Reach us for mobile app development chennai or just call us for best mobile app developers in chennai .

    ReplyDelete

  52. We at Colan Infotech Private Limited a Mobile application development company in chennai,
    is Situated in US and India, will provide you best service in enterprise mobile app development company and Colan Infotech has a group of exceedingly dedicated,
    inventive and creative experts with an energy for delivering exciting , helpful and stylish Web and Mobile Applications ,
    We work with customers in a wide variety of sectors.
    We design all of our websites and applications using the responsive web design approach.
    Our talented team can handle all the aspects of mobility so we are rated as
    best service provider in Mobile apps development companies in chennai.

    We solidly trust that our customers start things out and there is not a viable alternative for quality of service.
    We offer custom services to a wide range of industries by exceeding our client’s expectations. You can even interact directly with the team regarding your project,
    just as you would with your in-house team. we always desire to solicit our customer's fruitful experience with us,
    we are the top notch Mobile App Development Company in chennai and mobile app development companies in Bangalore.
    We can provide best mobile app development chennai . We can provide cutting edge technology services in Mobile application development in chennai.
    Reach us for mobile app development chennai or just call us for best mobile app developers in chennai .

    ReplyDelete


  53. Yes in the hibernate framework in objective programming like java ,C++ it is very much needed.
    Best sql course training in bangalore

    ReplyDelete
  54. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
    Java Training in Chennai

    ReplyDelete
  55. This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post.
    sell used cellphones

    ReplyDelete
  56. Great post! I am actually getting ready to across this information, It's very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    PEGA Training in Chennai

    ReplyDelete