whatspop - Kunal Anand

Designing a simple programming language

7/16/2006

Most non-programmers I come across are not inclined to learn any programming language because of its respectively steep syntax learning curve. I sympathize with those folks. There are countless moments when I open up an algorithms book or read a reference manual and walk away frazzled from all the geeky greek symbols. Sometimes I go back and stick it out, while other times I never return to solve the problem. Perhaps we can reduce the learning curve by making syntax more...natural.

Here is the big preface: I am not a language designer (I wrote this over a span of 20 minutes on a Sunday morning). I am merely interested in taking off the computer science hat for a moment and simplifying "different" concepts for other people. With that in mind, let us begin by looking at a common process:

A sixteen year-old student (suppose her name is Sandy) will get rewarded or disciplined from her parents depending on her test score. If Sandy gets a 90 or above on the test, then her parents will take her to Disneyland (how to go broke fast?). If she gets an 80 or above, then her parents will buy her a Superman Returns toy (I really need to see this movie soon). If Sandy gets anything else, her parents will get her a tutor.

When we analyze the above passage, we see things and actions. In this case, an action (reward or disciple) happens if a thing (test score) is excellent, good, or bad (all quantifiable). I want to start by modelling a student, who has a test score among other attributes (name and age). Geeky people will call these models objects - which sounds weird to a lot of average folks. So here is my approach to creating a model:

model student:
part name = "unknown"
part age = 0
part score = 0

So what does the above code do? It simply defines a model called a "student" and specifies its parts. The way I see this is that a model is defined by its identity (hopefully, this is not too philosophical for you), which is comprised of attributes. If you are a geek, suspend your thoughts about model initialization and assume that default part values will be instituted unless something explicit is passed in. Also, talking about polymorphism, inheritance, and other babble, is outside the scope of this rant.

So if you want to model a new student, you can do:

my_student_model = model person

In the above statement, we used a temporary name like "my_student_model". You can call it absolutely anything, like "sandy" or "person". Now, "my_student_model" has the parts and default values specified in the model definition.

For setting new values, we could do:

my_student_model.name = "Sandy P. Schmoe"
my_student_model.age = 16
my_student_model.score = 75

If you use Python, you can already see that I am influenced by the language. However, we might do better by saying:

set my_student_model name = "Sandy P. Schmoe"
set my_student_model age = 16
set my_student_model score = 75

I could see how this could be tedious if you are coming from a full-sized language. To appease those who want to pass in named parts at initialization, how about:

my_student_model = model student ( name = "Sandy P. Schmoe", age = 16, score = 75 )

I am a fan of named arguments and find the following to be confusing although terser:

my_student_model = model student ( "Sandy P. Schmoe", 16, 75 )

Now if we wanted to print out one of the model's parts, we could say:

print my_student_model.score

But perhaps this might be more friendly for some people:

print get my_student_model score

Both would print the value of 75. I like using dot notation, but others might now. Geeks are probably reading this going, well this might constitute being a model function. If this is you, relax and watch some Adult Swim. So we have a new model, which represents a student - now we need to institute our logic. Remember that criteria above regarding Sandy's test score? Here is the code for it:

if my_student_model.score >= 90 then:
print "The student is going to Disneyland! Hooray!"
but if my_student_model.score >= 80 then:
print "The student is getting a new Superman Returns toy!"
otherwise:
print "The student is going to be tutored over the summer =("

I think the above passage is completely self-explanatory. It almost (but not quite) reads like a series of English sentences. How is this code not maintainable or friendly?

So, the final version of this code is:

model student:
part name = "unknown"
part age = 0
part score = 0
my_student_model = model student ( name = "Sandy P. Schmoe", age = 16, score = 75 )
if my_student_model.score >= 90 then:
print "The student is going to Disneyland! Hooray!"
but if my_student_model.score >= 80 then:
print "The student is getting a new Superman Returns toy!"
otherwise:
print "The student is going to be tutored over the summer =("

This is 11 lines of code, where each line says something meaningful or defines something fairly clearly. We cannot throw out all principles of CS - afterall, this is a programming language. Instead we can just make things less complex. And look: there are no curly-braces, semi-colons, and real syntax barrier in the above code. A lot of these concepts exist in Python and Ruby. While there are serious short-comings and limitations for veteran coders, I think there might be some application of some of these ideas for people that struggle with learning a programming language.

About I am currently a Senior Engineer at MySpace. Feel free to check out my personal collective.

Archives
April 2005
May 2005
June 2005
July 2005
August 2005
September 2005
October 2005
November 2005
December 2005
January 2006
March 2006
April 2006
May 2006
June 2006
July 2006
August 2006
September 2006
October 2006
November 2006
January 2007
February 2007
April 2007
November 2007
December 2007
January 2008
March 2008
April 2008
May 2008
June 2008

Subscribe to my feed