Programmer Intent or What you're not getting about Ruby and why it's the tits
A user named yesthatmcgurk left a comment on DotNetKicks where he/she said:
I must be a complete loser, because I can't see where Ruby is such hot shit. I'd love to read a story, "What you're not getting about Ruby and why its the tits."
Such a great comment that I had to get involved. One of the other commenters pointed to a post over on "Softies on Rails" that's really worth reading.
Note: Forgive the use of "the tits" in this context. "Slang Definition: A description of something you show great liking to, or greatly appreciate..." Usually not a work-friendly phrase, but perhaps pub-appropriate.
There's a simple snippet of Ruby code:
def shutter_clicked
if @camera.off? || @camera.memory_card_full?
return
end
capture_image
end
Ruby folks have their own aesthetic and sense of beauty. They would say that the Programmer's Intent is better expressed like this:
def shutter_clicked
capture_image if @camera.on? && @camera.memory_available?
end
These two functions identically express the Programmer's Intent and the second one expresses it better, many believe. This one simple example is subtle to some, beautiful to others. TunnelRat says:
What is this obssesion[sic] with "expressiveness"? Go write poertry [sic] if you want to be expressvive.[sic]
Remember that ultimately our jobs are (usually) to solve some kind of business problem. We're aiming for a finish line, a goal. The programmer's job is translate the language of the business person to the language of the computer.
The whole point of compilers, interpreters, layers of abstraction and what-not are to shorten the semantic distance between our intent and the way the computer thinks of things.
Reginald "raganwald" Braithewaite links to the blog Agile Renaissance that absolutely nails it for me (emphasis mine):
If you survey the over 5000 different languages and dialects that humans speak, you find that there is no universal set of equivalent semantics between them. This fact implies that there can never be a computer language which will always have the shortest semantic distance between itself and any solution. Therefore there never will be a universally best programming language.
I'm just learning Ruby, personally. Like anyone making their way in a new language, be it a programming language or a spoken language, I can make myself understood, but not effectively. I'm certainly not writing poetry, nor am I able to "mince words" in Ruby.
You'll find lots of "smackdowns" on the .NET between different languages. This post isn't a smackdown post. Sure, if your language of choice doesn't have a particular function, it could likely be added.
There are some fun one-liner comparisons though and some folks think that paying a:
Java:
new Date(new Date().getTime() - 20 * 60 * 1000)
Ruby:
20.minutes.ago
In this example, the elegance is a combination of how Ruby works, and a Rails library called ActiveSupport that is a Domain Specific Language that extends Ruby. There's a special satisfaction when you read a well-written novel and you go over a turn of phrase and think, "wow, what a great way to express that. That was a perfect way to describe ____," and there's no ambiguity.
While programming, unless you have 100% code coverage via Tests, there's ambiguity. There's a lack of clarity in expressing what you intended vs. what you might get.
Cyclomatic complexity is just one of many software metrics that can help you understand what your code "says" it's going to do. Remember, your code always runs exactly as you wrote it.
Cyclomatic complexity may be considered a broad measure of soundness and confidence for a program. Introduced by Thomas McCabe in 1976, it measures the number of linearly-independent paths through a program module.[Carnegie Mellon SEI]
The most important word there, to me, is confidence. Can you be confident that your code is written to be express what you intended? If you have full coverage, there's a better chance you understand what it can and will do (although achieving full coverage guarantees nothing, but that's another post). If not, it often helps to have a language that makes expressing your intent very clear, concise, and above all, unambiguous. Unambiguous expression of intent gives you (and your customer) confidence that things will happen as expected. There are some things easier expressed in Zulu than in English. Folks who run Ubuntu should know that.
When programming (that is, expressing your intent to the computer) you should select a language that matches up with the program you're trying to solve. Every language is, in a way, a Domain Specific Language.
Regardless of what your language of choice is, you might be someone who says all languages eventually become, or try to become Lisp, or you might think Visual Basic is the best or that PHP is God's Language, you should learn a new language every year. I code, and have coded, for many years in C#, and before that C++, but it's important for me in my personal development to remember why I learned Haskell and Lisp (I suck at Lisp) and Smalltalk, and why I return to them occasionally for a visit.
This year, I'm learning Ruby. Does that mean my team is moving to Ruby? Probably not, but it does mean I'm learning Ruby this year because I believe in sharpening the saw. You might be too busy sawing to sharpen, but I'd encourage you - no matter what brand or type of saw you use - to remember that there are other folks out there cutting wood successfully with a different kind of saw. Maybe they know something that you don't.
Either way, it's clear that the jury is still out on all these technologies. Hedge your bets and learn as much as you can. There's more than one way to express yourself. Give it a try.
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.



About Newsletter