Friday, December 19, 2008

Programming has evolved

When I look at the recent advancements in Behaviour Driven Development in Rails, I tend to think that programming has really evolved. Not in terms of advanced data structures and superior algorithms, but as a focus shift of intention.

Earlier the focus was the machine. Days are gone when we thought so much about saving an extra byte, trading code simplicity for speed. A compromise here, a saving there, ad infinitum.

The focus has definitely shifted to human understandability. Beauty of code now lies in its simplicity (some folks would argue that it always did, and they're right, but its more so apparent now).

First it was Ruby, then Rails, and now BDD frameworks like rSpec and Cucumber prove the point. Just have a look at these executable code examples.

Ruby
animals = [ "Tiger", "Cow", "Python", "Horse" ]
animals.each do |animal|
puts animal + "is cool!"
end
#=> Tiger is cool! Cow is cool! ...
Does a C for loop or C++ iterator look more elegant?

Rails
class Project
belongs_to :portfolio
has_one :project_manager
has_many :milestones
has_and_belongs_to_many :categories
end
Code that you can just read, isn't it?

rSpec
lambda {
employee.develop_great_new_social_networking_app
}.should change(employee, :title).from("Mail Clerk").to("CEO")
Yes, that's a real executable example.

And now, Cucumber
Scenario: Search by topic
Given there are 240 courses where neither has the topic "biology"
And there are 3 courses A,B,C that each have "biology" as one of the topics
When I search for "biology"
Then I should see a the following courses:
| title |
| A |
| B |
| C |

More beautiful, more succinct, more concise. That's what programming has evolved to be.

5 comments:

Shamail said...

Cool!
Yet another example in python:

class User ( SQLObject ):
name = UnicodeCol ()
group = RelatedJoin ( Group )

class Group ( SQLObject ):
groupName = UnicodeCol ()

Seems readable!! Isn't it..
And this corresponds to a SQL of nearly 25 lines to do a related join of two tables...

Cool Kazim!

D said...

wow!!!!!!!!!

Honey Singh said...

Yeah,
What a poetic way of blogging about the styles of different language's.
Hey "kazim" it reminds me of wordpress tag line where they says "Code is poetry" !!

Kazim Zaidi said...

Agree on that with you Shamail.
In fact, ORMs have come a long way and they've saved web developers from writing ugly long SQL queries. :-P (Or, in words of DHH, SQL isn't ugly, just too verbose in trivial situations).

Honey, never thought it would turn out poetic. But when you pointed it out I read it over again, and yeah, code's poetic. We are but poets, ;-).

Waseem Ahmad said...

Really nice post there!

The idea of beauty in the code really depends upon making it understandable to human. But it also depends upon the type of the application which We are working on. e.g. an OS developer will certainly care less about beauty and more about performance and algorithm and a web developer or an application developer will certainly do care about the code beauty. I do not say that these guys do not care about the performance. In fact most of the times if certain piece of code is made beautiful by refactoring and other technologies, it's performance also improves.

Now this all depends upon the type of languages we are working with. We all know Ruby, Python etc have very nice syntax. For the languages which do not have not so beautiful syntax, we can beautify their code by embedding nice short and beautiful comments. But we all know in an application the code itself is more changed than the comments (say because of we the programmers are very lazy even writing comments in our programs).

The real thing really is the intention. A new member in the development team is more interested in why and what of a certain thing which is happening in the code than to what the certain thing is. How is most of the times obvious. This makes all these languages self commenting.