This post originated from an RSS feed registered with Ruby Buzz
by Michael Neumann.
Original Post: Is Ruby Human-Understandable?
Feed Title: Mike's Weblog
Feed URL: http://www.ntecs.de/blog-old/index.rss?cat=ruby&count=7
Feed Description: Blogging about Ruby and other interesting stuff.
Is Ruby human-understandable? Understandable by humans with absolutely no
experience in computer programming? How can we as experienced programmers
at all answer such a question? Well, we can't! The only way to determine
whether a language is easily understood by novices is to actually ask them.
To find an answer to this question I performed some tests with my beloved
girlfriend. She's an absolute novice in this area as she'd never seen any
source code of a computer program before. I gave her a page of paper with
examples written on it and told her to write down what would be the output
for each example. Then I left the room to prevent any communication during
the test.
The results were promising! She mastered 4 of 7 tests without my help.
Tests two and seven were obvious after introducing := for
assignment, and = for equality. And the failure of test 6 had more
to do with the English word "select" than with Ruby (keep in mind
that English is not our native tongue). Wow, she even implicitly used lazy
evaluation ;-)
The Tests
Test 1
print "hello world"
She passed this test easily.
Test 2
result = (3*10) + 5
print result
She pointed out a problem that I wasn't aware of. From her point of view,
the output should be "(3*10) + 5" and not "35". I
discussed this issue with her and found out that it would be easier for her
to understand if the "=" would be a
":=". Once I changed the tests according to this
problem, she was able to solve them.
Test 3
5.times {
print "hallo welt"
}
No problems here!
Test 4
5.downto(1) { |counter|
print counter
}
Again, no problems!
Test 5
elements = ["Hydrogen", "Oxygen", "Helium", "Ferrite"]
elements.each { |element|
print element
}
She even mastered this test without problems.
Test 6
one_to_ten = [1,2,3,4,5,6,7,8,9,10]
even = one_to_ten.select { |i| i.even? }
print even
Once I told her that select translates to the German word
auswählen (choose) she mastered this test easily.
Test 6b
Same as test 5, plus the following line:
print elements.select { |element| element == 'Ferrite' }
I created this test after test 6 failed. She mastered this one easily and
thereafter was able to solve test 6.
This test raised the same issue as test 2: Assignment vs.
Equality. Her result was "1 + 1 + 1 + ...", which
is not all that wrong. Of course it's wrong in Ruby, but remember Haskell's
lazy evaluation:
{- create an infinite list of ones -}
ones = 1 :: ones
She mastered the test after I introduced ":=" for
assignment instead of "=".
I created this one after she solved test 7 successfully. Of course it was a
mere child's play for her ;-)
Discussing Loops
I took the opportunity and asked her about loops.
loop {
...
}
loop {
...
break if cond
...
}
while 1 {
...
}
while true {
...
}
while cond {
...
}
repeat
...
until cond
She easily understood the loops that use the loop keyword, but not
the others. That's exactly what I prefer for most of the loops I write in
my programs. And IMO they are much more readable and less error prone than
while or repeat until loops.
I remember a statement of a mathematics professor at University of
Tuebingen who told us that the human mind is not good in understanding
negation (the "not" operator from logics). And at least for my
mind, he's correct. Whenever I can, I prefer positive logic over negative:
# this one is easy to understand (for me)
loop {
break if cond
}
# tell me when this loop aborts!
while not cond {
...
}
And whenever I see something like "while 1", especially
in so called very high level languages, I ask myself whether they
got caught in C forever.
Finally, I asked her about for loops:
for i in 1..10 do
...
end
As she hasn't studied mathematics, she had problems understanding the
for ... in notation. Luckily, Ruby supports customized loops like
"1.upto(10)" which reads easily even for absolute beginners.