tag:blogger.com,1999:blog-87217382465396872282024-03-08T10:31:59.724-06:00KaizenThe account of my apprenticeship at 8th Light.Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.comBlogger32125tag:blogger.com,1999:blog-8721738246539687228.post-21872061350999152322012-02-09T18:38:00.000-06:002012-02-09T18:38:10.887-06:00Pairing Tour: Day 7<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Today I got to pair with Doug Bradbury on some Rails work. Doug is currently working on a new project, so we got the chance to lay some of the foundation of the app. All of the stories for the iteration are currently blocked because they are dependent on work being done by other developers, so we got they rare opportunity to spend the day refactoring. For this Rails app we are trying to use the Boundary, Interactor (Control), Entity pattern <a href="http://www.cleancoders.com/codecast/clean-code-episode-7/show">inspired by Uncle Bob</a>. The goal of this is to make our tests much faster. By decoupling our business logic from our delivery mechanism, we will produce a better design that is easier to test. So we started by refactoring a controller action to not depend on an Active Record model. The required us to isolate the calls and return values from active record in our interactor (business logic module). Once we finished this we decided that we wanted to remove the dependency on Active Record from our interactor. So we started to abstract the calls to Active Record into Database transaction objects. These objects represent one transaction with the database in the same way that application interactors represent one business rule. So, we created one Transaction class title FindAuthorizationByNameAndUid. The name of transaction itself clearly represents what this module is supposed to do. It can be implemented using Active Record or any other persistence mechanism. Most importantly, it can be easily mocked out in tests my an in-memory equivalent. By the end of the day we were able to remove Active Record models from the interactor and put them into transaction objects. The refactoring of the code was really easy. Since our interactor was already tested, we made all the refactors that we wanted and the tests still passed. However, we were unable to refactor our tests to not use active record objects. This is little bigger of a task because the in-memory test double must be built first. So, I'm going to pair with Doug again in the morning to see if we can find a good way to test the interactor without using the Database. All in all, it was a very enlightening day on how to decouple application logic away from Rails.</span>Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-43956624519085128822012-02-08T22:11:00.002-06:002012-02-08T22:11:28.708-06:00Pairing Tour: Day 6<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Today I got the opportunity to pair with Craig Demyanovich. The first part of the day we attended a project retrospective facilitated by Uncle Bob. The retro provided a lot of context as to the history of the project, the team members, feelings about progress and the goals of the project. Essentially, the project is a "great big redesign in the sky". They have decided that their current application is no longer meeting the needs of the business, especially in the area of extensibility. As their business has grown, there has been an obvious desire to extend their application to serve more markets are more consumers. However, the architecture of their current system isn't able to support the growth of their business. So they have decided to redesign a new one. In some ways the new system is rebuilding the old application, but in many other ways, the new system is a "platform" in which to build other applications on top of. I say platform in a very loose way, because it's not really a platform. However, they have utilized service oriented architecture with the mindset that many other applications will need to consume the business logic that they are building right now. This is intelligent way to approach the problem of future extensibility. The complexity of the SOA has caused a few pain points along the way, however, the team seems to be handle the application very well by practicing the disciplines of TDD, pair programming, and continuous integration, all of which are new to the company. The team has done a remarkable job of coming into a fresh project and practicing disciples that they did not use on previous projects. The second half of the day, Craig and I were able to work on a few stories for the application. Close to the end of the day, we ran into an issue with the Ruby Date class. Our problem was simple, we needed to check that a given date was less than or equal one year from the current date. Here was our first try.</span><br />
<br />
<div class="highlight"><code><span class="p">(</span><span class="no">Date</span><span class="o">.</span><span class="n">current</span> <span class="o">-</span> <span class="n">given_date</span><span class="p">)</span> <span class="o"><</span> <span class="p">(</span><span class="no">Date</span><span class="o">.</span><span class="n">current</span> <span class="o">-</span> <span class="mi">1</span><span class="o">.</span><span class="n">year</span><span class="p">)</span>
</code></div>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">However, this didn't work at all. The expression on the left side takes the current date and subtracts the given date, with yields a Rational, representing the amount of days between them. The right hand side takes the current day and subtracts one year, which yields a Date one year ago. So in the end we were comparing a rational to a Date which blew up. Our next try was </span>
<br />
<br />
<div class="highlight"><code><span class="p">(</span><span class="no">Date</span><span class="o">.</span><span class="n">current</span> <span class="o">-</span> <span class="n">given_date</span><span class="p">)</span> <span class="o"><</span> <span class="p">(</span><span class="no">Date</span><span class="o">.</span><span class="n">current</span> <span class="o">-</span> <span class="mi">1</span><span class="o">.</span><span class="n">year</span><span class="o">.</span><span class="n">ago</span><span class="p">)</span>
</code></div>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">This time the right hand side takes the current day and subtracts it from 1 year ago. However, this doesn't work either because <code>1.year.ago</code> yields a DateTime object as opposed to a Date object, so this blew up too. So we massaged this a little to get it to work.</span>
<br />
<br /><div class="highlight">
<code><span class="p">(</span><span class="no">Date</span><span class="o">.</span><span class="n">current</span> <span class="o">-</span> <span class="n">given_date</span><span class="p">)</span> <span class="o"><</span> <span class="p">(</span><span class="no">Date</span><span class="o">.</span><span class="n">current</span> <span class="o">-</span> <span class="mi">1</span><span class="o">.</span><span class="n">year</span><span class="o">.</span><span class="n">ago</span><span class="o">.</span><span class="n">to_date</span><span class="p">)</span>
</code></div>
Finally! Both sides of the expression produce a Rational object which can be compared to each other!Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-12172972908045090102012-02-07T23:31:00.001-06:002012-02-07T23:31:11.523-06:00Pairing Tour: Day 5Today I got to pair with Brian Pratt. Since Brian was transitioning from one client project to another today, I got to sit in on the IPM for Brian's new project in the morning. Most of the stories picked out for the next iteration were front-end features and refactorings to prepare for a new UI design. So, in the afternoon we picked up one of the UI stories. This is a shift from my normal work, so it was a good learning experience. We had to use mock ups and a demo app as a template in which to redesign the header and footer of their website. This doesn't sound terribly complicated, but every one knows that HTML and CSS never like to play nice. We got caught up on a few styling issues throughout the day, but by the end of the day were able to successfully implement the design. I definitely learned a lot about working with designs and mock ups, as well as a few CSS caveats today. Although, one thing is for sure, I'm glad that I'm a developer and not a designer. On another note, I'm making some great progress in my refactoring of Jasmine, the BDD framework for Javascript. Over the weekend I was able to get all of the preexisting tests passing again. Now that the system is working as it was before, I believe that my next steps are going to be redesigning the internals (refactoring out the "Env" God object) and rewriting all the tests. Currently, Jasmine is tested with Jasmine. The source is used to test the source. This seemed cool to me at first, but now I'm convinced that it is a really bad idea. For instance, I can break all of the tests in the system by commenting out one line (the "describe" definition). This will essentially turn describe into a noop, leaving the system with no tests. So, I'm going to rewrite the tests without Jasmine. Also, there are some pretty nasty SOLID violations in there as well, so I would like to take a golden hammer to a few of those files. Should be fun.Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-64396653153617134772012-02-07T00:36:00.002-06:002012-02-07T00:36:27.512-06:00Pairing Tour: Day 4Today I had the opportunity to spend the day with Dave Moore on some client work. We were able to work on a few interesting stories throughout the day. Dave is currently on a team that is working on internationalizing a Rails application. I was able to pair with Dave while we tried to internationalize the social media widgets on the site, specifically the Facebook login, button, like button, and login window. This can be accomplished through calls to the FB API. However, we were seeing a very specific bug with Facebook's Api. When we opened the application in a specific language, say German, the Facebook button would render in German, no problem. Also, we could click on the login button and the FB Connect window would show up in German, no problem. Also, once we changed languages on our application, say we switched to French, the buttons would render find in French, all through the FB API, no problem. However, after changing languages in our application, the FB API login window would not change languages with us, even though the FB button was changing languages with us. After some searching we deduced that FB renders it's buttons solely off of the API calls you make to it, which can change on every page refresh. However, it renders it's login window based on the language that was specified when we first logged in, so we would see the window in German even though we had switched to French. We were able to track that FB was storing the locale information that we first gave to it within a cookie on the browser. So, our first instinct was to attempt to mutate the FB cookie every time we changed languages in our application. However, after some searching we found that altering cross domain cookies is not allowed. So, at this point we were essentially stuck. Facebook is going to render the login window based on the first API made to it. I definitely learned a lesson today about 3rd party dependencies. Even though they are extremely convenient at times, they come at a price of freedom. When you use a 3rd party library, you lose the freedom to manipulate every detail, and as I learned today, that can be a high price to pay.Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-51357093917497210782012-02-04T02:04:00.002-06:002012-02-04T02:04:50.805-06:00Javascript WoesEver since I attended GeekFest at Groupon on Tuesday, I have been interested in working with Javascript a little more. The talk was about sharing code between client and server via Node.js. It really got me curious about what that would look like on real project. However, I've not had the opportunity to try it out yet. My exploration into Node.js and Javascript over the past week has led me down a very dark and desperate path. The first woe that I encountered in my exploration was modules. As I tinkered, I began to realize that defining and loading modules was a very fragile process. I was quickly dismayed. I did some googling and found and library called RequireJS. This library implements AMD protocol for Javascript modules, which is great because it offers a standard definition that Node and Browsers can use. So, in theory, one could write one application that runs in both places. I was happy again. Then I decided to explore some previously existing Javascript projects to see how they handle modularization. I took a look at Jasmine, the BDD framework for Javascript. As I was browsing their code, I realized that they didn't attempt to use any sort of module loading like I hoped. Instead, they compile everything into one file, test against it, and distribute it as such. This isn't a bad method per se, but I think that it is possible to do better now that the toolset for Node has matured. Also, I was really displeased to see that Jasmine has a lot of Ruby files floating around their repository (mostly rake tasks). This also isn't bad per se, but once again, the Node toolset has matured and I think its possible to do better now. So, with that in mind, I decided I wanted to hack on Jasmine a little bit. First, I deleted all the Ruby files. Second, I decided to reorganize the project structure to separate Node specific files from Browser specific files. Now, I'm trying to convert all of Jasmine core into Node modules via RequireJS. This is when the headaches started. As it turns out, it is hard to test a testing framework. For instance, when I change the test Runner, not only does that break the tests, but it breaks the runner that which shows me that the tests are broken. I know, headaches. Anyways, I'm continuing to refactor Jasmine and learn more about Node. I'm hoping that soon I will have enough grasp to write a simple Todo's application where code is shared between client and server.Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com1tag:blogger.com,1999:blog-8721738246539687228.post-10453600920467633932012-02-02T23:32:00.000-06:002012-02-02T23:32:46.632-06:00Pairing Tour: Day 3Today I continued my pairing tour with Steven Degutis. I worked with him on a client's Rails app. However, this particular client has a very interesting setup with their application. They have brought service oriented architecture to the Rails world. Their application is actually multiple applications communicating with each other via HTTP requests. This may sound pretty messy, but they have managed to isolate the service calls into gems, which are managed and tested on their own. So each service (i.e. Rails app) has its own gem that wraps all the HTTP calls so that the user of the service is none the wiser. This really doesn't seem to bad. However, in practice it hasn't turned out so well. The have experienced some pain in adding features to the services. Adding a feature to a services means updating all of it's clients as well. This is not an inherent aspect of SOA, but it is a sign that they have broken the Common Closure Principle. They are starting to see that many of these separate modules all change together, which means that they should be kept together in the same package. Unfortunately, there is no simple answer to solving this problem. Bring the clients and services together into the same package is going be very difficult, especially since both clients and services are Rails apps. So, I definitely learned a lesson about the CCP today. On another note, pairing with Steven was a fun time. He is very discipled. He sticks to his principles well. And, he is very fast. I don't think I have ever seen anyone type and maneuver around Vim like he does. I definitely leaned a lot by pairing with him. I'm not pairing with a Craftsman tomorrow, because it's Friday, so I get to work on some Open Source stuff. I've been interested in Jasmine, the BDD framework for Javascript lately, so I'm planning to work on that.Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-51955768731897292912012-02-01T22:44:00.001-06:002012-02-01T22:44:16.316-06:00Pairing Tour: Day 2Today I had the opportunity to pair with Mike Jansen on some client work. He is currently working on a Rails app with some heavy client side Javascript via Backbone.js. We worked on a mix of bug fixes and new stories. Most of the bug fixes were in the Javascript code, while the new stories were in the Rails app, so I got to work on a variety of interesting stories. However, I didn't get to work on any Backbone code. In general, I'm a fan of client side Javascript frameworks and I like the way the our client is using Backbone in their app. I believe it has awarded them with a really rich UI. The Rails app I worked on today was nowhere near as large as the app that I worked on yesterday (1,500 specs vs 15,000 specs). This made a large difference in development feedback and turn around. We were able to fix a bug in the morning, push it to the CI server, and QA gave us feedback with in a few hours. Another plus was that we were able to run the entire test suite (acceptance + unit) locally. Even though this is better than having to wait 45 mins to get feedback from the CI server, it still took 5-10 minutes to run the suite. I think I'm starting to get a more clear picture of the dream world I have been living in the past few months of only Clojure development. The only thing limiting the feedback time on those projects was the JVM boot up time (~3 secs). At any rate, I'm grateful that I have been able to get a better understanding of what development will be like on client projects. Continuing my theme from yesterday about developer's personalized environment, I noticed that Mike has a very similar setup as Eric, besides that he uses MacVim, which is fortunately very easy for Textmate users like myself to pick up on. I'm starting to see that the Craftsmen at 8th Light really take ownership of their environments and customize them so that they can work as fast as they can. I'm hoping to take note of what other Craftsmen are using a start personalizing my environment a little more, albeit cautiously with version control. Tomorrow, I am off to pair with Steven Degutis on some more client work. Should be fun!Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-20617480480065162112012-02-01T01:17:00.001-06:002012-02-01T01:17:08.554-06:00Pairing Tour: Day 1<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">As part of my Apprenticeship, I get the pleasure of going on a pairing tour with four 8th Light Craftsmen. While pairing, the Craftsmen will be working on client projects, so I will gain some exposure to our current clients and an idea of what client work is like. This also gives me a great opportunity to learn from the Craftsmen I will be pairing with. The tour also gives a few Craftsmen some exposure to me, which will allow them to evaluate me fairly at the end of my apprenticeship. Today marked my first day pairing with a Craftsmen, Eric Meyer, whom I met with at a client office. We worked on two interesting stories that kept us busy all day. A few things a took away from the day. 1) Eric really takes ownership of his development environment. He was able to navigate around his system very quickly. He has lots of bash aliases set up. He has even set up a special windowing system. Recently, I made a conscious decision to avoid extravagant customization of my development machine because it makes context switching (changing machines, pairing on someone else's environment, etc...) a lot harder. I want to stay flexible with my set up. However, after watching Eric today, I'm starting to rethink that decision. 2) Continuous Integration servers (done right) can be pretty sweet. The CI server at the client had a lot of cool features, such as testing a branch based on the branch name, testing and then merging branches automatically. However, the downside to the really cool CI server is that it takes 45 minutes to run the test suite (Ouch!). This makes it hard to get instant feedback. Yet another example of why Rails needs to be contained. 3) The client we were at had a really cool talk over lunch today about reusing client and server code via node.js. It was extremely intriguing. It looks great in theory and I would love to build an application to see how well it works out in reality. Hopefully someday soon.</span>Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-62001874309839514172012-01-30T17:15:00.004-06:002012-01-30T17:15:58.945-06:00Artisan: Week 2It was a pretty quiet day at the 8th Light office today. As such, I was able to study the Artisan (our project management tool) code base more in depth along with completing some small clean up stories. Looking at Artisan today reminded me of my first week at my previous job. Diving head first into an existing code base can be overwhelming regardless of how large it is. Artisan's code base is by no means bad and is fairly well maintained, but it is still daunting to try and grasp everything that is going on. I've used two methods to help me try and understand features in the code base. 1) I look at specific use case in the UI and then try and trace the feature the entire way through the system. 2) Look at the tests. This method may be harder for those who don't have a large suite of tests wrapping their application, but lucky for me Artisan is well tested. However, some tests with in the code base are really hard to read, especially tests that make heavy use of stubbing. This had made me realize that I probably shouldn't use much stubbing in my tests because it is really hard for others to understand. Setting up five stubs, then calling one method isn't really too helpful to read and doesn't always reveal the intend of the test. So, this is yet another reason that I prefer to use hand rolled mocks over framework stubs. Overall, I think I'm learning a lot about Rails development through working on Artisan, but there are still some pain points (like slooooow tests).Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-44825398638822756632012-01-27T22:49:00.002-06:002012-01-27T22:49:37.335-06:00First Live Kata<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">I had the pleasure of presenting the Ruby to JSON kata that I posted a few days ago at <a href="http://university.8thlight.com/">8th Light University</a> today. I had a few issues towards the end of my kata, but overall it went well. When I first started practicing the Ruby to JSON kata, it took me around 10-12 minutes to complete. The video I posted a few days ago took me about 8 minutes. Today it took me about 6. In order to cut the time in half I had to practice this kata relentlessly for a few days. I think that I have probably written this kata close to 50 times now. But, that is what it takes to perform a cool kata in 5ish minutes. One cool thing that I did for my kata was playing music in the background. I made this decision because my kata did not really need any specific narration and I didn't want the silence to be awkward. Through this I discovered that it is really hard to perform a kata along with music. I don't just mean playing music in the background while you code, but actually pacing yourself to play along with the music. Towards the end of my practicing for the kata, I was able to do this pretty well. I picked a song throughout that lasted 5:30 and was able to track myself with certain markers in the song. However, during the live presentation, it flopped because I was so nervous. At first, I was typing really slow and got really behind in the song. Then, I had a few typing mistakes that put me behind further. In the end, I finished about 30 seconds after the song, which stinks, because in my practice I would usually end the kata about 15-30 seconds early (the song fades at the end). Overall, I believe that I benefitted a lot from performing the kata and I think (hope) that the views did as well.</span>Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-9818475987826382492012-01-26T17:51:00.002-06:002012-01-26T17:51:51.881-06:00Working with Rails againAfter working with Clojure for the majority of my apprenticeship, I'm now working on a Rails project. The transition has been pretty smooth, but nonetheless it has certainly been a different experience. In may ways, it feels like I am learning Rails all over. In my previous experience with Rails projects I never practiced TDD. So, now I am learning how to write good tests with rspec. Which, as it turns out, is kind of hard in Rails. Also, I'm learning how to write testable (i.e. good) code in Ruby, something I have never done. One of the ways we are attempting to write testable Rails code is abstracting all the actually application logic away from Rails. This is great. It makes it really easy to test the core application logic without being coupled to Rails. What sucks about this is that there really isn't that much core application logic, or at least there is not that much abstracted. The bulk of the application is the UI. Therefore, most of features are implemented with a combination of Haml, Coffee Script and Ruby, wherein lies the difficulty in testing. For each feature, we have to write rspec tests, Jasmine tests, and cucumber acceptance tests. Something about this doesn't sit quite right with me, however, I'm not quite sure why yet. Perhaps I will write a future blog analyzing what about this perturbs me. However, the transition to Rails has made me realize a one things about Clojure development that I took for granted. The Speclj auto runner. Writing tests an getting instant feedback is a complete game changer. I didn't realize this until we started writing tests a few days ago and had to manually run the tests every time we changed something. It was a complete waste of time. So we started using rspec-guard, a nifty gem that auto runs rspec tests upon change. However, this was only a partial solution. After getting guard up and running with our project, I realized that I wasn't getting the instant feedback loop that Speclj provided me. This is because every time guard detects a change, it has to reload the spec helper file, which essentially reloads Rails. So, every time I changed a spec file that has two tests in it, I have to wait 10 seconds for Rails to load. This is huge time waster. I'm guessing that the solution to this problem is to not use Rails, or at least isolate it as much as possible. However, that doesn't seem like too feasible of a solution with the current project I'm working on because as I said previously, there isn't really that much application logic. So, what can I do? Anyways, moving back in to Rails code has been interesting to say the least.Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-32600996148999180122012-01-26T12:04:00.002-06:002012-01-26T12:53:24.780-06:00Chicago Clojure Meet Up: Overtone<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Last night I had the pleasure of attending the Chicago Clojure meet up on Overtone. This was actually the first meet up I've ever attended. Over all, the experience was really cool. First, I was pleased to see 20 people gathered about Clojure. I love Clojure and I was excited to see other people passionate about Clojure. Second, the software demoed, Overtone, was really cool. Kevin Neaton, one of the contributors, gave an hour and a half demo on the capabilities of the Overtone, and a brief preview of where the project is going. For those unfamiliar, Overtone is an open source project that allows users to create musical synthesizers. So, Kevin started the night by showing us a very simple sine wave and then incrementally built upon it throughout the night until he created a dub-step beat. Pretty cool. The Overtone project offers the benefit of being written in Clojure, which offers some cool features, for instance, recursive beats. The built-in recursion of Clojure allows users to create recursive beats with ease. Also, it offers users the REPL. Users can fire up the REPL and create, test and demo beats on the fly without a ritzy setup. Overtone seems like a great tool for musicians and audiophiles alike. However, being neither of the two, I don't see myself using Overtone much, except of course to play around a see what comes out. Overall, I had a great time at the meet up and I hope to keeping attending.</span>Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-41867677362773135452012-01-24T19:31:00.006-06:002012-01-24T19:31:56.659-06:00Ruby to JSON Kata<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">I spent a large portion of my day today practicing for the Kata that I will be presenting on Friday. My original intention for this Kata was to take a well known kata that has a commonly accepted solution and rewrite it without using mutable data. Unfortunately, I was unable to find an existing kata that would showcase immutable data well. So, I decided to use the kata written by a fellow apprentice, Patrick Gombert, which does showcase immutable data rather well. Following is the Ruby implementation of this kata. Originally, this kata was written using Clojure, which really showcased immutable data. I believe that my implementation could be greatly improved by Clojure multimethods, which is what I would like to try tomorrow.
</span><br />
<br />
<div class="highlight"><code><span class="c1"># Acceptace Criteria</span>
<span class="c1"># ------------------</span>
<span class="c1"># nil -> null</span>
<span class="c1"># Fixnum</span>
<span class="c1"># Float</span>
<span class="c1"># Booleans</span>
<span class="c1"># Strings</span>
<span class="c1"># Symbols -> converted to strings</span>
<span class="c1"># Arrays -> all values converted to JSON</span>
<span class="c1"># -> values can be anything</span>
<span class="c1"># Hashes -> all values and keys converted to JSON</span>
<span class="c1"># -> ignores keys that are not Symbols or Strings</span>
<span class="c1"># -> values can be anything</span>
<span class="k">def</span> <span class="nf">str_or_sym?</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">data</span><span class="o">.</span><span class="n">is_a?</span> <span class="nb">String</span> <span class="ow">or</span> <span class="n">data</span><span class="o">.</span><span class="n">is_a?</span> <span class="no">Symbol</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">convert</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">nil?</span>
<span class="s2">"null"</span>
<span class="k">elsif</span> <span class="n">str_or_sym?</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="s2">"</span><span class="se">\"</span><span class="si">#{</span><span class="n">data</span><span class="si">}</span><span class="se">\"</span><span class="s2">"</span>
<span class="k">elsif</span> <span class="n">data</span><span class="o">.</span><span class="n">is_a?</span> <span class="nb">Array</span>
<span class="s2">"[</span><span class="si">#{</span><span class="n">data</span><span class="o">.</span><span class="n">map</span> <span class="p">{</span><span class="o">|</span><span class="n">entry</span><span class="o">|</span> <span class="n">convert</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span><span class="si">}</span><span class="s2">.join("</span><span class="p">,</span> <span class="s2">")}]"</span>
<span class="k">elsif</span> <span class="n">data</span><span class="o">.</span><span class="n">is_a?</span> <span class="no">Hash</span>
<span class="s2">"{</span><span class="si">#{</span><span class="n">data</span><span class="o">.</span><span class="n">map</span> <span class="p">{</span><span class="o">|</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="o">|</span> <span class="s2">"</span><span class="si">#{</span><span class="n">convert</span><span class="p">(</span><span class="n">key</span><span class="p">)</span><span class="si">}</span><span class="s2">: </span><span class="si">#{</span><span class="n">convert</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span> <span class="k">if</span> <span class="n">str_or_sym?</span><span class="p">(</span><span class="n">key</span><span class="p">)</span><span class="si">}</span><span class="s2">.select {|x| !x.nil?}.join("</span><span class="p">,</span> <span class="s2">")}}"</span>
<span class="k">else</span>
<span class="n">data</span><span class="o">.</span><span class="n">to_s</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">"Ruby to JSON"</span> <span class="k">do</span>
<span class="o">[</span>
<span class="o">[</span><span class="kp">nil</span><span class="p">,</span> <span class="s2">"null"</span><span class="o">]</span><span class="p">,</span>
<span class="o">[</span><span class="mi">1</span><span class="p">,</span> <span class="s2">"1"</span><span class="o">]</span><span class="p">,</span>
<span class="o">[</span><span class="mi">1</span><span class="o">.</span><span class="mi">0</span><span class="p">,</span> <span class="s2">"1.0"</span><span class="o">]</span><span class="p">,</span>
<span class="o">[</span><span class="kp">true</span><span class="p">,</span> <span class="s2">"true"</span><span class="o">]</span><span class="p">,</span>
<span class="o">[</span><span class="kp">false</span><span class="p">,</span> <span class="s2">"false"</span><span class="o">]</span><span class="p">,</span>
<span class="o">[</span><span class="s2">"a"</span><span class="p">,</span> <span class="s2">"</span><span class="se">\"</span><span class="s2">a</span><span class="se">\"</span><span class="s2">"</span><span class="o">]</span><span class="p">,</span>
<span class="o">[</span><span class="ss">:a</span><span class="p">,</span> <span class="s2">"</span><span class="se">\"</span><span class="s2">a</span><span class="se">\"</span><span class="s2">"</span><span class="o">]</span><span class="p">,</span>
<span class="o">[[</span><span class="kp">nil</span><span class="p">,</span> <span class="ss">:a</span><span class="o">]</span><span class="p">,</span> <span class="s2">"[null, </span><span class="se">\"</span><span class="s2">a</span><span class="se">\"</span><span class="s2">]"</span><span class="o">]</span><span class="p">,</span>
<span class="o">[</span><span class="p">{</span><span class="ss">:a</span> <span class="o">=></span> <span class="kp">nil</span><span class="p">,</span> <span class="ss">:b</span> <span class="o">=></span> <span class="ss">:c</span><span class="p">},</span> <span class="s2">"{</span><span class="se">\"</span><span class="s2">a</span><span class="se">\"</span><span class="s2">: null, </span><span class="se">\"</span><span class="s2">b</span><span class="se">\"</span><span class="s2">: </span><span class="se">\"</span><span class="s2">c</span><span class="se">\"</span><span class="s2">}"</span><span class="o">]</span>
<span class="o">].</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">value</span><span class="p">,</span> <span class="n">json</span><span class="o">|</span>
<span class="n">it</span> <span class="s2">"convert </span><span class="si">#{</span><span class="n">value</span><span class="o">.</span><span class="n">class</span><span class="si">}</span><span class="s2">"</span> <span class="k">do</span>
<span class="n">convert</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">should</span> <span class="o">==</span> <span class="n">json</span>
<span class="k">end</span>
<span class="n">it</span> <span class="s2">"array values can be </span><span class="si">#{</span><span class="n">value</span><span class="o">.</span><span class="n">class</span><span class="si">}</span><span class="s2">"</span> <span class="k">do</span>
<span class="n">convert</span><span class="p">(</span><span class="o">[</span><span class="n">value</span><span class="o">]</span><span class="p">)</span><span class="o">.</span><span class="n">should</span> <span class="o">==</span> <span class="s2">"[</span><span class="si">#{</span><span class="n">json</span><span class="si">}</span><span class="s2">]"</span>
<span class="k">end</span>
<span class="k">unless</span> <span class="n">str_or_sym?</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
<span class="n">it</span> <span class="s2">"ignores hash key type </span><span class="si">#{</span><span class="n">value</span><span class="o">.</span><span class="n">class</span><span class="si">}</span><span class="s2">"</span> <span class="k">do</span>
<span class="n">convert</span><span class="p">({</span><span class="n">value</span> <span class="o">=></span> <span class="mi">1</span><span class="p">,</span> <span class="ss">:a</span> <span class="o">=></span> <span class="mi">1</span><span class="p">})</span><span class="o">.</span><span class="n">should</span> <span class="o">==</span> <span class="s2">"{</span><span class="se">\"</span><span class="s2">a</span><span class="se">\"</span><span class="s2">: 1}"</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">it</span> <span class="s2">"hash values can be </span><span class="si">#{</span><span class="n">value</span><span class="o">.</span><span class="n">class</span><span class="si">}</span><span class="s2">"</span> <span class="k">do</span>
<span class="n">convert</span><span class="p">({</span><span class="ss">:a</span> <span class="o">=></span> <span class="n">value</span><span class="p">})</span><span class="o">.</span><span class="n">should</span> <span class="o">==</span> <span class="s2">"{</span><span class="se">\"</span><span class="s2">a</span><span class="se">\"</span><span class="s2">: </span><span class="si">#{</span><span class="n">json</span><span class="si">}</span><span class="s2">}"</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</code></div>Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-30713572900385306932012-01-23T20:25:00.003-06:002012-01-23T20:25:52.096-06:00Prime Factors Kata with Immutable Data<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Here's my take on the Prime Factors kata in Ruby. I set out with the goal to use immutable data instead of the normal implementation which mutates an array. Check it out <a href="http://screencast.com/t/ZkvaDXhIKqZ">here</a>.</span>Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-5459377337852918822012-01-20T20:33:00.001-06:002012-01-20T20:33:46.303-06:00Dependency Inversion PrincipleIn a recent project I have been working on, I've been learning a lot about the Dependency Inversion Principle. There were some places in our code base were Micah showed us how we were violating the DIP and showed us how to refactor. In short, the DIP states that no module should be dependent on a concrete class, but rather an abstraction. Violations of this principle are commonly seen in higher level modules depending on lower level modules, such as a UI depending on a Controller or Model. This means that the modules within the UI become coupled and are not able to be reused later. In our case, our UI layer was directly depended on the concrete implementation of our application classes. In order to adhere to the DIP, we needed to refactor so that our UI was not dependent on our application logic. This required that we build two abstractions. We built an abstraction for the application (called an Interactor) and an abstraction for the UI. The UI interface talks to the Interactor interface and the Interactor interface talks to the UI interface. This allows the UI to be decoupled from the application logic. So, in theory, we could build another implementation of the application underneath of the UI and the UI would be none the wiser. By the same token, we can now build any number of UI's on top of the application and the application is non the wiser. The second scenario is not really a theoretical possibility, but reality. There are tentative plans to build a web application on top of the current architecture (in addition to the desktop app we are building now). Without these abstractions, building the new Web UI would turn the code base into a tangled mess that no one could maintain. So, in summary, always program to an interface or abstraction, never a concrete class.Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-34776934187868832022012-01-19T18:42:00.001-06:002012-01-19T18:42:29.608-06:00Test Generation with specljLet's start with a simple test definition in <code>speclj</code>, the BDD testing framework for Clojure.<br />
<br />
<div class="highlight">
<code><span class="p">(</span><span class="nf">describe</span> <span class="s">"my module"</span>
<span class="p">(</span><span class="nf">it</span> <span class="s">"has the correct value"</span>
<span class="p">(</span><span class="k">for </span><span class="p">[</span><span class="nv">item</span> <span class="p">{</span><span class="nv">:item1</span> <span class="s">"value1"</span> <span class="nv">:item2</span> <span class="s">"value2"</span><span class="p">}]</span>
<span class="p">(</span><span class="nf">should=</span> <span class="p">(</span><span class="nb">val </span><span class="nv">item</span><span class="p">)</span> <span class="p">(</span><span class="nf">get-value</span> <span class="nv">item</span><span class="p">)))))</span>
</code></div>
This test simple asserts that <code>my module</code> <code>has the correct value</code> for <code>item1</code> and <code>item2</code>. Pretty simple. Ok, let's say that we add 15 more fields to <code>my module</code>. Our test will now look like this...<br />
<br />
<div class="highlight">
<code><span class="p">(</span><span class="nf">describe</span> <span class="s">"my module"</span>
<span class="p">(</span><span class="nf">it</span> <span class="s">"has the correct value"</span>
<span class="p">(</span><span class="k">for </span><span class="p">[</span><span class="nv">item</span> <span class="p">{</span><span class="nv">:item1</span> <span class="s">"value1"</span> <span class="nv">:item2</span> <span class="s">"value2"</span> <span class="nv">:item3</span> <span class="s">"value3"</span> <span class="p">(</span><span class="nf">etc</span><span class="o">.</span><span class="p">)}]</span>
<span class="p">(</span><span class="nf">should=</span> <span class="p">(</span><span class="nb">val </span><span class="nv">item</span><span class="p">)</span> <span class="p">(</span><span class="nf">get-value</span> <span class="nv">item</span><span class="p">)))))</span>
</code></div>
Not too bad. For every test that we add all we have to do is add a key and value to the for loop. Seems pretty extensible to me. So later, one of the assertions fails. However, we only get one error. Thats not good. That makes it seem like our entire module is broken. We first have to identify will field the test broken on, then we have to go fix the bug. This could be especially painful if there were more like 1000 fields! Ok, so lets find a better solution. Consider this example.<br />
<br />
<div class="highlight">
<code><span class="p">(</span><span class="nf">describe</span> <span class="s">"my module"</span>
<span class="p">(</span><span class="k">for </span><span class="p">[</span><span class="nv">item</span> <span class="p">{</span><span class="nv">:item1</span> <span class="s">"value1"</span> <span class="nv">:item2</span> <span class="s">"value2"</span><span class="p">}]</span>
<span class="p">(</span><span class="nf">it</span> <span class="p">(</span><span class="nb">str </span><span class="s">"has the correct value for "</span> <span class="p">(</span><span class="nb">key </span><span class="p">(</span><span class="nb">name </span><span class="nv">item</span><span class="p">)))</span>
<span class="p">(</span><span class="nf">should=</span> <span class="p">(</span><span class="nb">val </span><span class="nv">item</span><span class="p">)</span> <span class="p">(</span><span class="nf">get-value</span> <span class="nv">item</span><span class="p">)))))</span>
</code></div>
So what is different in this example? Instead of generating assertions inside of the <code>(it...)</code> block, we loop over it and generate a bunch of <code>(it...)</code> blocks. So what is useful about this? If we consider the example above were "my module" has 1000 fields. Using this new method, we will be able to tell exactly which field this test failed on. This is extremely helpful because it cuts out the entire time it takes to find which assertion was the culprit of the failure. Also, when this module fails, not every test will fail. We will get better feedback on what is actually being tested and breaking. So, in summary, if you need to test a large list of items, it's better to generate tests for it rather than generating assertions.<br />
<br />
<br />Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com2tag:blogger.com,1999:blog-8721738246539687228.post-91246250344770608492012-01-18T20:06:00.004-06:002012-01-18T20:06:45.829-06:00ns-resolve in Clojure<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">One interesting feature of Clojure that I was able to make use of today is <code>ns-resolve</code>. This feature allows you to dynamically find a var within a specific namespace. This is especially useful for dynamically calling a function, given a string. I have also used this method in the metis validations project to dynamically load validation functions. Consider the snippet from metis,
</span><br /><br />
<div class="highlight"><code><span class="p">(</span><span class="k">defn </span><span class="nv">get-validation</span> <span class="p">[</span><span class="nv">validatior-key</span><span class="p">]</span>
<span class="p">(</span><span class="nb">if-let </span><span class="p">[</span><span class="k">fn </span><span class="p">(</span><span class="nb">ns-resolve </span><span class="ss">'metis</span><span class="o">.</span><span class="nv">validator</span><span class="o">.</span><span class="nv">validations</span> <span class="p">(</span><span class="nb">symbol </span><span class="p">(</span><span class="nb">name </span><span class="nv">validatior-key</span><span class="p">)))]</span>
<span class="nv">fn</span>
<span class="p">(</span><span class="nf">throw</span> <span class="p">(</span><span class="nf">Exception</span><span class="o">.</span> <span class="p">(</span><span class="nb">str </span><span class="s">"Could not locate the validator: "</span> <span class="p">(</span><span class="nb">name </span><span class="nv">validatior-key</span><span class="p">))))))</span>
</code></div>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Here I'm using <code>ns-resolve</code> slightly differently because this method is within the <code>metis.validator.validations</code> namespace already. This function is used to retrieve a validation function based upon a keyword. For instance, <code>(get-validation :inclusion)</code> returns the function <code>inclusion</code> from the validations namespace. This is allows us to not violate the open/closed principle. Users have access to the entire namespace by only using one method on the namespace. This means that if a new validation is added, the user does not have to change their namespaces imports to use it, they just start using it. In this way, the validator is closed to modification and open to extension.</span>Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-77296542503585638432011-12-16T15:48:00.000-06:002011-12-28T18:05:34.388-06:00Clojure Mocks<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">A few weeks ago a I wrote an HTTP server using Java. While testing, I made heavy usage of Mock objects. I didn't use a mocking framework, just hand rolled mocks. The motivation for this can be seen <a href="http://blog.8thlight.com/josh-cheek/2011/11/28/three-reasons-to-roll-your-own-mocks.html">here</a>. Now that I'm finished with the server I have been working on a Clojure project. Having just written a Java server, I find myself wanting to use mocks to testing everything in Clojure. However, this can be difficult with Objects. Here is how I'm mocking using Clojure.</span>
<br />
<br />
<h3><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Dependency Injection</span></h3>
<br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Dependency Injection can be used similarly to the way one would in Java. The only difference is that dependencies are now functions as opposed to objects.</span>
<br />
<br />
<h3><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Bindings</span></h3>
<br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">This is the method I have taken the most advantage of. You simply bind all functions that the function under test uses. Like so...</span><br /><br />
<div class="highlight"><code><span class="p">(</span><span class="k">def </span><span class="nv">mock-called-count</span> <span class="p">(</span><span class="nf">atom</span> <span class="mi">0</span><span class="p">))</span>
<span class="p">(</span><span class="k">def </span><span class="nv">mock-called-content</span> <span class="p">(</span><span class="nf">atom</span> <span class="nv">nil</span><span class="p">))</span>
<span class="p">(</span><span class="k">defn </span><span class="nv">mock</span> <span class="p">[</span><span class="nv">&</span> <span class="nv">args</span><span class="p">]</span>
<span class="p">(</span><span class="nf">swap!</span> <span class="nv">mock-called-count</span> <span class="o">#</span><span class="p">(</span><span class="nv">inc</span> <span class="nv">%</span><span class="p">))</span>
<span class="p">(</span><span class="nf">reset!</span> <span class="nv">mock-called-content</span> <span class="nv">args</span><span class="p">))</span>
<span class="p">(</span><span class="nb">binding </span><span class="p">[</span><span class="nv">interactor/create</span> <span class="nv">mock</span><span class="p">]</span>
<span class="p">(</span><span class="nf">create</span> <span class="nv">@customer</span><span class="p">))</span></code></div>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">
Here, the create function use the method create in the interactor namespace. So, with the binding in place, when the create function calls interactor/create, it will actually call the mock function. In this case, the mock function just stores the data given to it and increments a call count. However, the possibilities are endless.
</span><br /><br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">
So, with these two great methods of mocking in Clojure, who needs a mocking framework?
</span>Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-83811734280332557152011-12-14T17:26:00.000-06:002011-12-14T17:26:50.423-06:00Iteration Planning<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Today I went through my first iteration planning meeting with a client at 8th Light. My previous experience with iteration planning has only been with internal project managers presenting stories as opposed to customers presenting stories. This offered a different dynamic than my previous experience in that I was being presented stories directly from the customer as opposed to the PM who represents the customer. I really liked that there was virtually no distance between the us and the customers, which allowed us to clear confusing points very quickly. The customers were very easy to work with and were very receptive to our guidance and feedback. We were able to estimate stories and the clients prioritized the stories so that we will be able to produce working software by the end of the iteration. One could say that the planning meet went "by the books" as I have been learning in the Project Management Course. At the end of the meeting it was cool to see satisfaction and agreement between both parties. This project will be the first client work that I do for 8th Light. I'm really excited to start providing value to the business!</span>Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com08th Light, Inc.42.30042053 -88.00178560618.463137029999995 -128.431473106 66.13770403 -47.572098106tag:blogger.com,1999:blog-8721738246539687228.post-25152017881417055082011-12-12T20:30:00.000-06:002011-12-14T08:24:24.846-06:00Implementing Java Interfaces in Clojure<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">I recently ran into some hiccups while trying to implement a Java interface in Clojure. While it the documentation my make it appear seamless, there are a few quirks. I'll explain the different ways I've found of implementing an interface and the hangups/quirks of each.</span><br />
<br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">(Note: I'll be implementing an Interface called "RequestHandler", which is part of a namespace called "HttpServer")</span><br />
<br />
<h1><code>:gen-class</code></h1>
<br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">The unfortunate truth about this method is that it requires AOT compilation. So, in order to get this example to compile, you must add <code>:aot [implement-java-interface.first-request-handler]</code> to your <code>project.clj</code> file. Here's what it looks like.</span><br />
<br />
<div class="highlight"><code><span class="p">(</span><span class="nf">ns</span> <span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">first-request-handler</span>
<span class="p">(</span><span class="nf">:gen-class</span>
<span class="nv">:implements</span> <span class="p">[</span><span class="nv">HttpServer</span><span class="o">.</span><span class="nv">RequestHandler</span><span class="p">]))</span>
<span class="p">(</span><span class="k">defn </span><span class="nv">-canRespond</span> <span class="p">[</span><span class="nv">this</span> <span class="nv">request</span><span class="p">]</span>
<span class="nv">true</span><span class="p">)</span>
<span class="p">(</span><span class="k">defn </span><span class="nv">-getResponse</span> <span class="p">[</span><span class="nv">this</span> <span class="nv">request</span><span class="p">]</span>
<span class="nv">nil</span><span class="p">)</span>
<span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">main=></span> <span class="p">(</span><span class="o">.</span><span class="nv">canRespond</span> <span class="p">(</span><span class="nf">implement_java_interface</span><span class="o">.</span><span class="nv">first_request_handler</span><span class="o">.</span><span class="p">)</span> <span class="nv">nil</span><span class="p">)</span>
<span class="nv">true</span></code></div>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif; white-space: normal;">Looks pretty simple right? The first thing to notice is that to instantiate the class I had to call <code>implement_java_interface.first_request_handler</code> instead of <code>implement-java-interface.first-request-handler</code>. Because of the way that Clojure resolves namespaces, any dashes in namespaces are converted to underscores for file names. Therefore, we generated a class with underscores. There are a few ways to fix this. First, we can use the <code>:name</code> parameter of <code>:gen-class</code></span><br />
<br />
<div class="highlight"><code><span class="p">(</span><span class="nf">ns</span> <span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">first-request-handler</span>
<span class="p">(</span><span class="nf">:gen-class</span>
<span class="nv">:name</span> <span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">first-request-handler</span>
<span class="nv">:implements</span> <span class="p">[</span><span class="nv">HttpServer</span><span class="o">.</span><span class="nv">RequestHandler</span><span class="p">]))</span>
<span class="p">(</span><span class="k">defn </span><span class="nv">-canRespond</span> <span class="p">[</span><span class="nv">this</span> <span class="nv">request</span><span class="p">]</span>
<span class="nv">true</span><span class="p">)</span>
<span class="p">(</span><span class="k">defn </span><span class="nv">-getResponse</span> <span class="p">[</span><span class="nv">this</span> <span class="nv">request</span><span class="p">]</span>
<span class="nv">nil</span><span class="p">)</span>
<span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">main=></span> <span class="p">(</span><span class="o">.</span><span class="nv">canRespond</span> <span class="p">(</span><span class="nf">implement-java-interface</span><span class="o">.</span><span class="nv">first-request-handler</span><span class="o">.</span><span class="p">)</span> <span class="nv">nil</span><span class="p">)</span>
<span class="nv">true</span></code></div>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Second, we can write a constructor method in the namespace,</span>
<br />
<br />
<div class="highlight"><code><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;"><code><span class="p">(</span><span class="nf">ns</span> <span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">first-request-handler</span>
<span class="p">(</span><span class="nf">:gen-class</span>
<span class="nv">:implements</span> <span class="p">[</span><span class="nv">HttpServer</span><span class="o">.</span><span class="nv">RequestHandler</span><span class="p">]))</span>
<span class="p">(</span><span class="k">defn </span><span class="nv">-canRespond</span> <span class="p">[</span><span class="nv">this</span> <span class="nv">request</span><span class="p">]</span>
<span class="nv">true</span><span class="p">)</span>
<span class="p">(</span><span class="k">defn </span><span class="nv">-getResponse</span> <span class="p">[</span><span class="nv">this</span> <span class="nv">request</span><span class="p">]</span>
<span class="nv">nil</span><span class="p">)</span>
<span class="p">(</span><span class="k">defn </span><span class="nv">new-first-request-handler</span> <span class="p">[]</span>
<span class="p">(</span><span class="nf">implement_java_interface</span><span class="o">.</span><span class="nv">first_request_handler</span><span class="o">.</span><span class="p">))</span>
<span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">main=></span> <span class="p">(</span><span class="nf">use</span> <span class="ss">'implement-java-interface</span><span class="o">.</span><span class="nv">first-request-handler</span><span class="p">)</span>
<span class="nv">nil</span>
<span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">main=></span> <span class="p">(</span><span class="o">.</span><span class="nv">canRespond</span> <span class="p">(</span><span class="nf">new-first-request-handler</span><span class="p">)</span> <span class="nv">nil</span><span class="p">)</span>
<span class="nv">true</span>
</code></span></code></div>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">This is the preferred method. It allows us to use more Clojure-like constructs to create an object, i.e. calling a method within a namespace as opposed to calling a constructor on a class.</span>
<br />
<br />
<h1><code>deftype/defrecord</code></h1>
<br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Using deftype or defrecord is certainly a little cleaner than <code>:gen-class</code> and it doesn't require AOT compilation. Here's what it looks like.</span><br />
<br />
<div class="highlight"><code><span class="p">(</span><span class="nf">ns</span> <span class="nv">implement-java-interface</span><span class="p">)</span>
<span class="p">(</span><span class="nf">deftype</span> <span class="nv">second-request-handler</span> <span class="p">[]</span>
<span class="nv">HttpServer</span><span class="o">.</span><span class="nv">RequestHandler</span>
<span class="p">(</span><span class="nf">canRespond</span> <span class="p">[</span><span class="nv">this</span> <span class="nv">request</span><span class="p">]</span> <span class="nv">true</span><span class="p">)</span>
<span class="p">(</span><span class="nf">getResponse</span> <span class="p">[</span><span class="nv">this</span> <span class="nv">request</span><span class="p">]</span> <span class="nv">nil</span><span class="p">))</span>
<span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">main=></span> <span class="p">(</span><span class="nf">require</span> <span class="ss">'implement-java-interface</span><span class="o">.</span><span class="nv">second-request-handler</span><span class="p">)</span>
<span class="nv">nil</span>
<span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">main=></span> <span class="p">(</span><span class="o">.</span><span class="nv">canRespond</span> <span class="p">(</span><span class="nf">implement_java_interface</span><span class="o">.</span><span class="nv">second-request-handler</span><span class="o">.</span><span class="p">)</span> <span class="nv">nil</span><span class="p">)</span>
<span class="nv">true</span></code></div>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Once again, our clean code is befuddled by the way Clojure resolves namespaces. Let's fix this by putting a method on the namespace.</span><br />
<br />
<div class="highlight"><code><span class="p">(</span><span class="nf">ns</span> <span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">second-request-handler</span><span class="p">)</span>
<span class="p">(</span><span class="nf">deftype</span> <span class="nv">second-request-handler</span> <span class="p">[]</span>
<span class="nv">HttpServer</span><span class="o">.</span><span class="nv">RequestHandler</span>
<span class="p">(</span><span class="nf">canRespond</span> <span class="p">[</span><span class="nv">this</span> <span class="nv">request</span><span class="p">]</span> <span class="nv">true</span><span class="p">)</span>
<span class="p">(</span><span class="nf">getResponse</span> <span class="p">[</span><span class="nv">this</span> <span class="nv">request</span><span class="p">]</span> <span class="nv">nil</span><span class="p">))</span>
<span class="p">(</span><span class="k">defn </span><span class="nv">new-second-request-handler</span> <span class="p">[]</span>
<span class="p">(</span><span class="nf">second-request-handler</span><span class="o">.</span><span class="p">))</span>
<span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">main=></span> <span class="p">(</span><span class="nf">use</span> <span class="ss">'implement-java-interface</span><span class="o">.</span><span class="nv">second-request-handler</span><span class="p">)</span>
<span class="nv">nil</span>
<span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">main=></span> <span class="p">(</span><span class="o">.</span><span class="nv">canRespond</span> <span class="p">(</span><span class="nf">new-second-request-handler</span><span class="p">)</span> <span class="nv">nil</span><span class="p">)</span>
<span class="nv">true</span></code></div>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Excellent! We were able to use a method on the namespace to construct the class for us, just like the first example. Plus, this method doesn't require AOT!</span><br />
<br />
<h1><code>reify/proxy</code></h1>
<br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Last, but not least, let's use the <code>reify</code> feature of Clojure to implement an interface.</span><br />
<br />
<div class="highlight"><code><span class="p">(</span><span class="nf">ns</span> <span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">third-request-handler</span><span class="p">)</span>
<span class="p">(</span><span class="k">defn </span><span class="nv">new-third-request-handler</span> <span class="p">[]</span>
<span class="p">(</span><span class="nf">reify</span>
<span class="nv">HttpServer</span><span class="o">.</span><span class="nv">RequestHandler</span>
<span class="p">(</span><span class="nf">canRespond</span> <span class="p">[</span><span class="nv">this</span> <span class="nv">request</span><span class="p">]</span> <span class="nv">true</span><span class="p">)</span>
<span class="p">(</span><span class="nf">getResponse</span> <span class="p">[</span><span class="nv">this</span> <span class="nv">request</span><span class="p">]</span> <span class="nv">nil</span><span class="p">)))</span>
<span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">main=></span> <span class="p">(</span><span class="nf">use</span> <span class="ss">'implement-java-interface</span><span class="o">.</span><span class="nv">third-request-handler</span><span class="p">)</span>
<span class="nv">nil</span>
<span class="nv">implement-java-interface</span><span class="o">.</span><span class="nv">main=></span> <span class="p">(</span><span class="o">.</span><span class="nv">canRespond</span> <span class="p">(</span><span class="nf">new-third-request-handler</span><span class="p">)</span> <span class="nv">nil</span><span class="p">)</span>
<span class="nv">true</span></code></div>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">As you can see, <code>reify</code> is the cleanest of all the examples and works like we want it too right away. </span><br />
<br />
<h1>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Conclusion</span></h1>
<br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">
Of the three, I would have to say that it's a close call between <code>deftype</code> and <code>reify</code>, but <code>reify</code> is my favorite. The two methods are very similar, however <code>deftype</code> is a little confusing with the namespace issues and constructing, but <code>reify</code> has the same benefit of not using AOT and is very succinct and clean as well. So, in the end, I will be using <code>reify</code>.</span>Myles Megyesihttp://www.blogger.com/profile/06006852692314796110noreply@blogger.com68th Light, Inc.42.2830786 -87.953130342.2360886 -88.0320943 42.330068600000004 -87.8741663tag:blogger.com,1999:blog-8721738246539687228.post-23173100232728249782011-12-07T19:49:00.001-06:002011-12-07T20:57:09.403-06:00Http Server (2)<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">After finishing the my iteration of the Http Server, here's a few things a I learned:</span><br /><br />
<h3>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">I kind of like explicitly typed languages</span></h3>
<br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">I can't explain it, but I am somehow my comfortable programming in explicitly typed languages. There is something very comforting about knowing exactly what you are working with at all times. I like not having to guess all the time. That's not to say that I don't like implicitly typed languages, but I don't have the same ease when working with them.</span><br /><br />
<h3>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Not all IDE's are evil</span></h3>
<br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">During the first half of college I dabbled with using Eclipse and Xcode. I didn't like either. Then I switched to using TextMate. Then I switched to Vim. Then I switched back to TextMate. Then I became a .Net developer. So, naturally I started using Visual Studio. I never liked it. Visual Studio does way to much for you. Some developers like it, but, I think I'll pass on the 3-5 minute start up time. Right now, I'm at an awkward spot where I'm not in love with any editor or IDE. So, when I came to 8th Light and saw that lots of people were using IntelliJ, I wasn't too excited about it. However, at the strong encouragement of Micah, I decided to use it for my server. I must say, IntelliJ is a happy medium. I doesn't do too much for you, but it doesn't enough to save you a lot of hassle. This probably won't be the last time I use it.</span><br /><br />
<h3><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Java Logging sucks</span></h3><br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">After messing around with the Java Logger forever, I decided to ditch it. It's just not worth it. Sure, it's nice if you need to write to the console and file with one interface, but it's not easy to use. In my opinion, it was poorly designed. Root loggers, parent loggers, default handlers and singletons are way to much complication for something that is just supposed to write informational messages for me. I'm considering writing my own Logger for Java. But, in the mean time, I'm just going to use print lines.</span>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-53612459224832642502011-12-01T22:59:00.001-06:002011-12-01T23:54:46.186-06:00Http Server (1)<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">As part of my apprenticeship, I have been working on building an Http Server this week. I'm writing the server in Java, which is a nice language to work with, albeit very verbose. Here's a few things I have learned while writing the server.</span><br />
<br />
<h3>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Factory Pattern and Dependency Injection</span></h3>
<br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">While writing my listener, I needed to instantiate a new handler upon receiving a request. Originally, I would simply insatiate a new handler object and spawn it up in a new thread to handle the given request. This was working fine until my tests started to get too large. Anytime I tested the listener, I was essentially testing the entire system. While testing I had no control over what happened inside the listener. So, in order to make my tests more focused, I decided to implement a handler factory. Now, I simply pass in a factory to my listener and the listener calls the factory when it needs to instantiate a new handler. This is great because I can now pass in a mock factory that returns mock handlers. This allows me to test just the listener and not the handlers at the same time. </span><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Along the same reason as using the factory pattern, I have found the Dependency Injection has made my test 100% more testable.</span><br />
<br />
<h3>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Hand Rolled Mocks</span>
</h3>
<br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">As per the requirement of the task, I was not allowed to use any outside libraries other than JUnit and the Java Standard Library. Even so, I have found it very helpful to not have a mocking framework to mess around with. I've found that hand rolling mocks is significantly simpler than rolling mocks via a framework. While they may not be as powerful out of the box, they take a lot less time to set up and are easy to add whatever functionality you need. The only problem I have had with hand rolled mocks is keeping their function signatures in sync with their parent. However, I suppose if I had used the proper refactoring tools of IntelliJ it wouldn't have been a problem.</span>
<br />
<br />
<h3>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Hang Ups</span></h3>
<br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">My biggest hang ups so far have been Java related, Logging and Shutdown events. Logging in Java is uncharacteristically complex. You are unable to instantiate a logger and use it. Loggers are singletons, and they are assigned by name. So to get a logger you have to call the static method </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">getLogger(String name)</span><span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;"> with the name of the Logger you would like to retrieve. In addition each logger can have multiple handlers, which allows writing logs to any number of places (console, file, memory, etc.). This is nice, but I was not aware that there is a default logger that writes to the console. So when I attempted to add another handler that wrote the console, I was receiving lots of output. So, in the future, I will be sure to either disable the default handler or use it appropriately.</span><br />
<br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Shutdown events were another headache. In Java, you cannot simply intercept a SIGINT as with other languages. I needed to intercept the SIGINT in order to shutdown the server properly before exit. The only was to do this in Java is to add a Shutdown event handler. So, I added a handler and some logging statements within the shutdown handler to notify me if it was running. However, the shutdown handler was very sporadic, sometimes it would run and sometimes it would not. However, after a lot of debugging, I discover that the handler was being run every time but only the log messages where not being run every time. This is because every shutdown event handler runs concurrently. In another thread, the logger was being closed. So, sometimes I would log a message before it would close and sometimes it would close first. </span><br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;"><br /></span><br />
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">All in all, programming the server in Java has been fun, albeit a challenge at some points.</span>Unknownnoreply@blogger.com0Home41.89503 -87.69226541.8935525 -87.6947325 41.8965075 -87.689797500000012tag:blogger.com,1999:blog-8721738246539687228.post-22023820922154801072011-11-23T15:15:00.000-06:002011-11-23T16:57:45.554-06:00Bowling Kata<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">I've been working on a lot of Katas past few days. Some of them have been trivial and some more challenging. Generally, before I start on a kata I will watch a kata cast before trying it. This gives me a good idea of where I need to go before starting. However, it doesn't really simulate a real life situation very well. In real life you are given a problem and you have to solve it without any clue what the solution might be. So, before I started the bowling kata I decided not to watch the kata cast or look at any previous solutions. It took me a lot longer to come up with the solution than I thought it would. It was significantly harder to write the algorithm with out knowing the solution ahead of time. However, I think it was very valuable to approach the problem without any preconceptions. My algorithm was very messy. I took in an array of rolls and looped over them to sum up the score. The algorithm was really long with lots of duplicaiton. So I decided to make the problem smaller. I refactored so that I was taking in an array of rolls and returning an array of frames. This refactoring allowed me to stop looping once I have reached the maximum amount of frames, 10. It is also a lot easier to test. I can test special frames (strikes, spares, and the 10th frame) without passing in an entire game. So, through this kata I learn a valuable lesson of continuing to break the problem into smaller chucks. Here's the final product.</span>
<br />
<br />
<script src="https://gist.github.com/1390092.js"></script>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-18956182198476766932011-11-22T10:41:00.001-06:002011-11-22T10:45:24.945-06:00Prime Factors in Clojure<script src="https://gist.github.com/1386142.js">
</script>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8721738246539687228.post-74321945969085881782011-11-22T09:09:00.001-06:002011-11-22T09:10:00.072-06:00SCNA<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', serif;">Last weekend I attended the Software Craftsmanship of North America Conference. Overall, it was a really sweet experience. Having never attended a software conference before, I was pleasantly surprised by how much fun it was. One of my favorite things was the community there. There is a very close camaraderie, even among competing companies. Everyone was united by the common goal to become better Craftsman. This also means that there was an atmosphere of learning. Everyone wanted to learn more about everything. My favorite talk was by Carl Erickson, which, strangely enough, was not specifically about programming, but about what makes a conducive environment for Craftsmen to work. I was very intrigued by the wisdom he shared about open financial policies, open work spaces, collaboration and so forth. My least favorite talk was by Zed Shaw. He was relentlessly trying to tell us that we had lost our way and forgotten about just programming. I didn't understand his points because the Software Craftsmanship movement is about being a better programmer. Regardless, it was still enjoyable to have my ideas challenged. All in all, my first conference was really fun and really exhausting. By the end of the weekend I didn't even want to think anymore. This can probably be compared to an athlete being tired after a workout. If you're not tired, you probably didn't work to hard. So, I'm glad that I learned a lot at the conference, but I was glad to rest afterwards as well.</span>Unknownnoreply@blogger.com0