Jason Rowe

Be curious! Choose your own adventure.

Shiny New Toys, Visitor Pattern, and Real World Functional Programming

 

Last week in the office I thought to my self, “Holiday weekend coming! I’ll have time to play with WP7 or maybe I’ll play with HTML5”. Then I saw this tweet on twitter.

We as developers spend too much time playing with baubles and shiny new tools and not enough time on core skills and fundamentals

I thought well, instead of using some free time this holiday weekend to build something why not do a little research. I happened to have a problem at work which someone suggested using the Visitor Pattern. So I spent some time looking over a few resources and Wikipedia has a great explanation. I won’t try to repeat that. One area I was stuck on though was the concept of double dispatch. I found this example on StackOverflow.com which solidified the concept. For some reason learning by spaceship and rebel alliance works for me.

So now that I had a good understand all I needed to was implement it right? I thought about it and figured that could wait. Now is an opportunity to work on my fundamentals. The Wikipedia article mentioned Common Lisp and how the Visitor Pattern simplified when using dynamic languages. This got me to thinking what would this look like in F# or a functional language?

While looking into functional languages I came across this Stack Overflow question “I am a C# developer, Should I start looking more on F# [closed]”.  Jon Skeet’s answer influenced me and I figured I would pick up a book Real World Functional Programming. It fits into the category of fundamentals because it’s using C# and F# to teach functional programming. Which I haven’t used in many years.

So I start reading with the goal in mind that I would make a quick example of the visitor pattern in F#. Don’t think I am a super geek (not that you would be confused because of my skills). I did take time to drink a few of these and go to the Crowded House concert this weekend. By the fourth chapter or so things started to sink in with F# and functional programming.

If I were using a declarative functional style of programming, I wouldn’t even need the visitor pattern at all!  I don’t have objects that would need to be separated from the algorithm. I would only have types and could use pattern matching and discriminated unions to perform my algorithm. The visitor pattern is needed in my current project because I’m using an implicit OO coding approach.

It was really shocking to me that this wasn’t obvious right away. I’ve been doing OO for so long now I’ve completely become accustomed to dealing with objects and state. A programming world without objects had completely slipped from my mind. Glad I took the time to get started on this book as it’s already giving me new ideas and techniques I can use in my daily C# and OO programming. Now to start working on some of the other programmer competencies.


Posted

in

, ,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *