Why methods shouldn’t replace a language

There’s been a big push for OO scripting languages like Python and Ruby over older procedural languages like Perl. While I used to be quite the Perl bigot, lately I’ve learn to appreciate other languages like Ruby. But while OO techniques can make some things much much easier, strict adherence to the OO concepts can be more trouble then it’s worth.

Take for example this common case of a for loop:
for ($i = 0; $i < = 10; $i++) { &lt;do something with $i&gt; }
In Ruby you’d use:
1.upto(10) { |x| &lt;do something with x&gt; }

I’ll give the win to Ruby here for being more concise, even though those people coming from most procedural languages will be taken back by the syntax. Now lets make a simple change and increment by 2 instead of 1:

If you read the first Perl example, this should be obvious:
for ($i = 0; $i < = 10; $i += 2) { .... }
Ruby, however isn’t quite so obvious:
1.step(10, 2) { |x| ... }

Wtf? It’s a different method? For the record, I had to ask 3 people who write Ruby code how to do that because the first two didn’t know the magical method name. Sure it’s a small thing, but it’s really fustrating when the “obvious” solution isn’t correct and doesn’t lead you to the answer.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.