tag:blogger.com,1999:blog-83178838424268318552024-03-13T10:42:30.835+05:30Experiments with DesignUnknownnoreply@blogger.comBlogger4125tag:blogger.com,1999:blog-8317883842426831855.post-22199065573324138352013-08-14T09:41:00.002+05:302013-08-14T09:41:42.181+05:30Sinatra - as a newbie.<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">I've been using <a href="http://sinatrarb.com/" target="_blank">Sinatra</a> for a couple of Ruby projects lately and have found it to be easy to pick up and get running quickly. </span><div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">I've tried to answer some questions to help newbies like me in this <a href="http://rubylearning.com/blog/2013/08/14/sinatra-some-questions-from-ruby-newbies/" target="_blank">post</a> by <a href="https://plus.google.com/107809992818057105754" target="_blank">Satish Talim</a>. Hope it helps.</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Will try to share some more inputs on my experiments with Sinatra in the days to come.</span></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8317883842426831855.post-64372116162466552572013-03-15T15:11:00.000+05:302013-03-15T15:14:01.808+05:30State pattern : Java and Ruby comparison<span style="font-family: Trebuchet MS, sans-serif;">I've been working with Java as a programming language for long now and just started learning Ruby a few weeks back. Here I present a comparison between the two while trying to implement a basic State pattern. The reason I compare these examples is that they highlight the difference in the way these languages enable <i>Design by contract</i>.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">In both examples clients of the </span><span style="font-family: Courier New, Courier, monospace;">StateExample</span><span style="font-family: Trebuchet MS, sans-serif;"> class call its </span><span style="font-family: Courier New, Courier, monospace;">transition()</span><span style="font-family: Trebuchet MS, sans-serif;"> method with a </span><span style="font-family: Courier New, Courier, monospace;">criteria </span><span style="font-family: Trebuchet MS, sans-serif;">parameter to change it's state. </span><br />
<br />
<h4>
<span style="font-family: Trebuchet MS, sans-serif;">Java implementation</span></h4>
<span style="font-family: Trebuchet MS, sans-serif;">The </span><span style="font-family: Courier New, Courier, monospace;">StateExample</span><span style="font-family: Trebuchet MS, sans-serif;"> class has a reference to an implementation of the </span><span style="font-family: Courier New, Courier, monospace;">IState</span><span style="font-family: Trebuchet MS, sans-serif;"> interface. The </span><span style="font-family: Courier New, Courier, monospace;">transition()</span><span style="font-family: Trebuchet MS, sans-serif;"> method delegates itself to the </span><span style="font-family: Courier New, Courier, monospace;">evaluate()</span><span style="font-family: Trebuchet MS, sans-serif;"> method of the </span><span style="font-family: Courier New, Courier, monospace;">IState</span><span style="font-family: Trebuchet MS, sans-serif;"> implementation which evaluates the criteria and returns either itself or a new </span><span style="font-family: Courier New, Courier, monospace;">IState</span><span style="font-family: Trebuchet MS, sans-serif;"> implementation.</span>
<span style="font-family: Trebuchet MS, sans-serif;"><script src="https://gist.github.com/sudarshanshubakar/5168465.js"></script></span><span style="font-family: 'Trebuchet MS', sans-serif;">Basically this allows the Implementation of the State to be abstracted to the interface </span><span style="font-family: Courier New, Courier, monospace;">IState</span><span style="font-family: 'Trebuchet MS', sans-serif;"> and the </span><span style="font-family: Courier New, Courier, monospace;">StateExample</span><span style="font-family: 'Trebuchet MS', sans-serif;"> class works on the contract specified by </span><span style="font-family: Courier New, Courier, monospace;">IState</span><span style="font-family: 'Trebuchet MS', sans-serif;">.</span><br />
<br />
<h4>
<span style="font-family: 'Trebuchet MS', sans-serif;">Ruby Implementation</span></h4>
<span style="font-family: 'Trebuchet MS', sans-serif;">Let's now look at the Ruby implementation of the same example. The </span><span style="font-family: Courier New, Courier, monospace;">StateExample</span><span style="font-family: 'Trebuchet MS', sans-serif;"> class looks almost similar to it's Java counterpart. What's noticeable here is the absence of the equivalent of interface </span><span style="font-family: Courier New, Courier, monospace;">IState</span><span style="font-family: 'Trebuchet MS', sans-serif;">. </span><br />
<script src="https://gist.github.com/sudarshanshubakar/5168472.js"></script>
<span style="font-family: 'Trebuchet MS', sans-serif;"><a href="http://rubylearning.com/satishtalim/duck_typing.html" target="_blank">Duck typing</a> in Ruby allows this. The object referenced by the </span><span style="font-family: Courier New, Courier, monospace;">current_state</span><span style="font-family: 'Trebuchet MS', sans-serif;"> variable only needs to contain an </span><span style="font-family: Courier New, Courier, monospace;">evaluate()</span><span style="font-family: 'Trebuchet MS', sans-serif;"> method. The contract is therefore on the behaviour of the value of </span><span style="font-family: Courier New, Courier, monospace;">current_state </span><span style="font-family: 'Trebuchet MS', sans-serif;">rather than it's type.</span><br />
<br />
<h4>
<span style="font-family: 'Trebuchet MS', sans-serif;">Conclusion</span></h4>
<span style="font-family: 'Trebuchet MS', sans-serif;">Design by contract seems to be subtler and more implied in Ruby as compared to being typed in Java. For someone coming from Java, programming in a dynamically typed language like Ruby separates the concepts of Object oriented programming and Java language semantics. I can't help imagining Morpheus asking me to "Free your mind" :).</span>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8317883842426831855.post-49367971417123217342013-03-11T16:36:00.003+05:302013-03-15T06:29:19.062+05:30Test Driven (Design &) Development <span style="font-family: Trebuchet MS, sans-serif;">I have been following Test Driven Development (TDD) for a few months now. </span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">Some of the direct benefits that I've seen are:</span><br />
<br />
<ul>
<li><span style="font-family: Trebuchet MS, sans-serif;">Highly improved productivity due to minimal & accurate development.</span></li>
<li><span style="font-family: Trebuchet MS, sans-serif;">Highly improved code quality as I can refactor without fear of breaking something.</span></li>
<li><span style="font-family: Trebuchet MS, sans-serif;">A mental pat on my back whenever my tests are green-lighted :). i.e it is fun.</span></li>
</ul>
<div>
<span style="font-family: Trebuchet MS, sans-serif;">I have been trying to spread this practice at the workplace for the past few months with little success. My "sales pitch" typically revolves around the points mentioned above and I've had little success.</span></div>
<div>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Trebuchet MS, sans-serif;">As I use TDD more and more, what I have come to realize is that not only has my code become cleaner now, my designs are more robust and flexible. I think to a large extent this is due to the fact that TDD tends to negate the effects of design prejudices.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">Here is another <a href="http://blog.iterate.no/2012/10/30/do-you-know-why-you-are-testing-on-the-principles-underlying-tdd/" target="_blank">blog</a> that echoes some of the points mentioned here.</span></div>
<div>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Trebuchet MS, sans-serif;">I am currently reading <a href="http://www.amazon.com/Test-Driven-Development-Kent-Beck/dp/0321146530" target="_blank">Test Driven Development : By Example</a> by Kent Beck . Will publish a review once done reading. Something that has really taught me TDD has been the <a href="http://www.objectmentor.com/resources/articles/xpepisode.htm" target="_blank">XP episode </a></span>by <span style="font-family: Trebuchet MS, sans-serif;">Robert C. Martin and Robert S. Koss.</span></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8317883842426831855.post-85071692828564064992013-01-23T17:56:00.002+05:302013-01-29T15:28:58.932+05:30Single Responsibility Principle<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://lostechies.com/derickbailey/files/2011/03/SingleResponsibilityPrinciple2_71060858.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="256" src="http://lostechies.com/derickbailey/files/2011/03/SingleResponsibilityPrinciple2_71060858.jpg" width="320" /></a></div>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">Every once in a while one comes across a concept that he always knew was right but did not know how to apply. For me the concept of writing methods/functions having one(and only one) purpose was one such thing.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<h2>
<span style="font-family: Verdana, sans-serif; font-size: small;">What is Single Responsibility Principle (SRP)?</span></h2>
<span style="font-family: Verdana, sans-serif;">Functions should do one thing. They should do it well. They should do it only. - is how Robert Martin describes this concept in his book <a href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882" rel="nofollow" target="_blank">Clean Code</a><a href="http://www.blogger.com/blogger.g?blogID=8317883842426831855#1"><sup>1</sup></a>(which I would highly recommend). The issue with this has been to understand: what is one thing? Methods are written to do one operation after all so how do you determine the "one thing" that needs to go into the method. A quick review of any long method is typically enough to figure out what parts of code can be extracted into another method. How much to extract and where to stop is something that I've struggled with till recently. Let me try to explain these concepts using some material from <a href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882" rel="nofollow" target="_blank">Clean Code</a> and some of my experiments.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<h2>
<span style="font-family: Verdana, sans-serif; font-size: small;">How to determine whether a method is having single responsibility?</span></h2>
<span style="font-family: Verdana, sans-serif;">Sometimes the name of the method itself reveals that there are multiple responsibilities with the method. Does the method reads like an algorithm is what I like to check. Preferably like an algorithm that is simple to understand in one reading for someone who does not understand too much of programming. </span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">Robert Martin in his book <a href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882">Clean Code</a> describes this as "In order to make sure our functions are doing “one thing,” we need to make sure that the statements within our function are all at the same level of abstraction." </span><span style="font-family: Verdana, sans-serif;">Now what does this mean? </span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">Basically it means that if your method's responsibility is to make a cup of coffee, it should just make coffee. It shouldn't be concerned with the act of visiting the grocery store to buy the Nescafe, sugar, etc. These related activities can be abstracted to respective methods/objects. This helps focus on the act of making the coffee right, irrespective of what happened while buying sugar. The same concept in turn would apply to the act of buying groceries and so on.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">Here's an example of re-factoring code to have a single level of abstraction.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;"><script src="https://gist.github.com/4604553.js"></script></span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">The above code tries to remove a certain quantity of an item from a Shopping Cart by iterating through an internal data structure. It is pretty clear from this code that the level of detail spans at least 3 levels of abstraction from the intent expressed by the method name.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">Here's the same method after a first pass of refactoring.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;"><script src="https://gist.github.com/4604670.js"></script></span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">We can see from the code above that the task of finding the reference of the item in the Shopping cart has been abstracted to another method. There is still however some code that decides how to remove the item from the cart that can further be exracted.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;"><script src="https://gist.github.com/4604685.js"></script></span><br />
<span style="font-family: Verdana, sans-serif;"><script src="https://gist.github.com/4604703.js"></script></span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">This version of the code reads like an algorithm which goes:</span><br />
<br />
<ol>
<li><span style="font-family: Verdana, sans-serif;">Get a hold of the item that needs to be removed.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Remove it appropriately.</span></li>
</ol>
<br />
<span style="font-family: Verdana, sans-serif;">As can be seen, the extracted methods themselves go one level of abstraction below the intent stated by it's name. Also a noticeable side effect is that this led to the discovery of the </span><span style="font-family: Courier New, Courier, monospace;">ShoppingBasketItem </span><span style="font-family: Verdana, sans-serif;">object.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<h2>
<span style="font-family: Verdana, sans-serif;"><span style="font-size: small;">How much to extract?</span></span></h2>
<span style="font-family: Verdana, sans-serif;">You will know when to stop extracting when the code in an extracted method almost exactly matches the name of the method.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<h2>
<span style="font-family: Verdana, sans-serif; font-size: small;">Benefit of SRP.</span></h2>
<span style="font-family: Verdana, sans-serif;">One obvious benefit of SRP is enhanced readability. Other benefits of SRP is that one may discover new Objects while extracting code into Methods.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<h2>
<span style="font-family: Verdana, sans-serif; font-size: small;">How to refactor?</span></h2>
<span style="font-family: Verdana, sans-serif;">Martin Fowler in his book <a href="http://martinfowler.com/books/refactoring.html" rel="nofollow" target="_blank">Refactoring</a> stresses on the point of having a solid set of unit tests before refactoring code. </span><br />
<span style="font-family: Verdana, sans-serif;">A typical refactoring should include:</span><br />
<br />
<ul>
<li><span style="font-family: Verdana, sans-serif;">Create a set of test cases to test the functionality of the method being refactored.<a href="http://www.blogger.com/blogger.g?blogID=8317883842426831855#2"><sup>2</sup></a> </span></li>
<li><span style="font-family: Verdana, sans-serif;">Iterate through refactoring and testing (using unit tests) till you reach the required level of abstraction for each method.</span></li>
</ul>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<h2>
<span style="font-family: Verdana, sans-serif;"><span style="font-size: small;">Conclusion</span></span></h2>
<span style="font-family: Verdana, sans-serif;">Single Responsibility Principle is a great guideline to make code readable. It will lead to an increase in the number of methods in the class or number of classes. The code will however express a clear and crisp flow of logic within and among the units.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">Excerpts from <a href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882" rel="nofollow" target="_blank">Clean Code</a> - Robert Martin and <a href="http://martinfowler.com/books/refactoring.html" rel="nofollow" target="_blank">Refactoring</a> - Martin Fowler. Code snippets and example are my own.</span>
<br />
<br />
<br />
<a href="http://www.blogger.com/blogger.g?blogID=8317883842426831855" name="1">1. A highly recommended book.</a><br />
<a href="http://www.blogger.com/blogger.g?blogID=8317883842426831855" name="2">2. The above refactoring exercise was backed by a set of unit tests which validated the functionality of the method.</a>Unknownnoreply@blogger.com1