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:
- Select all code in Xcode
- Press my assigned hot key: Control + Command + Shift + I
- 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
- 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
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)
- You can also assign a shortcut for it in the Xcode > Services > Text menu.
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?
8. The uploading process will show up in the Console application. A dialog will also pop up after upload is finished.
How to setup
* 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.
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.
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.
Consolas It will appear after you installed Microsoft Office. This font looks very similar to Dejavu font.
Inconsolata Many people recommend this font. It is a great open source font with a cool name.
Color and Thems
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.
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
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
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!