Simplicity is Profound

[Warning: tech stuff.]

So I’ve been thinking over a very simple technique for a couple of weeks now, that is so brilliant in its simplicity that I wonder why I haven’t come across this before, and why nobody ever suggested something like this in my programming career.

I feel like going into a rant of them imperative programming, and then into the beauty of functional programming, unit testing, lisp and some other things made in heaven, but then, I am aware that good programmers already know, and the others don’t care, so there would be no point in all that.

But for its simplicity this is beautiful code:

The problem: when you’re into the whole functional programming ideology, you tend to have multiple nestings of function calls. func1(func2(func3))). And then one fine day, comes along a bug, and you’ve got to find the value that func2 returns.

What do you do?

A normal Java or C++ programmer (who, I can still imagine nesting functions three levels deep), will just pull out that func2 and store it in a variable, and print it and then pass it to that func1.

Maybe I’m wrong, but in my reasonably short life of programming, I’ve never seen anyone do it better. I wish to be less arrogant, but I’m hurt and and upset that none of you good programmers decided to teach me the better way.

If this were elisp, define a function like:

(defun deb (obj)
   (message "%s" obj)
   obj))

Did you see what I did there? Maybe some ruby code (which already has exactly this function — but less advertised — and you should use it):

def p(obj)
  puts obj.to_s
  obj
end

And then some Java:

<T>T p(T t) {
  System.out.println(t.toString())
  return t
}

What this provides you now, is that you can debug anything anywhere, with minimum surgery. func1(p(func2(func3()))). It’s brilliant once you get used to it. You’ll find that it’s not just for things like func1(func2(..)), you can use it with just about any kind of weird syntax that your language defines for you.

So. Didn’t you know this, or am I just too young and have a lot of learning to do?

About these ads

3 Responses to Simplicity is Profound

  1. S says:

    Thanks for this. I know I have done this sort of thing before, but non-systematically and without noting it down as a general method to keep in mind for the future. Now I will. :-)

    [I thought this was related to "trap patching", but that term appears to be something else.]

    Incidentally, do you also feel that these techniques come to your mind more naturally when you’re coding in some Lisp?* The language influences your thoughts!

    [* And also JavaScript: it's a really nice language once you ignore all its warts and the DOM (as Douglas Crockford has been proselytizing), and deserves respect as the most mainstream functional programming language. It's unfortunate that it has such a bad reputation.]

    • Arnie says:

      Yes! Lisp really influences me as a programmer! this idea first hit me while programming in lisp, I mean, it’s nothing specific to lisp, but for some reason it just makes it very obvious once you remove all the syntactic ugliness other languages create.

      Javascript is a fantastic language, before I knew javascript I wrote code in GWT: translating Java to Javascript, possibly the most evil language translation ever invented.

      My only complaint with JS is that creating functions is syntactically verbose. Which is why, I want to try ParenScript sometime.

  2. manish says:

    yes, you are right as processing become bulky is we add multiple function under function. It’s better to try the OOPs concept to do so.

    In java encapsulation is a good method to do so.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: