Quack, you B*!#*rd!

Now, don’t get me wrong… I like dynamically typed languages (especially Python, not so much Ruby ;^), but I have to say that the whole “duck typing” thing is a bit overrated!

Take the following simple example:-

def pretty_print_person(person):
    """ Pretty print a person instance to stdout. """

    print "Name:", person.name, "Age:", person.age


Although no types were harmed in the making of the above function, I can’t just pass any old object in as the “person” argument. If I try to pass in an integer, say, 42 I get:-

AttributeError: 'int' object has no attribute 'name'

In other words there is an implied protocol (Python terminology) or interface (Java et al!) that is required of the “person” argument, namely that it has attributes called “name” and “age” (note that in Python that’s *all* it says, it says nothing about the types of those attributes).

Now if I am a single developer, working alone in my bedroom, on a small piece of code that nobody else in the world will ever need (or want!) to see, and I name all my arguments nicely so that the implied type is kinda obvious, and I have an amazing memory then this is probably fine, but what if I am working in a team of n developers (where n > 1 ;^)?

In the team situation, the implied type information that was in the head of the developer that created the function is thrown away. Now, I can hear the quacks of protest already… “Why not just add a comment?”. Fair cop. Here is some code taken from the “ActionController” module in Ruby.

  # Holds a hash of all the GET, POST, and Url parameters passed to
  #  the action. Accessed like params["post_id"] to get the
  #  post_id. No type casts are made, so all values are returned as
  # strings.
  attr_internal :params

  # Holds the response object that's primarily used to set additional
  # HTTP headers through access like response.headers["Cache- # Control"] = "no-cache". Can also be used to access the final
  # body HTML after a template has been rendered through
  # response.body -- useful for after_filters that wants to
  # manipulate the output, such as a OutputCompressionFilter.
  attr_internal :response

  # Holds a hash of objects in the session. Accessed like
  # session[:person] to get the object tied to the "person"
  # key. The session will hold any type of object as values, but the key
  # should be a string or symbol.
  attr_internal :session

  # Holds a hash of header names and values. Accessed like
  # headers["Cache-Control"] to get the value of the Cache-
  # Control directive. Values should always be specified as strings.
  attr_internal :headers

Here the developer has, very nicely, commented the attributes so that I can see, for example that the “headers” attribute contains a hash of header names and values, and that the values should always be specified as strings. I can also see that the “response” attribute holds a response object which also places a requirement on the protocol/interface support by any values assigned to it.

All of this is very handy information indeed (assuming that the comments are correct and up-to-date of course), but why not provide the information in the code so that it is accessible beyond just the API documentation system? You never know, it might come in handy for:-

a) validation of values assigned to attributes/passed in as arguments
b) simple GUI generation
c) OR database mapping
d) component frameworks
e) web frameworks
f) insert your favourite tool here ;^)

Now, the hard-hearted amongst you might, at this point, just tell me to bugger-off back to Java or C++ or whatever statically-typed hell-hole I came from. Well, truth be told, I don’t want to. I like the terseness of most dynamically-typed languages (so, maybe Ruby overdid it there a tad ;^), I like the meta-programming/introspection capabilities, I like how I can get closer to being able to express *what* it is the code is intended to do as opposed to *how* it does it. I especially like being able to prototype without types and gradually “harden” them as I understand more about what is going on (which IIRC was a feature of Dylan, a programming language from Apple).

Now, I am obviously, neither the first nor the only one to want this combination of static and dynamic types, and if you cast an eye around the Python community you will see that every person (and their dog!) involved in team development has, at some point, written or adopted a system for specifying type information, and there at least 2 stable and mature projects that have achieved much wider adoption:-

1) Traits
2) Zope Interfaces

Disclaimer: I used to work for Enthought Inc., the company behind Traits, but I didn’t write it, I have no vested interest in it, and it is free, open-source, and BSD licensed!

IMHO, using dynamically-typed languages in conjunction with optional static-type systems combines expressive power, readability and incredible tool potential, and offers a viable alternative to statically-typed (and usually compiled) languages for non-bedroom based development teams ;^)

Blocks and Procs

If a function/method uses a block, why would I ever use the implicit ‘yield’ syntax instead of explicitly pass the block as the final ampersand argument?


Why would I do:-

def foo(x)
  yield x

Instead of:-

def foo(x, &block)
  block.call x

Doesn’t the implicit syntax hide the fact that the function/method allows a block from the caller, meaning that the only way to discover it is to read the code?!? What am I missing?

Function, schmunction…

Hmmm, because of the fact that it is syntactically legal to call methods/functions with no parentheses (i.e foo is the same as foo()) I can’t define a function and then inspect it as I would expect, e.g.

def double(x)
  return x * 2

If I do this in “irb” and then enter “double” it trys to invoke the function, but I just want to see the bare-nekkid function as an object ;^(

First Impressions…

And the first one is Frank Spencer… I’ll be here all week ;^)

I have to say, that the geek side of me likes much of what I see in Ruby so far… objects everywhere, blocks, the ability add to “re-open” class definitions etc. but the team developer side of me, however, ┬áis a bit more apprehensive.

My pet “thing” in software is finding tools and techniques to make the intent of programs clear. At the simplest syntactic level this comes down to what is usually referred to as “readability”. Martin Fowler famously said “Any fool can write code that a computer can understand. Good programmers write code that humans can understand”, and it seems to me that Ruby’s terseness is potentially more help to the computer ;^)

I think it comes down to a question of whether you think it is the transcription (i.e. typing) or the comprehension (i.e. reading and understanding) of code that is the tricky part. To add new features and to fix bugs a developer has to first determine the intent of the code and my hunch is that that takes somewhat longer than the typing required to make it happen.

So, that said, does anybody really think that we actually save time by typing ‘to_a’ or ‘to_s’ instead of ‘to_array’ and ‘to_string’?

Learning Ruby…

We’re just starting a project that’s going to be using Ruby on Rails and so I figured learning Ruby might come in handy ;^) Now, I’m not a language “bigot” in any sense of the word – “Horses, for courses” is my motto, so if you are a die-hard Ruby fan, don’t be offended if any of my posts on this topic seem critical – I’m just telling it like I see it as an experienced developer coming to a new language, and I am not trying to promote any other language by doing so. I can quite easily find just as many “disappointing” features of pretty much any language ;^)

Oh, and as I’m a newbie to the language feel free to correct me when I’ve misunderstood something!