Wednesday, January 17, 2007

Brain Repair: Thinking in Ruby instead of BASIC

Brain Damaged

When I see an IF statement in any programming language, it still comes with a THEN. A FOR statement still comes with a NEXT. My first language was BASIC (learned on a Commodore PET in Junior High), and it's still in my head. As Edsger Dijkstra said "It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration." I believe that most of my mental mutilation has been repaired, but some residual damage is apparently there.

In the same essay, Dijkstra also says "The tools we use have a profound (and devious!) influence on our thinking habits, and, therefore, on our thinking abilities." Since switching to Ruby and Rails for the majority of my programming work, I have repeatedly found this statement to be true. Other than some long-ago assembly language programming, my past work has been done in a relatively similar family of languages (C#, Delphi, VB, etc.), causing me to settle into predictable and comfortable thought patterns.

Brain Repair

To dive deeper into Ruby and change some old habits I've been exloring the following with satisfying results:
  • Array and Ennumerable methods: (in the Pickaxe book or here). Arrays include the methods of Ennumerable, so both are worth knowing. Since ActiveRecord returns arrays for multiple record searches, it is good to know more than for and .each to work with the models.
  • Ruby modules: It's beautiful to have something other than inheritance to share behavior between classes.
  • Method parameters: Default parameters, hashes as parameters, variable length argument lists, using a code block as a parameter, using a code block at the end of the method call.
  • Dynamically adding methods to classes and objects.
It is productive to examine the Rails source code, scaffold-generated code, code from plugins and engines, etc. line by line. When I pick someone elses code apart, making sure I understand every method, operator, etc. used, I learn a lot -- fast. It is painful but effective.

2 comments:

Luis said...

I agree with you, but in 75% ;-)

Like you, I started programming in 80's with BASIC, but adapted to different variations and evolutions of it (so it no longer stick to BASIC definition).

Ruby, in the other hand, is a expresive language, more than a structural, procedural one. It priorizes understanding (readability).

Still, good programming practices could be learned from Ruby and applied to your other languages in your toolbox, something I do too often with Python and FreeBASIC.

Instead of fixing/repairing your language capabilities, try learning from all them and converge into something productive to yourself.

Anonymous said...

Everywhen I stumble over theese words from Dijkstra, I am pervaded by the painful feeling that I will never be able to code in any language. I didn't use Basic, but my story started with Fortran IV when statements had to be code-punched on cards and 1 mb of memory took an entire floor of a building. Am I definitely "brainfucked"? Yes of course. Why ? Just because I still suppose that a good language should be composed of a small set of simple statements. So I still think of an array as a sequence of locations (of the same size each) where contents can be stored and retrieved by reference to it's name and an index value.
Too damaged to be repaired ?
It seems so. I am poorly inclined to read more than I can remember for at least a couple of hours. When I tried to look at those Array and Ennumerable method, I realized that when I was in the middle of the list I could no more remember how it begun.
I'm in despair....
Who said "keep it simple, stupid..." they must have been thinking of me, I believe....