Related Posts Plugin for WordPress, Blogger...
This form does not yet contain any fields.

    Follow Me on Pinterest

    Our products are on iTunes!

     Nanaimo Studio

    Find us on Google+ 

     

    We are listed on: Dmegs Link Directory

    Blog Index
    404 page
    « Temple WTF | Main | The Productive Apple Developer »
    Wednesday
    Jun272012

    The Productive Apple Developer (Part II)


    Code Conventions, Uncrustify and others

    In the second part of this blog, I will continue to introduce some of my favorite tools for my daily development. The first thing I would like to talk about is coding convention. Check out the first part of the blog if you have missed it.

    Good coding convention not only makes code much easier to read, it will improve the overall stability (See Broken Window Theory). It will be difficult for me to cover all code styles in one post, so I will use Objective-C as an example.

    There are two coding conventions in Objective-C that I like, one is Introduction to Coding Guidelines for Cocoa written by Apple. Another one comes from Google: Google Objective-C Style Guide. Google's coding convention combined some conventions from Google's Open Source C++ Style Guide. It covers more topics such like line length or comments style. Both of them are very good to read up on.

    A wise man once says (OK I said it) "the biggest challenge for programming is to come up a good name for a variable or method". Often times I see developers fall into the temptation of naming things with numbers when they fail to come up with something sensible. Don't do that - you will go to hell in your afterlife.

    Following good coding convention is important, but it comes at a cost - it slows us down having to indent things or lining things up and developers don't like to be slowed down. We are already trying to make our typing catch up with our thought stream. Luckily, the beauty of being in the software industry is that for every problem out there, someone is likely to have come up with a solution for it. Uncrustify is one such solution. It is a tool to reformat all your code. It supports C, C++, C#, ObjectiveC, D, Java, Pawn and VALA. Here is how I use it:

    1. Select all code in Xcode
    2. Press my assigned hot key: Control + Command + Shift + I
    3. DONE. All code are nicely formated. Amazing, no?

    How to set up uncrustify?

    First, run following command to install uncrustify:

    sudo brew install uncrustify 

    If you don't have brew command install, please refer to previous post.

    • Next is to open Automator and create a new Service, the service it created is located at
    ~/Library/Services

    Create Service in Automator

    • Add a Run Shell Script item, and using following command. Because the configure file (uncrustify_obj_c.cfg) is stored at ~/.uncrustify/, you probably need to create a folder for it and download the cfg file. If you don't have that folder, please create one. You can use your own configuration file and your own path, you just have to let uncrustify knows it. There are lots of work in configuring due to too many options there. I have spent hours on it and I would hate for you to go through the same fate. Download the file and use it as you wish.
      Download Uncrustify Configuration File
    #!/bin/bash
    /usr/local/bin/uncrustify -l OC -q -c ~/.uncrustify/uncrustify_obj_c.cfg

    Reformat

    Jonah Williams provide a method to reformat all opened files. I don't like it - I have this innate fear that I am putting all my code in jeopardy. Go for it if you suffer no such psychological barrier.

    • Save and use it. Select all your code, and go to Xcode -> Services -> Uncrustify(Your script name)
      Create Services
    • You can also assign a shortcut for it in the Xcode > Services > Text menu.
      Add shortcuts

    Here comes the stunt, select a chunk of code and press Control + Command + Shift + i. Good job Houdini!

    Automating TestFlight Upload

    Testflight makes distributing our test builds easy. You can get more details on TestFlight here. But uploading to Testflight gets tiresome really quick (sorry TestFlight, yes we are that lazy). It's a repetition of clicking, waiting, click waiting, wait clicking, you get the idea. Luckily, this too can be fixed. Thanks to NWTestFlightUploader, you can use it to upload to TestFlight in Xcode with just a few clicks. If you find even doing this is too much, it's time to look for another job.

    For those of you who are still sticking around, here is what you do (I will show you how to install the tool after this):

    1. After you install NWTestFlightUploader, you will see an upload dialog after you do an archive on your app.

    2. Select signing identify

    3. Select provisioning profile, if you want to overwrite.

    4. Add notes.

    5. Choose Distribution lists.

    6. Send Email Notification?

    7. Confirmation.

    8. The uploading process will show up in the Console application. A dialog will also pop up after upload is finished.

    How to setup

    Setup Upload to Testflight in Xcode * Edit the Scheme of your product in Xcode4
    * Expand the "Archive" scheme
    * Click "Post-actions"
    * Add a "New Run Script Action"
    * Select the target that corresponds to the Scheme in the "Provide build settings from..." dropdown
    * Copy & paste the contents of NWTestFlightUploader.sh into the text field
    * At minimum, supply API_TOKEN and TEAM_TOKEN.
    * Done! To try it out, build the Archive scheme. At the end of the process the TestFlight upload 'wizard' will start.

    Color, Themes, and Fonts

    After two very nerdy topics, let me introduce some pretty stuff to please you right brainer out there. In our daily development, a large part of the time is dealing with editors such as Xcode, Sublime text 2, Textmates, etc. If we stare at a window with white background and black text, it's very easy for your eyes to get tired. So let me be your eye doc for a few minutes and show you my choice of colors and fonts and you can pick yours.

    Fonts

    For fonts, I will present you a list of my favorites. I've tried each of them at least one month so I know they work. I've tried many many other fonts, but most of them only last few days. The screenshot is using font size 16, because I want you to see the fonts clearer. If you use it, I would recommend to set your font size 13 to 16. For Anonymous (my most favorite font), I think setting the font size to 13 and 14 are probably the best.

    Anonymous Pro It is my new favorite, very clean and elegant font.
    Anonymous DejaVu Sans Mono The first font I used and I've used it for almost a year before giving up. I'm tired to look at the font over and over again lol.
    DejaVu Consolas It will appear after you installed Microsoft Office. This font looks very similar to Dejavu font.
    Consolas Inconsolata Many people recommend this font. It is a great open source font with a cool name.
    Inconsolata

    Color and Thems

    EGO

    In the past, EGO is my favorite. But after using for about one year, I finally realized I don't like the dark background and the stark contrast.
    EGO

    To install the themes, run the following shell command in Terminal:

    mkdir -p ~/Library/Developer/Xcode/UserData/FontAndColorThemes; cd ~/Library/Developer/Xcode/UserData/FontAndColorThemes; curl -O http://developers.enormego.com/assets/egotheme/EGOv2.dvtcolortheme

    Solarized

    Solarized The color picked and the theme in Solarized is elegant and pleasant. I found my eyes in a blissful state while looking at code in Solarized. Solarized uses a sixteen-color palette. It is a very interesting project and has support for Vim, Emacs, IntelliJ IDEA, NetBeans, TextMate, BBEdit, Visual Studio, and Xcode. But the support for Xcode is not that good. The color from it just don't feel right and bothers me for a long time. You can download the official at solarized official github. Or my version of Solarized Theme or use following shell command to install it.

    mkdir -p ~/Library/Developer/Xcode/UserData/FontAndColorThemes; cd ~/Library/Developer/Xcode/UserData/FontAndColorThemes; curl -O http://bit.ly/Solarize

    Solarized

    Summary

    Consider a normal Objective-C file with 500 lines of code. Uncrustify can save 10 mins of manual code formatting. Automating your Testflight submission saves you another 10 mins from having to upload and distribute manually (If you use web version, you have to wait until file has been uploaded so you can distribute). With good code syntax color and a beautiful font, I don't know how much time it can save for daily coding and debugging process and potential visit to your doctor for migraine. So there you have it! I hope the above tips not only make you a productive but also healthier and happier coder. In the next post, I will introduce even more tips to optimize your workflow to the next level. Please subscribe our blog to get more valuable tips!

    PrintView Printer Friendly Version

    EmailEmail Article to Friend

    Reader Comments (5)

    Great post...lots of useful information. Thanks for posting all of this!
    Just one note...regarding uncrustify. The automator action code snippet doesn't match the code in the image grab. It took me a while to figure out that automator was erroring out because the path to uncrustify wasn't specified.

    The correct code snippet would be:

    #!/bin/bash

    /usr/local/bin/uncrustify -l OC -q -c ~/.uncrustify/uncrustify_obj_c.cfg

    June 27, 2012 | Unregistered Commentersean @adaptIO

    Sean, thanks a lot for your comment. I've made the correction.

    June 27, 2012 | Registered Commenterzitao

    Uncrustify sucks for Objective-C++ ...

    July 3, 2012 | Unregistered Commenterleonardo

    I tried your script but it is failing as I cannot see any effect of it (I tried e.g. if(YES == NO) {} line which I think would be reformatted). Any suggestions why? The only solution working is the one which uses AppleScript and reformat whole project.

    July 4, 2012 | Unregistered CommenterAncient

    Ancient, you can use
    uncrustify -c ~/.uncrustify/uncrustify_obj_c.cfg -l OC -f foo.m -o foo_format.m
    to test if your uncrusitfy is working. If it is. Probably there is soem setting is wrong in your Service. Did you enable "Output replaces selected text"?

    November 3, 2012 | Registered Commenterzitao

    PostPost a New Comment

    Enter your information below to add a new comment.

    My response is on my own website »
    Author Email (optional):
    Author URL (optional):
    Post:
     
    Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>