Programblings

Rambling about programming and life as a programmer

Archive for the ‘programming’ Category

Disblog haasmoovt

Posted by webmat on March 1, 2008

For a while now I have wanted a bit more control than wordpress.com could offer me. Now has come the time to make the move. This blog’s new home is programblings.com.

Here is the link to the new feed.

By the way, sorry for the title. I just wanted to get your attention :-)

Posted in programming | Leave a Comment »

Do not learn Ruby

Posted by webmat on February 20, 2008

Update: This article has has a new home on programblings.com. Go to the article.
Comments are closed here but still open there :-)

Ruby will get under your skin. You will miss its features and quirks when you’re not using it. You might even find other languages insufferable, once you get comfortable with Ruby.

After you’ve started using Ruby, there’s a significant chance you’ll start loathing whatever code base you currently have to work on. Especially if it’s a statically compiled language. A code base you used to think was ok, except for its few quirks.

After a while of perusing the different Ruby-related blogs, you’ll have heard other Rubyists speak of their work with words like beauty, productivity, expressiveness, conciseness, fun and you’ll realize just how far your current language is taking you from all of these words.

You’ll see

Dictionary<string, string> someDic = new Dictionary<string, string>();

And dream of

some_dic = {}

You'll see multiple declarations for the same method, trying to emulate optional parameters and think of Ruby's symbols and options hashes, where such a simple method as:

def method_with_options(options = {})
encoding = options[:encoding] || 'utf-8'
puts('Other option detected') if options[:other_option]
#...
end

Enables all of the following uses

method_with_options :encoding => 'utf-16', :other_option => true
method_with_options :encoding => 'utf-16'
method_with_options :other_option => true
method_with_options

You'll hear about metaprogramming and the complex syntax or frameworks that can bend Java, C# or C++ to allow a programmer to achieve what he seeks.

In the back of your head, you'll think of the insane flexibility allowed by

- simple Ruby syntax for method chaining or redefinition;

- dynamic class definition, that lets you add methods to any existing class, even Ruby's core classes;

- duck typing, where objects of any type can be passed to a method, as long as it responds to the expected method calls in a reasonable fashion;

and oh so many other Ruby niceties.

You'll encounter twisted method definitions such as

bool SomeMethod(int param1, ref SomeClass someClass, out SomeEnum resultType, out string result)

and think of Ruby's multiple returns that allows you to clearly define what's a return value and what's a parameter:

success, resultType, result = some_method(param1, someClass)

You'll delve into huge, puzzling class hierarchies that struggle just to use the right abstraction level for class names... You'll eventually realize that the whole hierarchy was simply there to share a few methods among loosely similar classes.

Then you'll really get irritated at all the accidental complexity that could have been avoided by simply using mixins, where you define a common method and then include it in any appropriate class. And all of this without ever puzzling over strange abstract names for classes that happen to sit between 2 clear-cut levels of abstraction.

You'll want to explore a new part of the .Net API by playing with it. You'll create a dummy project in some random directory, find a name for it, include the proper parts of the API in the generic main class created by default and finally start playing with the construct of interest.

All this time you'll be thinking of IRB, the Ruby interactive console. It not only allows you to play with an existing API with absolutely no fuss, but thanks to Ruby's flexibility, you can even define classes in the console and then play with them!

But then you'll think "Oh yeah, IRB is in fact so flexible that I can use if from a frickin' web page (with a tutorial)!" And you'll go play there for a couple minutes (when no one's looking), just to keep you sane for a couple more hours. Until the C++ / C# / Java drudgery is over for the day.

You've been warned. If you learn Ruby, you'll start thinking it's impossible for you to keep using the technology you're currently using at work. If you're patient you'll try to introduce it there gently (and most likely get frustrated at the time it takes). If you're not so patient, you'll just end up changing job.

Next monday I'm joining the great team at Karabunga to work on Defensio. I'll be doing Ruby and a bit of Rails. Liberation is coming :-)

Posted in garbage out, programming, ruby-rails | 54 Comments »

An easy way to make your code more testable

Posted by webmat on December 13, 2007

Update: This article has has a new home on programblings.com. Go to the article.
Comments are closed here but still open there :-)

James Golick wrote a very good article about testing a while ago. In it he dissects (and refutes) the too often heard arguments where people say they don’t write automated tests because they don’t have the time.

In the comments, some people concluded that yes, they should try to write more tests, but didn’t know where to start. In this post I won’t suggest frameworks, or specific tutorials. I’d just like to give one very first step that will help you write code that is easier to test. You’ll benefit from it even if you don’t use a testing framework yet.

As the title suggests, what I’m suggesting is pretty simple. Write side effects free methods/functions. Simple isn’t it? The rest of this article is just about explaining my point. So if the light bulb went off already, you can stop reading now.

I’m kidding, of course! So let’s not take anything for granted, instead let’s make sure we’re on the same page and define “side effect free”. It means that a function (substitute with “method” if you like) receives parameters, spits out a result and has not touched anything outside of it. There are two key parts to this definition:

  1. The function does not depend on anything else than it’s parameters: it does not expect a variable to be set outside of it to work properly (at the object, class or global level).
  2. It does not modify anything. Its result can be entirely observed either from the return value or from the exception thrown.

Of course you often have to modify the state of the application as a result of a computation. What I’m suggesting is not a substitute for that. What I’m suggesting is simply to put the juicy bits of your computation in a side effect free function. This part will be trivial to test, but you’ll still have the code that uses this function. That other part, which modifies the state of the app will need unit tests or other higher level testing.

Trivial examples of side effect free functions can be found in any good math library supplied with a language. Of course no modern language expects you to set a global variable in order to compute a square root. Those who want to follow the Ruby examples can do so on Try Ruby (fear not, you’ll be able to follow along even if you don’t know any Ruby).

Math.sqrt(4)

=> 2.0

And

Math.sqrt(-1)

Errno::EDOM: Numerical argument out of domain - sqrt
from (irb):6:in `sqrt'
from (irb):6

So there we have it. The result of squirt is observed either from the return value or from the exception thrown. We don't expect any state to have been modified anywhere else in the application. I'll present a less trivial example in a bit, but first let me just say that the direct consequence of writing this kind of code is that you can test it trivially, whatever your technique of choice.

  • If you're in your debugger or in your interactive console, you can call it as many times as you want, with different parameters and check out if its behavior is what you expect.
  • If you use unit tests, you can code the interesting scenarios and verify their expected outcome, only in a repeatable manner.

Now since we all have a Math library of some sort in our language of choice, let's look at another example: analyzing the parameters that will dictate the execution of your program. This is valid for configuration files with a bit more work, but let's keep the example simple and just analyze command-line parameters.

Most languages provide us with some kind of array of parameters when entering our main function. The common way of dealing with them is to slap a big if or switch statement somewhere at the beginning of your program, which sets the state of the application accordingly, before actually starting to work on the application's main task.

A side effect free approach would be to split the process in two parts:

  1. parse the parameters
  2. set the state / do some work

For example we could define a function that accepts an array and returns a hash (a Dictionary for .Net folks, a Map for Java folks) of the execution parameters:

{

'arg1' => 'val1',

'arg2' => 'val2'

}

So let's say we start with the following method to analyze our arguments (to see the readable, indented version of this code, look at it on Pastie) :

def analyze_args(arg_list)
parsed_args = {}
#We check that each argument is in the form 'arg=value'
arg_list.each { |arg|
key, value = arg.split '='
if (key.nil? || value.nil?)
raise "Some arguments are not in the 'arg=value' format"
end
parsed_args[key] = value
}
return parsed_args
end

Now we can trivially test the parsing of the command-line params:

analyze_args(["mom"])

RuntimeError: Some arguments are not in the 'arg=value' format
from (irb):23:in `analyze_args'
from (irb):20:in `each'
from (irb):20:in `analyze_args'
from (irb):29
from :0

analyze_args(["mom=food"])

=> {"mom"=>"food"}

analyze_args(["mom=food", "dad=car"])

=> {"mom"=>"food", "dad"=>"car"}

Now that this part is taken care of, I can test it with whatever input I want, trivially.

To reiterate, in order to make your code easier to test, just extract the juicy bits of your program in side effect free functions and keep them apart from the rest of your program, which in turn makes sense of the result. At least the side effect free parts will be trivial to test.

This is just the beginning of the testability and automated tests journey, however. Of course you still have the rest of the program to test, preferably in an automated fashion.

Posted in garbage out, programming, ruby-rails | Tagged: | 25 Comments »

Culture shock

Posted by webmat on November 16, 2007

Update: This article has has a new home on programblings.com. Go to the article.
Comments are closed here but still open there :-)

I had a bit of a culture shock a couple of days ago. I saw a video of the visual designer for IronRuby, named SapphireSteel. The tool looks very nice and polished.

Something unsettled me, however, when I saw it. It has nothing to do with the team or what the narrator says. In the demo, he shows us a couple of features of the designer, and inevitably we end up seeing snippets of code. That’s when the culture shock, uhh, shocked me.

I’ve seen plenty of verbose code in my time. I even work in C# right now, which I find errs on the side of verbosity. So I’m used to seeing verbose code, and I’m certainly used to see WinForms code.

But seeing this WinForms code in Ruby felt very weird.

WinForms in Ruby is Still WinForms

WinForms code in Ruby is still exactly as verbose as in C#. Only the semicolons are missing.

C# colleagues might tell me that this code just satisfies what the API wants to hear, in the way it wants to hear it. But I’m getting used really fast of the Ruby way of doing things. Which consists “sane defaults” for a start, and using DSLs to solve complex problems in a declarative manner rather than in an imperative manner.

WinForms development with Ruby screams for a Rubyish wrapper. Anybody up for that? If it can be done for Java’s Swing (with Profligacy, the Swing reducer), it can certainly be done for WinForms.

For my part, however, I’d be much more interested in exploring XAML with Ruby, but that’s another story.

Posted in programming, ruby-rails, windows | Tagged: | Leave a Comment »

JRuby, not in its setting (or configuring jirb under Windows)

Posted by webmat on October 18, 2007

Update: This article has has a new home on programblings.com. Go to the article.
Comments are closed here but still open there :-)

For a good while now, I’ve been using Ubuntu on my main home machine. Working with Ruby and JRuby on Linux is a charm.

Tonight, as I tried to set myself up to work with JRuby on Windows*, I bumped into an annoying problem. When I tried to configure jirb, the JRuby version of IRB, it didn’t always seem to find the .irbrc config file in my home directory. I looked a bit on the 'net and found nothing that helped me solve my problem. I've worked out a fix, you can follow along in this wonderful adventure.

So to configure irb, I create the .irbrc file in my home directory.

C:\Documents and Settings\Mat\.irbrc

Note: Windows will yell at you if you try to make a new file starting with a "." in windows explorer. Just use any good text editor to create it directly.

In it I put some configs for auto-completion and auto indent, as suggested on the Tips and Tricks page on the Rails wiki:

IRB.conf[:AUTO_INDENT] = true
IRB.conf[:USE_READLINE] = true
require 'irb/completion'
puts "Yay! Completion's loaded!" #Uhh, that's my special debugging code :-)

Now I fire up a console and start the interactive Ruby shell with

C:\Documents and Settings\Mat>jirb
Yay! Completion's loaded!
irb(main):001:0>

Hmmm, it works. What am I yelling about? :-) Let's say we fire it up in an actual project directory instead.

C:\projects\an_actual_project>jirb
irb(main):001:0>

Oops! My "special" debugging code isn't executing now :-)

If you care to see what's the code that hints at how to solve the problem, you can open

...\jruby-1.0.1\lib\ruby\1.8\irb\init.rb

Or check it out a snippet of IRB.rc_file_generators online.

As we can see, if the HOME environment variable is set, the method looks for .irbrc in the directory, otherwise it looks for it in the pwd (present working directory).

Nice! That explains why it works in my home and not elsewhere :-)

So now I just have to set a HOME variable and I'm all set:

home.png

Now after restarting my console (to have the updated environment variables):

C:\projects\an_actual_project>jirb
Yay! Completion's loaded!
irb(main):001:0>

Note that even though Windows variables are not case sensitive, you must name your HOME variable in capital letters. Ruby expects the ENV hash to contain the HOME variable in caps.

* Q: Why would I want to set this up on Windows, if I've got a perfectly good Linux setup at home?

A: I'll be giving a talk about JRuby at the next Montreal on Rails, and since I haven't really needed a laptop in at least two years, well, I only have 3 years old laptop. This old geezer can run equally old versions of Linux, but I wouldn't like my audience to have to suffer the visually challenged distro I currently have on there.

Posted in garbage out, programming, ruby-rails, windows | Tagged: , , , | 2 Comments »

Using Windows? Try Powershell!

Posted by webmat on October 16, 2007

Update: This article has has a new home on programblings.com. Go to the article.
Comments are closed here but still open there :-)

Recently, at work I’ve had to implement a couple of simple scripts. They were yet again not simple enough to be captured completely in a batch file. For example I needed to have an arbitrary precision and format date/time that I could use in a file name. date /t and time /t weren't cutting it.

I thought about implementing a small command-line tool just to give me the functionality I was missing, but it felt too patchy for my taste, this time. I had to use it on other test machines so I wanted to be able to just copy the script without having to update some executable utilities as well.

Sadly I could not use Ruby because my company doesn't use it, and I don't want to sneak it in the wrong way. And as I said it was to be used on another machine, so it was not like creating a personal script on my own machine. In fact it even had to be documented in a manual testing procedure.

So I decided that it was time to try Powershell. I've been hearing about it for quite a while now, and I heard it's pretty powerful. Since it's the future of Windows console/scripting anyway, I figured nobody sane at a Windows shop would yell at me for introducing this particular technology.

Now that I've been dabbling with it for the past few weeks, my opinion is starting to form and it goes a little bit like this. If you're a Windows power user (especially an admin or a developer) and you don't know Powershell, you're at a disadvantage :-) The "Power" in Powershell is very well deserved. If you're still flopping along using batch files, you're using a weak, error-prone tool where a new, very powerful tool is available. It's free and it's by Microsoft, so there's no reason it wouldn't be well received in most Windows shops :-)

I decided to capture my understanding of Powershell in an introductory article. In it I explain some of the basics of the language and introduce you to all that's necessary to get started with Powershell. In the reference section at the end I also mention other good sources of information if you want to learn more. Depending on the amount of fiddling around you do while following along, I guess it's a 30 minutes to 2h read tops.

So without further ado, Discover Powershell (it's an order!)

Posted in garbage out, programming, windows | Tagged: | Leave a Comment »

Free PDF Rails Book for beginner Rails developers!

Posted by webmat on October 3, 2007

Update: This article has has a new home on programblings.com. Go to the article.
Comments are closed here but still open there :-) 

As seen on the Rails blog, sitepoint.com is giving away free PDF copies of Patrick Lenz‘s book, Build Your Own RoR Web Apps. The offer is good for 60 days. Well, 59d15h at the time of posting :-)

It involves jumping through a couple of hoops, and the book contains a couple of pages containing ads about other Sitepoint books. But nothing too bad, at first sight. And it’s still a free PDF book! So if you know anybody who’s curious about Rails and might want to try it out, you have until the beginning of December to point him/her to rails.sitepoint.com.

Posted in garbage in, programming, ruby-rails | Tagged: | 2 Comments »

Montreal on Rails tonight

Posted by webmat on October 2, 2007

Update: This article has has a new home on programblings.com. Go to the article.
Comments are closed here but still open there :-) 

Tonight is Montreal on Rails #3! I can’t wait to attend: this will be a nice refreshment from my gruesome C++ tar pit at work, ugh! :-)

The first 2 were very interesting and filled with great people to meet. Tonight promises to be just as great, and as a bonus, we’re leaving behind the small McGill University classroom to be welcome instead by Fred and Ben from Standoutjobs. Can’t wait to see that either!

And great big thanks to Mat and Carl for organizing this monthly meetup!

Posted in garbage in, programming, ruby-rails | Tagged: , | 1 Comment »

Podcasts I listen to

Posted by webmat on September 29, 2007

Update: This article has has a new home on programblings.com. Go to the article.
Comments are closed here but still open there :-)

Here’s the deal. I have to beat writer’s block. I wrote a first post a while ago, then came the time for a little vacation and haven’t gotten down to write here again. Well, except for this quickie.

I thought I’d write a list of the podcasts I listen to. This way I can just point people here when they’re curious. I’ll put this somewhere else soon, to update it when I discover new stuff, but now I’m just in brain-dump mode :-)

Those I listen to religiously

  • The Linux Action Show! – Bryan and Chris entertain us with Linux news, the occasional interview, tips on development, system administration, desktop compositing, and So Much More!
  • .Net Rocks – Carl and Richard meet the cream of the crop in the .Net world. Great podcast to learn about tons of tools and practices related to .Net development and other MS technologies surrounding it. There’s even good stuff for non .Net developers.
  • Hanselminutes – Scott Hanselman’s the ultimate geek. He seems to follow everything in the world of development. He often talks about .Net development, but has a very open mind and covers a broad range of other technologies, including a bit of Ruby on Rails once in a while. He also covers lots of other technology-related topics. He pretty much always manages to keep the podcasts around a no-fuss 30 minutes, hence the name Hanselminutes.
  • Security Now – The ultimate security podcast, with host Leo Laporte and security expert Steve Gibson. All episodes are worth going back to, since the formula is about the theory behind concepts of security as well as security tools, rather than being a news-centric podcast.
  • Macbreak Weekly – Definitely a news-centric podcast, very entertaining crew that transmit their passion for the Mac (and the iPhone) very well. The discussion often revolves around digital media as well, with very interesting insights. Oh, and Merlin Mann is often one of the cohosts. Very funny guy :-)
  • The Ruby on Rails podcast – The podcast extraordinaire if you’re interested in Ruby on Rails. Interviews with the big names in the Rails community. Lots of talk about different tools that can aid you in your quest to create the ultimate Rails app. The summer has unfortunately brought few new shows, but one came out recently. Let’s hope the pace picks up again, I miss the RoR podcast :-)
  • FLOSS Weekly – Very promising podcast about Free, Libre and Open Source Software (not about dental floss). Some very big names have been interviewed on the show (Guido van Rossum, “Maddog” Hall, Jeremy Allison, Randall Schwartz…) The first host, Chris, has a very busy schedule and the pace has really slowed down for a while. But there has been interest by Mr Shwartz and Allison to host the show once in a while as well. I can’t wait to see that happen (the most recent show is hosted by Schwartz, actually).
  • You Suck at Web Design – Short, funny podcast by Matthew D. Jordan about being a freelance web designer (and the journey to get there). Twisted humor and cringe-inducing stories await you here.

Those I listen to occasionally

  • net@nightAmbermac, the ultimate web geek speaks with Leo about what’s going on the internet. Interviews with web entrepreneurs. Shows recorded live on talkshoe.com (can be good or not).
  • Windows Weekly – Paul Thurrot speaks with Leo about what’s going on in the world of Windows and Microsoft in general. The subject may seem dry, but the hosts definitely make up for it. A plus for anyone working in IT with lots of Windows machines. Thurrot knows the Windows world inside out, but is very balanced: he uses (and loves) his Mac and uses Ubuntu as well, so you can count on his opinion to always be fair.
  • This Week in Tech – News-centric podcast about the world of technology in general. Very interesting hosts, including once in a while Merlin Mann, as well as John Dvorak.org/blog (don’t ask :).
  • Penny Arcade Podcast – Every once in a while, Gabe and Tycho take us through the creative process of creating the day’s comic strip.

</braindump>

Huh, well! I appear to be beating my writer’s block little by little :-)

Let me know if you’ve discovered something interesting here or if you’d like to share the podcasts you listen too once in a while.

Posted in garbage in, programming | Tagged: , , | Leave a Comment »

On using C++

Posted by webmat on September 25, 2007

Update: This article has has a new home on programblings.com. Go to the article.
Comments are closed here but still open there :-) 

Sorry for this content-free post, but this is just too good:

When your hammer is C++, everything begins to look like a thumb.

This especially strikes a chord, since I’m currently working on something in C++ at work. Ugh!

Posted in programming | Tagged: , , | 3 Comments »

 
Follow

Get every new post delivered to your Inbox.