{"id":5377,"date":"2018-01-14T13:00:41","date_gmt":"2018-01-14T13:00:41","guid":{"rendered":"http:\/\/writeasync.net\/?p=5377"},"modified":"2018-01-08T01:01:22","modified_gmt":"2018-01-08T01:01:22","slug":"object-calisthenics-and-advent-of-code","status":"publish","type":"post","link":"http:\/\/writeasync.net\/?p=5377","title":{"rendered":"Object Calisthenics and Advent of Code"},"content":{"rendered":"<p>A few weeks back, I became aware of <a href=\"http:\/\/adventofcode.com\/2017\">Advent of Code<\/a>, a site built by <a href=\"http:\/\/was.tl\/\">Eric Wastl<\/a>. AoC is basically an annual 25 day programming challenge\/competition which has run every December for the past three years. Since I had the time over the holiday break, I gave it a try. I started too late to be in the running for the leaderboard, so I treated it as more of an &#8220;expanded <a href=\"https:\/\/en.wikipedia.org\/wiki\/Kata_(programming)\">kata<\/a>&#8221; &#8212; a way to try out coding ideas in a relatively low-pressure environment. I have to hand it to Eric for creating such an engaging and entertaining set of puzzles. Having run the AoC gauntlet &#8212; and some parts were quite difficult, at least for me! &#8212; I feel I emerged a better programmer.<\/p>\n<p>Given that I was already in the practicing\/experimenting mindset, I kept thinking of <a href=\"http:\/\/www.xpteam.com\/jeff\/writings\/\">Jeff Bay<\/a>&#8216;s &#8220;<a href=\"https:\/\/blogs.msdn.microsoft.com\/cellfish\/2009\/08\/15\/object-calisthenics-first-contact\/\">Object Calisthenics<\/a>&#8220;. Up to that point, I had only minimal direct practice with this set of rules which, in his words, represent &#8220;far stricter coding standards than you&#8217;ve ever used in your life.&#8221;<\/p>\n<p>As I went through the challenges, one rule in particular kept coming up: <strong>first class collections<\/strong>. Basically, instead of using raw collection types like <code>List<\/code> or <code>Dictionary<\/code>, you are supposed to wrap the collection in a class. Here is an example from Day 8 of AoC 2017, where you are supposed to manage a set of integer-valued registers:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nclass Registers\r\n{\r\n    private readonly Dictionary&lt;string, int&gt; values;\r\n\r\n    public Registers()\r\n    {\r\n        this.values = new Dictionary&lt;string, int&gt;();\r\n    }\r\n\r\n    public int MaxValueEver { get; private set; }\r\n\r\n    public int MaxValue() =&gt; this.values.Values.Max();\r\n\r\n    public void Increment(string register, int value)\r\n    {\r\n        int start = this.Get(register);\r\n        this.values&#x5B;register] = this.CheckMax(start + value);\r\n    }\r\n\r\n    public int Get(string register)\r\n    {\r\n        int value;\r\n        if (!this.values.TryGetValue(register, out value))\r\n        {\r\n            value = 0;\r\n            this.values.Add(register, value);\r\n        }\r\n\r\n        return value;\r\n    }\r\n\r\n    private int CheckMax(int value)\r\n    {\r\n        if (value &gt; this.MaxValueEver)\r\n        {\r\n            this.MaxValueEver = value;\r\n        }\r\n\r\n        return value;\r\n    }\r\n}\r\n<\/pre>\n<p>In faithful Object Calisthenics style, the class has only one member which is the collection itself. Given that the collection is hidden and only accessible indirectly via public methods, a constrained set of operations grew out of this. Rather than being unnecessarily strict, to me this was a big improvement over some common alternatives where code dealing with raw collections gets spread widely in an application. (Think of how often you have seen code like the conditional get-or-add in the <code>Get()<\/code> method land in three or four separate call sites.)<\/p>\n<p>Despite Jeff&#8217;s permission to &#8220;relax and go back&#8221; to my old ways after the exercise is done, I can see myself continuing to strive for first class collections in more of my daily coding work.<\/p>\n<p>For my full set of AoC solutions (C#, .NET 4.6.1), check out my <a href=\"https:\/\/github.com\/brian-dot-net\/advent2017\">advent2017<\/a> GitHub repo.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few weeks back, I became aware of Advent of Code, a site built by Eric Wastl. AoC is basically an annual 25 day programming challenge\/competition which has run every December for the past three years. Since I had the&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[91],"tags":[],"class_list":["post-5377","post","type-post","status-publish","format-standard","hentry","category-design"],"_links":{"self":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/5377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5377"}],"version-history":[{"count":1,"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/5377\/revisions"}],"predecessor-version":[{"id":5378,"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/5377\/revisions\/5378"}],"wp:attachment":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5377"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}