{"id":109,"date":"2010-12-17T13:15:32","date_gmt":"2010-12-17T21:15:32","guid":{"rendered":"http:\/\/bynomial.com\/blog\/?p=109"},"modified":"2010-12-17T13:15:32","modified_gmt":"2010-12-17T21:15:32","slug":"code-formatting","status":"publish","type":"post","link":"http:\/\/bynomial.com\/blog\/?p=109","title":{"rendered":"Code formatting"},"content":{"rendered":"<p>In this post, I&#8217;ll give a quick overview on choosing a personal coding style, and present a way to auto-format code in bulk.<\/p>\n<p>If you take your code seriously, you probably have a code style that you consistently use.  For example, maybe you always use braces with every if clause, or you use underscores in instance variable names but not local variables.<\/p>\n<h2>Style Guides<\/h2>\n<p>If you work with other coders, it&#8217;s good practice to have an agreed-on general style.  Google engineers have been doing this carefully for a while now, and they&#8217;re even nice enough to publish their own style recommendations for many languages, including Objective-C.  I don&#8217;t agree with every guideline Google suggests (for example, the 80 char line limit), but it&#8217;s very good to consider their perspective. \u00c2\u00a0One thing I wish they did (and that I&#8217;ve seen in other style guides) is to give an explanation for each style decision; otherwise the rules may feel arbitrary and unintuitive. \u00c2\u00a0If you&#8217;re developing a personal or company style guide, make sure to refer to Apple&#8217;s suggestions as well!<\/p>\n<p><a href=\"http:\/\/google-styleguide.googlecode.com\/svn\/trunk\/objcguide.xml\">Google&#8217;s Objective-C Style Guide<\/a><\/p>\n<p><a href=\"http:\/\/developer.apple.com\/library\/mac\/#documentation\/Cocoa\/Conceptual\/CodingGuidelines\/CodingGuidelines.html\">Apple&#8217;s Coding Guidelines for Cocoa<\/a><\/p>\n<h2>Autoformatting<\/h2>\n<p>If you&#8217;re just catching up on good style, you may have a lot of source that suddenly looks poorly formatted to you. \u00c2\u00a0Fear not! \u00c2\u00a0You can clean it up automatically using a couple tools. The first is Xcode itself, which knows a fair amount about how to indent those lines. \u00c2\u00a0Press command-comma in Xcode to open the Preferences window, mosey on over to the Indentation section, and set your options.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-110\" title=\"Screen shot 2010-12-17 at 12.50.31 PM\" src=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/12\/Screen-shot-2010-12-17-at-12.50.31-PM.png\" alt=\"\" width=\"810\" height=\"545\" srcset=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/12\/Screen-shot-2010-12-17-at-12.50.31-PM.png 810w, http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/12\/Screen-shot-2010-12-17-at-12.50.31-PM-300x201.png 300w\" sizes=\"(max-width: 810px) 100vw, 810px\" \/><\/p>\n<p>To auto-indent a section of code, select it (command-A to select all) and press control-I (that&#8217;s control-not-command + i).<\/p>\n<p>But Xcode doesn&#8217;t handle advanced formatting details, such as enforcing consistent spacing between various elements. \u00c2\u00a0To do a good job with this, you can use the uncrustify tool that comes with <a href=\"http:\/\/universalindent.sourceforge.net\/index.php\">UniversalIndentGUI<\/a>. \u00c2\u00a0After trying out this tool for a while, I decided that I prefer to use uncrustify directly from the command-line. \u00c2\u00a0The GUI seems slightly buggy. \u00c2\u00a0You can autoformat a source file with a command-line like this:<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">\/Applications\/UniversalIndentGUI\/indenters\/uncrustify -c uncrustify.cfg -lOC -f MyFile.m -o MyFile.m<\/div><\/div>\n<p>Of course, you may have to edit the paths appropriately for that to work in your setup. \u00c2\u00a0I&#8217;ve added a sample <a href=\"https:\/\/github.com\/tylerneylon\/moriarty\/blob\/master\/uncrustify.cfg\">uncrustify.cfg config file to the moriarty library<\/a>. \u00c2\u00a0This file contains all the specific details about how you want your code formatted. \u00c2\u00a0The most concise documentation I was able to find on the format of this file is in <a href=\"http:\/\/uncrustify.sourceforge.net\/default.cfg\">the comments of this sample cfg file<\/a>.<\/p>\n<p>In addition, it turns out there are ways to incorporate command-line tools directly into Xcode. \u00c2\u00a0In theory, legend has it that you can set up a custom user script (these are under the scroll-looking icon in Xcode&#8217;s menu) to be able to use uncrustify to auto-format selected code with a keyboard shortcut within Xcode. \u00c2\u00a0I was not able to actually get this to work, but if you are feeling brave, here is <a href=\"http:\/\/hackertoys.com\/2008\/09\/18\/adding-a-code-beautifier-script-to-xcode\/\">the blog post where I first heard such tales<\/a>.<\/p>\n<p>Time to make some beautifully formatted code!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, I&#8217;ll give a quick overview on choosing a personal coding style, and present a way to auto-format code in bulk. If you take your code seriously, you probably have a code style that you consistently use. For example, maybe you always use braces with every if clause, or you use underscores in [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[40,27],"tags":[91,56,93,92],"_links":{"self":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/109"}],"collection":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=109"}],"version-history":[{"count":0,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions"}],"wp:attachment":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}