Wednesday, November 14, 2012

4 Rules of Simple Design

  • Pass all tests
  • Clear, Expressive and Consistent
  • Duplicates no behavior or configuration
  • Minimal methods, class and modules
By Kent Beck

Thursday, November 8, 2012

Keep adding types use Polymorphism, keep adding functionalities use Struct

If we set minimize the changes as a goal, object-oriented programming people tend to agree that conditionals are bad smell, we should consider refactor them to types and use Polymorphism mechanism instead so that when we have a new type of data we can have a small change just to create a new types. But there is a good point in the early part of this podcast Something Else Was Smellier stated that if we are working on the problem that number of types are likely to be stable but what always changes are the behavior, we should consider keep behaviors separate(Separate function) from those types and leave the types as plain data structures(Struct).

For Polymorphism, if we are adding a new functionality to a class hierarchy we have to add it to every class in that hierarchy otherwise you will get no method error. In the opposite, if you just create a separate function to hold that new functionality while you know that you are not likely to have a new data type, you can just handle those types with case statement which each case response to each type. You can also simply ignore the data types that you don't interested in for that new functionality by fall it back to default case. If you have an another new functionality, just add a new function. If you follow this direction, all changes you have to add will rest in one place, you don't have to modify multiple places(types) as in Polymorphism approach, which leads to the goal of minimize the changes.

More visual example I can give is cards for card game. For example, you try to transform 52-cards deck(Type/Struct) to magic game card deck. You want to add attacking power(behavior) to those cards(Polymorphism), you have to draw value of attacking power on to every cards, which is quite a lot of work. Instead, you can draw a separate attacking power mapping table in a paper(Separate function) then you don't have to modify those cards. You changes will always in the table. And if you want to add defensive power you can have another mapping table. You can always have a new mapping table for every new functionality without touching those cards. The pain will occur when you need to add a new card(Type) because you have to go and modify every tables. If you always have a new card(Type), don't use a mapping table approach step back and go for modifying the card approach (Polymorphism).

One downside of emphasizing the struct is that you will have a hard dependency on that struct and you have to adapt the functionality to be match with that structure. When the struct get modified, your functionalities that rely on that struct are likely to change. For example the same 52-cards deck(Struct), which is a stable type never change structure, you can apply as many kind of games(behavior) you want on it easily but your game have to be sufficient of using only these 52 cards. When one of those 52 cards is modified, your games are screwed up.

Tuesday, October 16, 2012

Modern product development motto

Don't launch if you aren't going to monitor
Don't monitor if you aren't going to improve
Don't improve if you don't know who to improve for

Sunday, October 14, 2012

Weird Clojure build-in function names

Clojure has a rich set of build-in functions for manipulating data structures. Library developers have to name each of them as concise as possible to make them convenient to use. Selecting a word that can describe the whole behavior of each function is a very important step and they did this very well.

But for non-native English speaker that coming from Java and Ruby world, I found that some of the
function names are hard to guess and remember what they do. These are an example list of them.
  • quot
  • assoc
  • dissoc
  • interleave
  • interpose
  • juxt
  • reductions
  • trampoline

Btw, I have a feeling that I remember them now after wrote this blog. :)

[VIM] + and * register don't work with clipboard on MacOSX

This problem is because VIM version that shipped with MacOSX doesn't enable xterm_clipboard. You can check this by run :version command in VIM. Enabled xterm_clipboard VIM must have +xterm_clipboard but the version shipped with MacOSX has -xterm_clipboard.
If you want it to be enabled, you need to compile VIM yourself with clipboard enabling flag.

Tuesday, October 9, 2012

Clojure Map function

I came across one of the quiz from 4clojure, it asks us to reimplement interleave.
=> (interleave [:a :b :c] [1 2 3])
(:a 1 :b 2 :c 3)
After I solve this quiz with a long solution, I looked at the other people solution. One of the solution that impressed me somewhat similar to this
=> (apply concat (map vector [:a :b :c] [1 2 3]))
(:a 1 :b 2 :c 3)
If we run only evaluate Map part, the result looks like this
=> (map vector [:a :b :c] [1 2 3])
([:a 1] [:b 2] [:c 3])
I had hard time to understand that. I got a question in my head. "Of what reason, why Map function take element of 2 vector (3th, 4th parameters) one by one?" It turns out that this is a Clojure Map function behavior.

Then I realized that the reason why I don't easily get that because all functional programming style syntaxes that I have played with, Map functional only takes 1 argument.

Look at Ruby code.
=> [1, 2, 3].map { |i| i + 1 }
[2, 3, 4]  
But for Clojure's Map function, it takes variable arguments and behave like what I explained earlier.
=> (map inc [1 2 3])
(2 3 4) 
=> (map + [1 2 3] [4 5 6])
(5 7 9)
Another question I had was "Can we archive the same variable arguments behavior with Object-Oriented style?"
The answer is No because we can only calling a method to 1 object. But of course, you can archive this with OO language but in functional way like this.
map(->x,y{ x+y }, [1,2,3], [4,5,6])
This examples might look trivial but if we look at it carefully it represents fundamental differences between OO languages and functional languages. For OO language, our method call is a mechanism of passing message to an object. But for functional language, it's just a function call.

Saturday, September 8, 2012

Experimenting Java library with Clojure

I'm reading Joy of Clojure. The book is a bit outdated in terms of library version. It was written when Clojure was at 1.2 but now Clojure is at 1.4 and 1.5 is development.

In one section of the book, it explains that we can easily use function show in clojure.contrib.repl.utils to display document of Java library but that was for Clojure.

According to Where Did Clojure.Contrib Go, show moved to clojure.reflect/reflect. Let play with it a little bit.

Pretty handy.

Note: you also can open Javadoc in browser with this snippet
(javadoc java.lang.Math)

Saturday, July 14, 2012

Never ending function

Today, I had a Clojure quiz for myself. How to write a function that print out a number and return a function that will print out an incremented number and return out a function with the same behavior again and again, never end.

For those who are familiar with Clojure might find it easy but for me it took a while and I couldn't solve it so I decided to start solving it with Ruby first.

This is my solution.
After that it was way more easier to port it to Clojure.

Thursday, July 5, 2012

I get paid for code that works, not for tests ... - Kent Beck

This quote by Kent Beck is just popping up all around articles I have read these days. Want to keep it  here.
I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence (I suspect this level of confidence is high compared to industry standards, but that could just be hubris). If I don't typically make a kind of mistake (like setting the wrong variables in a constructor), I don't test for it

Tuesday, June 26, 2012

Curry in Ruby

While I was wandering around Proc and Lambda in Ruby, I found very useful method Proc#curry. This method answer the question that bugs me for a while, is there an easy way to make Curry happen in Ruby? Let look at the example how to use it.

Start with a simple method: Manually currying: But with Proc#curry, you can heavily simplify your code like this: Pretty neat!

Sunday, June 3, 2012

Benefits of TDD

I just read a blog post about TDD and really like it, TDD for Those Who Don't Need It. It reminds me to my first impression when I start practicing TDD more than a year ago, it allows us to not have to to hold everything in our head.

By the way, that post also mentions to other TDD benefits. I want to note it down here just for a reference.
  • Not have to hold everything on our head. Just follow errors.
  • Confidence to refactor
  • Better design
  • Documentation
  • Regression

Monday, May 28, 2012

Mac OS X Terminal shortcut keys

Terminal is one of the most frequently using application everyday but I have never had a chance to master its short keys. Today is a good day to start.

As I know, Terminal's short keys come from Emac, when I refer to meta short keys it means that you can use esc button or setting option(alt) button as a meta key. (Settings > Keyboard > Use option as meta key)

  • Ctrl + a = Go to the beginning of the line
  • Ctrl + e = Go to the end of the line
  • Ctrl + l or Command + k = Clear screen
  • Ctrl + r = Search for previous commands (pressing more r to get earlier commands)
  • Ctrl + w = Delete the word before cursor
  • Meta + d = Delete the word after the cursor
  • Ctrl + u = Clear the line before cursor
  • Ctrl + k = Clear the line after cursor
  • Meta + t = Swap last 2 words before cursor
  • Ctrl + d = Delete a character after cursor
  • Meta + f = Move cursor to the right one word
  • Meta + b = Move cursor to the left one word

Monday, May 14, 2012

Things I Didn't Know Rails Could Do

Something I thing it's easy and useful to me from James Edward Gray II presentation in RailsConf 2012
Ten Things You Didn't Know Rails Could Do

  • rails c --sandbox
  • rake db:migrate:status
  • pluck
    • User.pluck(:email)
    • User.uniq.pluck(:email)
  • Count Record in Group
  • File.atomic_write
  • { nested: { a: 1 } }.deep_merge(nested: { b: 2 })
  • hash.except(:key1, :key2)
  • hash1.reverse_merge(hash2) # keep hash1 value if hash2 has a duplicate key
  • content_tag_for(:tag, array) { |e| # content_inside }
  • render partial: @active_record_model & to_partial_path
  • grouped_options_for_select

Saturday, March 3, 2012

Personal (and non-popular) Rails development rules of thumb

  • View first, then Controller and Model last.
  • In Controller tests:
    • Don't mock ActiveRecord method
    • Do stub ActiveRecord method (No real SQL execution allowed)
    • Do mock custom model method
    • Use signature from model mock to create real method in model (or module)
    • Should not contain more than 5 lines or 10 function calls
  • In Model tests:
    • Do real SQL execution (No mock, no stub)

Clojure how to get JVM options from inside application

(.getInputArguments ( Credit:  How to get vm arguments from inside of java applic...