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.

32 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. 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
  4. 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
  5. 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
  6. 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

  7. 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

  8. 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
  9. 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
  10. 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
  11. 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

  12. 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


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

    ReplyDelete
  14. 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
  15. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
    Data Science Training in Chennai
    Robotic Process Automation Training in Chennai
    Cloud Computing Training in Chennai
    Data Warehousing Training in Chennai
    Dev Ops Training in Chennai

    ReplyDelete
  16. It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.
    Data Science Training in ChennaiKK Nagar | Data Science Course in Chennai
    Python Course in Chennai KK Nagar | Python Training Course Institutes in Chennai
    RPA Training in Chennai KK Nagar | RPA Training in Chennai

    ReplyDelete
  17. DevOps is currently a popular model currently organizations all over the world moving towards to it. Your post gave a clear idea about knowing the DevOps model and its importance.DevOps Training in Chennai

    DevOps Course in Chennai

    ReplyDelete
  18. Excellent blog!!! I got to know more useful information by reading your blog. Thanks for posting this blog.

    Online training for big data
    Big Data Hadoop Online Training

    ReplyDelete
  19. Hızlı takipçi almak için takipçi satın al
    Organik takipçi almak için takipçi satın al
    Bilgisayardan takipçi almak için takipçi satın al
    Mobil cihazdan takipçi almak için takipçi satın al
    Gerçek ve orijinal takipçi almak için takipçi satın al
    Yazarkasa ile takipçi almak için takipçi satın al
    Bitcoin takipçi almak için takipçi satın al
    Pos ile takipçi almak için takipçi satın al
    EFT ile takipçi almak için takipçi satın al
    Havale ile takipçi almak için takipçi satın al
    Mobil ödeme ile takipçi almak için takipçi satın al
    Tamamı orijinal takipçi almak için takipçi satın al
    Organik ile takipçi almak için takipçi satın al
    Türkiye takipçi almak için takipçi satın al
    Global takipçi almak için takipçi satın al
    En hızlı instagram takipçi satın al
    En uygun instagram takipçi satın al
    En telafili instagram takipçi satın al
    En gerçek spotify takipçi satın al
    En ucuz instagram takipçi satın al
    En otomatik instagram takipçi satın al
    En sistematik tiktok takipçi satın al
    En otantik instagram takipçi satın al
    En opsiyonel instagram takipçi satın al
    En güçlü instagram takipçi satın al
    En kuvvetli instagram takipçi satın al
    En seri instagram takipçi satın al
    En akıcı instagram takipçi satın al
    En akıcı instagram takipçi satın al
    En akıcı instagram takip etmeyenler

    ReplyDelete
  20. Very Informative blog thank you for sharing. Keep sharing.

    Best software training institute in Chennai. Make your career development the best by learning software courses.

    rpa training in chennai
    power bi training in chennai
    uipath training in chennai

    ReplyDelete
  21. Windows 7 product key 2023 is an alphanumeric code used to install or reinstall Microsoft latest OS to find it on a label inside your copy of Windows under .Win 7 Ultimate Key

    ReplyDelete