Hiatus (agh) wrote,

Function Calls as Messages

The metaphor of the function call as a message has never resonated with me, and I think I finally know why.

You see the term "message" a lot in software engineering, and not just for function calls.  In Win32 programming, user events are considered to be passed as messages, via the "message pump".  In networking, of course, messages go about via packets.  These are reasonable uses of the term.

But I've seen a surprising tendency to talk about procedure calls as messages, even non-remote procedure calls.  I see it a lot in literature, as in  "[An] API is an older technology that facilitates exchanging messages or data between two or more different software applications." [1]  I see it when reading about OO, as in, objects exchanging messages.  That terminology puts me on edge.

Message implies, to me, some unreliability.  It's asynchronous.  It may not be delivered.  It may be malformed or misunderstood.  There may be bad weather.  Etc.  A message also implies some sort of packaging, like an envelope or a little ribbon around a scroll. 

For the simplest case, local procedure calls via a well-designed procedure on an object, (a) nothing can get messed up along the way, (b) the interface is designed, as much as possible, such that the caller cannot make mistakes in input that aren't immediately flagged and actionable by the programmer (at compile time) or his function (at runtime), and (c) there's not any packaging going on, just passing of raw data in separate parameters.

Two basic extensions of this simple case flirt with the idea of being messages.

The first is remote procedure calls.  Those look like regular procedure calls, but marshal their parameters to call out-of-process or on another machine, fundamentally asynchronously.  Unreliability and packaging, so this is a message, right?  Well yes, but... the spirit of the RPC technique is to abstract away the messagey-ness and make it look like a simple, synchronous local call.  As an RPC caller, you only need to think about the messagey-ness when you're writing the error-handling code.  At the academic and architectural levels, you're not dealing with messages, you're dealing with something much more tightly bound.  RPC is thus a protocol designed to abstract away the "message" metaphor.

The second is procedure calls that take strings to be interpreted at runtime, as in
Object.SetProperty("Color", "Black");
This technique is often used for XML or SQL queries, where text of another language is being passed in and parsed through a procedural language.  The string can be thought of as a bundle, which is at a layer beneath the procedural program semantics, and in that way this type of call really is a message.  

And that's its problem.  It's so late-bound, it gives you no statically-checked assurances that you're doing something reasonable. It's OK for machine-generated code to do this kind of thing, and that's quite common, but to ask a more fallible human to program like this is a bad idea.  A human would much rather write
with nice autocompletion and red squigglies and other compile time checks.  For the case of SQL, LINQ is supposed to provide the static checking long missing from embedded queries.
Strings are sometimes used for less "legitimate" purposes too, such as for extensibility.  Enums aren't easily extensible from version to version, but strings are.  The lack of compile-time checking is a large price to pay for versioning, and it makes me wonder why better solutions haven't been popularized for this problem.

So, if a function call being like a message is such a bad thing, why embrace the term?

[1]  How a Good Software Practice Thwarts Collaboration - The Multiple Roles of APIs in Software Development deSouza et. al.

Tags: code

  • Windows 7

    Windows 7 is coming out in beta this weekend. It's a no-brainer upgrade from Vista, as it is, for my purposes, a strictly superior OS. And if you're…

  • Pro and Repro were sittin' in a boat... Pro fell out, who was left?

    So, I'm debugging this issue with the tooltips in the Windows common controls. A particular debugger is crashing when it's trying to display the…

  • F#MiniJava

    Sunday, Chris and I finished our minijava compiler implementation in F#. Whereas most of the class implemented it in Java or C#, we knew that all we…

  • Post a new comment


    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.