| Games | |
|---|---|
| Connect6 | (play) |
| Pente | (play) |
| Keryo-Pente | (play) |
| Phutball | (play) |
| Othello | (play) |
| Kalah | (play) |
| Oware | (play) |
| Footsteps | (play) |
| Articles by Tag | |
|---|---|
| AI | (3) |
| Announce | (21) |
| Ruby | (2) |
| Software Development | (3) |
| Technology | (9) |
| Vying Games | (31) |
Ruby and Trust |
|
|
May 25, 2007 at 04:00PM
In Ruby, Software Development |
By
eki
1 Comments |
Few things help an individual more than to place responsibility upon him, and to let him know that you trust him. -- Booker T. Washington
Why I Love RubyWhen I got up this morning I felt particularly inspired to write an article -- no, an ode -- to the Ruby programming language. I think this is what happens when a programmer finds a language that he or she feels really in tune with. To stretch the analogy, programmers are all tuned to different frequencies, and, if they're lucky, they eventually find that one language with which they really resonate with. Which is probably why there are so many "Language blank is the Best Language Ever!" articles strewn about the internet. Anyway, I sat down this morning and started making a list of all the reasons I love Ruby. I didn't get too far before I realized that the full list would be too much for one article, so I've decided instead to focus on just one element of my Ruby love affair: Trust. Ruby trusts programmers. Open ClassesAs an example of that trust, Ruby has open classes. Any developer can open any class and modify it in whatever way he or she feels like. This can mean adding new functionality, redefining existing methods, or even removing functionality (think sandboxes). Let's say I'm developing board game software. I'll be dealing with board coordinates quite a lot. One natural solution would be to create a
This
There are two ways we want to use a
Don't worry too much about the implementation details, what I really wanted to point out is an alternative:
This code allows us to use Ruby's symbols as
Ruby allows us to open it's There are, of course, negatives to open classes. What if some library I depend on decides to add an So, when should you take advantage of open classes? Really it's up to you. Maybe you're a more conservative programmer and only see benefit in making temporary bug fixes to third party libraries. Or, maybe you're a huge fan of Ruby Facets. Either way, Ruby allows you to draw your own line in the sand. Not every programming language will trust you to modify its core classes. I suspect more OO languages have closed classes than open classes. Some, like Java, take it a step further: The Java Duck TypingWhen defining a method, it's considered idiomatic in Ruby not to check the class of parameters. For example, the following is not encouraged:
With the above code,
Of course, we could just chill out and write it like this:
In which case, garbage-in would cause an error like this:
I gave By relaxing, and not worrying about type checking parameters, I'm trusting future programmers to pass valid coordinate-like objects. This is duck typing: if something walks like a duck, and quacks like a duck, why should I care whether or not it really is a duck? Ruby's dynamic typing and duck typing philosophy allow programmer creativity and inventiveness. For example, creating proxies and mock objects in a duck typing environment is a breeze. None of this would be possible without trust. HappinessRuby's creator, Yukihiro Matsumoto (Matz), has said many times that one of the biggest objectives of the Ruby programming language is programmer happiness. I think a lot of happiness is achieved through Ruby's expressive syntax. For many, Ruby code is easy on the eyes, and there's a certain joy in writing elegant code. But for me, happiness comes from freedom. There is nothing more frustrating than working around restrictions inherit in your programming language. Sometimes the restrictions are tolerable because they're a part of a trade off. Erlang, for example, places restrictions on its programmers, but those are for the sake of providing a high degree of parallelization and fault tolerance. But some languages, like Java, are restrictive due to a lack of trust. Sometimes I wonder if the Java motto isn't: Remove all the sharp tools from the toolbox before some programmer pokes his eye out. I prefer my toolbox fully stocked. I prefer a relationship built on trust. And, that's why I love Ruby. |
Nicolás Sanguinetti says,
Hear hear! :) Agree 100% with what you say.
It's a pity most programmers don't want to be trusted by their languages and prefer to bloat their code "just because" it's the right thing to do.